View Javadoc

1   /**
2    * SimpleMonitorableBean.java
3    *
4    * @author Created by Omnicore CodeGuide
5    */
6   
7   package com.builder.uk.watchme;
8   
9   /*
10   * #%L
11   * phynixx-jmx
12   * %%
13   * Copyright (C) 2014 csc
14   * %%
15   * Licensed under the Apache License, Version 2.0 (the "License");
16   * you may not use this file except in compliance with the License.
17   * You may obtain a copy of the License at
18   * 
19   *      http://www.apache.org/licenses/LICENSE-2.0
20   * 
21   * Unless required by applicable law or agreed to in writing, software
22   * distributed under the License is distributed on an "AS IS" BASIS,
23   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24   * See the License for the specific language governing permissions and
25   * limitations under the License.
26   * #L%
27   */
28  
29  
30  import java.beans.PropertyChangeListener;
31  import java.beans.PropertyChangeSupport;
32  
33  public class WatchMeBean implements Runnable, WatchMeBeanMBean {
34      private PropertyChangeSupport changes = new PropertyChangeSupport(this);
35  
36      int count;
37      String msg;
38  
39      public WatchMeBean() {
40          count = 0;
41          setMsg("Initialised");
42      }
43  
44      public int getCount() {
45          return count;
46      }
47  
48      public void incCount() {
49          int oldcount = count;
50          count = count + 1;
51          changes.firePropertyChange("count", oldcount, count);
52      }
53  
54      public void setMsg(String msg) {
55          String oldmsg = this.msg;
56  
57          this.msg = msg;
58  
59          changes.firePropertyChange("msg", oldmsg, msg);
60      }
61  
62      public String getMsg() {
63          return msg;
64      }
65  
66      public void run() {
67          while (true) {
68              try {
69                  Thread.sleep(1000);
70              } catch (InterruptedException e) {
71              }
72  
73              incCount();
74          }
75      }
76  
77      public void reset() {
78          int oldcount = count;
79          count = 0;
80          changes.firePropertyChange("count", oldcount, count);
81      }
82  
83      public void addPropertyChangeListener(PropertyChangeListener l) {
84          changes.addPropertyChangeListener(l);
85      }
86  
87      public void removePropertyChangeListener(PropertyChangeListener l) {
88          changes.removePropertyChangeListener(l);
89      }
90  }
91