KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > invoker > InvokerTest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jmx.invoker;
23
24 import java.util.Timer JavaDoc;
25 import java.util.TimerTask JavaDoc;
26 import javax.management.ListenerNotFoundException JavaDoc;
27 import javax.management.NotificationListener JavaDoc;
28 import javax.management.NotificationFilter JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import org.jboss.logging.Logger;
31 import org.jboss.mx.notification.AsynchNotificationBroadcasterSupport;
32 import org.jboss.mx.notification.NotificationFilterProxy;
33 import org.jboss.util.threadpool.BasicThreadPool;
34 import org.jboss.util.threadpool.BlockingMode;
35 import org.w3c.dom.Element JavaDoc;
36
37 /**
38  * Used in JMX invoker adaptor test.
39  *
40  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
41  * @version $Revision: 37406 $
42  *
43  * @jmx:mbean name="jboss.test:service=InvokerTest"
44  */

45 public class InvokerTest
46    extends AsynchNotificationBroadcasterSupport
47    implements InvokerTestMBean
48 {
49    static Logger log = Logger.getLogger(InvokerTest.class);
50
51    private CustomClass custom = new CustomClass("InitialValue");
52    private NonserializableClass custom2 = new NonserializableClass();
53    private Element JavaDoc xml;
54
55    public InvokerTest()
56    {
57       BasicThreadPool pool = new BasicThreadPool();
58       pool.setBlockingMode(BlockingMode.RUN);
59       pool.setMaximumQueueSize(20);
60       pool.setMaximumPoolSize(1);
61       super.setThreadPool(pool);
62       /* With this set to 0, the testNotificationWithBadListener in
63       JMXInvokerUnitTestCase should fail due to the BadListener blocking the
64       server notification thread pool. With a value of
65       */

66       super.setNotificationTimeout(1000);
67    }
68
69    /**
70     * @jmx:managed-attribute
71     */

72    public String JavaDoc getSomething()
73    {
74       return "something";
75    }
76
77    public void addNotificationListener(NotificationListener JavaDoc listener,
78       NotificationFilter JavaDoc filter, Object JavaDoc handback)
79    {
80       log.info("addNotificationListener, listener: "+listener+", handback: "+handback);
81       super.addNotificationListener(listener, filter, handback);
82       if( "runTimer".equals(handback) )
83       {
84          Timer JavaDoc t = new Timer JavaDoc();
85          Send10Notifies task = new Send10Notifies();
86          t.scheduleAtFixedRate(task, 0, 1000);
87       }
88       /**
89        * This had to be added for the jmx remoting (jsr-160) tests.
90        * Per spec, the handback will NOT be the same as what the client
91        * initially passed when calling addNotificationListener. Instead,
92        * in the case of our implementation, it will be a NotificationFilterProxy.
93        */

94       if(filter instanceof NotificationFilterProxy)
95       {
96          NotificationFilter JavaDoc delegateFilter = ((NotificationFilterProxy)filter).getFilter();
97          if(delegateFilter instanceof RunTimerFilter)
98          {
99             Timer JavaDoc t = new Timer JavaDoc();
100             Send10Notifies task = new Send10Notifies();
101             t.scheduleAtFixedRate(task, 0, 1000);
102          }
103       }
104
105    }
106
107    public void removeNotificationListener(NotificationListener JavaDoc listener)
108       throws ListenerNotFoundException JavaDoc
109    {
110       log.info("removeNotificationListener, listener: "+listener);
111       super.removeNotificationListener(listener);
112    }
113
114    /**
115     * @jmx:managed-attribute
116     */

117    public CustomClass getCustom()
118    {
119       return custom;
120    }
121
122    /**
123     * @jmx:managed-attribute
124     */

125    public void setCustom(CustomClass custom)
126    {
127       this.custom = custom;
128    }
129
130    /**
131     * @jmx:managed-attribute
132     */

133    public NonserializableClass getNonserializableClass()
134    {
135       return custom2;
136    }
137    /**
138     * @jmx:managed-attribute
139     */

140    public void setNonserializableClass(NonserializableClass custom)
141    {
142       this.custom2 = custom;
143    }
144
145    /**
146     * @jmx:managed-attribute
147     */

148    public Element JavaDoc getXml()
149    {
150       return xml;
151    }
152    /**
153     * @jmx:managed-attribute
154     */

155    public void setXml(Element JavaDoc xml)
156    {
157       this.xml = xml;
158    }
159
160    /**
161     * @jmx:managed-operation
162     */

163    public CustomClass doSomething(CustomClass custom)
164    {
165       return new CustomClass(custom.getValue());
166    }
167    /**
168     * @jmx:managed-operation
169     */

170    public CustomClass doSomething()
171    {
172       return new CustomClass(custom.getValue());
173    }
174
175    /**
176     * @jmx:managed-operation
177     */

178    public void stop()
179    {
180       stopThreadPool(true);
181    }
182
183    private class Send10Notifies extends TimerTask JavaDoc
184    {
185       int count;
186       /**
187        * The action to be performed by this timer task.
188        */

189       public void run()
190       {
191          log.info("Sending notification on timer, count="+count);
192          Notification JavaDoc notify = new Notification JavaDoc("InvokerTest.timer",
193             InvokerTest.this, count);
194          InvokerTest.super.sendNotification(notify);
195          count ++;
196          if( count == 10 )
197          {
198             super.cancel();
199             log.info("Cancelled timer");
200          }
201       }
202    }
203 }
204
Popular Tags