001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jmx.invoker;
023:
024: import java.util.Timer;
025: import java.util.TimerTask;
026: import javax.management.ListenerNotFoundException;
027: import javax.management.NotificationListener;
028: import javax.management.NotificationFilter;
029: import javax.management.Notification;
030: import org.jboss.logging.Logger;
031: import org.jboss.mx.notification.AsynchNotificationBroadcasterSupport;
032: import org.jboss.util.threadpool.BasicThreadPool;
033: import org.jboss.util.threadpool.BlockingMode;
034: import org.w3c.dom.Element;
035:
036: /**
037: * Used in JMX invoker adaptor test.
038: *
039: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
040: * @version $Revision: 57211 $
041: *
042: * @jmx:mbean name="jboss.test:service=InvokerTest"
043: */
044: public class InvokerTest extends AsynchNotificationBroadcasterSupport
045: implements InvokerTestMBean {
046: static Logger log = Logger.getLogger(InvokerTest.class);
047:
048: private CustomClass custom = new CustomClass("InitialValue");
049: private NonserializableClass custom2 = new NonserializableClass();
050: private Element xml;
051:
052: public InvokerTest() {
053: BasicThreadPool pool = new BasicThreadPool();
054: pool.setBlockingMode(BlockingMode.RUN);
055: pool.setMaximumQueueSize(20);
056: pool.setMaximumPoolSize(1);
057: super .setThreadPool(pool);
058: /* With this set to 0, the testNotificationWithBadListener in
059: JMXInvokerUnitTestCase should fail due to the BadListener blocking the
060: server notification thread pool. With a value of
061: */
062: super .setNotificationTimeout(1000);
063: }
064:
065: /**
066: * @jmx:managed-attribute
067: */
068: public String getSomething() {
069: return "something";
070: }
071:
072: public void addNotificationListener(NotificationListener listener,
073: NotificationFilter filter, Object handback) {
074: log.info("addNotificationListener, listener: " + listener
075: + ", handback: " + handback);
076: super .addNotificationListener(listener, filter, handback);
077: if ("runTimer".equals(handback)) {
078: Timer t = new Timer();
079: Send10Notifies task = new Send10Notifies();
080: t.scheduleAtFixedRate(task, 0, 1000);
081: }
082: }
083:
084: public void removeNotificationListener(NotificationListener listener)
085: throws ListenerNotFoundException {
086: log.info("removeNotificationListener, listener: " + listener);
087: super .removeNotificationListener(listener);
088: }
089:
090: /**
091: * @jmx:managed-attribute
092: */
093: public CustomClass getCustom() {
094: return custom;
095: }
096:
097: /**
098: * @jmx:managed-attribute
099: */
100: public void setCustom(CustomClass custom) {
101: this .custom = custom;
102: }
103:
104: /**
105: * @jmx:managed-attribute
106: */
107: public NonserializableClass getNonserializableClass() {
108: return custom2;
109: }
110:
111: /**
112: * @jmx:managed-attribute
113: */
114: public void setNonserializableClass(NonserializableClass custom) {
115: this .custom2 = custom;
116: }
117:
118: /**
119: * @jmx:managed-attribute
120: */
121: public Element getXml() {
122: return xml;
123: }
124:
125: /**
126: * @jmx:managed-attribute
127: */
128: public void setXml(Element xml) {
129: this .xml = xml;
130: }
131:
132: /**
133: * @jmx:managed-operation
134: */
135: public CustomClass doSomething(CustomClass custom) {
136: return new CustomClass(custom.getValue());
137: }
138:
139: /**
140: * @jmx:managed-operation
141: */
142: public CustomClass doSomething() {
143: return new CustomClass(custom.getValue());
144: }
145:
146: /**
147: * @jmx:managed-operation
148: */
149: public void stop() {
150: stopThreadPool(true);
151: }
152:
153: private class Send10Notifies extends TimerTask {
154: int count;
155:
156: /**
157: * The action to be performed by this timer task.
158: */
159: public void run() {
160: log.info("Sending notification on timer, count=" + count);
161: Notification notify = new Notification("InvokerTest.timer",
162: InvokerTest.this , count);
163: InvokerTest.super .sendNotification(notify);
164: count++;
165: if (count == 10) {
166: super .cancel();
167: log.info("Cancelled timer");
168: }
169: }
170: }
171: }
|