1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12 * specific language governing permissions and limitations under the License.
13 *
14 */
15 package org.logicblaze.lingo.jmx.remote;
16
17 import java.net.URI;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.Map;
21 import javax.management.Attribute;
22 import javax.management.MBeanInfo;
23 import javax.management.MBeanServer;
24 import javax.management.MBeanServerConnection;
25 import javax.management.MBeanServerFactory;
26 import javax.management.Notification;
27 import javax.management.NotificationListener;
28 import javax.management.ObjectName;
29 import javax.management.monitor.GaugeMonitor;
30 import javax.management.remote.JMXConnector;
31 import javax.management.remote.JMXConnectorFactory;
32 import javax.management.remote.JMXConnectorServer;
33 import javax.management.remote.JMXConnectorServerFactory;
34 import javax.management.remote.JMXServiceURL;
35 import junit.framework.TestCase;
36 import org.apache.activemq.broker.BrokerFactory;
37 import org.apache.activemq.broker.BrokerService;
38 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
39
40 /***
41
42 */
43 public class JmxRemoteTest extends TestCase{
44 private MBeanServer server;
45 private BrokerService broker;
46 private JMXConnectorServer connectorServer;
47 private JMXConnector connector;
48 private ObjectName serviceName;
49 private SimpleService service;
50
51 protected void setUp() throws Exception{
52 broker=BrokerFactory.createBroker(new URI("broker:(tcp://localhost:6000)/localhost?persistent=false"));
53 broker.start();
54 server=MBeanServerFactory.createMBeanServer();
55
56 service = new SimpleService();
57
58 serviceName= new ObjectName("examples","mbean","simple");
59 server.registerMBean(service,serviceName);
60
61
62 JMXServiceURL url=new JMXServiceURL("service:jmx:jms:///tcp://localhost:6000");
63
64 Map env=new HashMap();
65 env.put("jmx.remote.protocol.provider.pkgs","org.logicblaze.lingo.jmx.remote.provider");
66 connectorServer=JMXConnectorServerFactory.newJMXConnectorServer(url,env,server);
67 connectorServer.start();
68
69 connector=JMXConnectorFactory.connect(url,env);
70
71 }
72
73 protected void tearDown() throws Exception{
74 connector.close();
75 connectorServer.stop();
76 broker.stop();
77 }
78
79 public void testSimpleRemoteJmx() throws Exception{
80
81
82 MBeanServerConnection connection=connector.getMBeanServerConnection();
83 ObjectName queryName=new ObjectName("*:*");
84 java.util.Set names=connection.queryNames(queryName,null);
85 for(Iterator iter=names.iterator();iter.hasNext();){
86 ObjectName name=(ObjectName) iter.next();
87 MBeanInfo beanInfo=connection.getMBeanInfo(name);
88 System.out.println("bean info = "+beanInfo.getDescription());
89 System.out.println("attrs = " + beanInfo.getAttributes());
90 }
91 Attribute attr = new Attribute("SimpleValue",new Integer(10));
92 connection.setAttribute(serviceName,attr);
93 Object value = connection.getAttribute(serviceName, "SimpleValue");
94 assertTrue(value.equals(new Integer(10)));
95 }
96
97 public void testNotificationsJmx() throws Exception{
98
99
100
101
102 GaugeMonitor monitorMBean=new GaugeMonitor();
103 ObjectName monitorName=new ObjectName("examples","monitor","gauge");
104 server.registerMBean(monitorMBean,monitorName);
105
106 monitorMBean.setThresholds(new Integer(8),new Integer(4));
107
108 monitorMBean.setNotifyHigh(true);
109 monitorMBean.setNotifyLow(true);
110
111 monitorMBean.setDifferenceMode(false);
112
113 monitorMBean.addObservedObject(serviceName);
114 monitorMBean.setObservedAttribute("SimpleCounter");
115
116 monitorMBean.setGranularityPeriod(50L);
117
118 MBeanServerConnection connection=connector.getMBeanServerConnection();
119 final AtomicBoolean notificationSet = new AtomicBoolean(false);
120
121
122 connection.addNotificationListener(monitorName, new NotificationListener(){
123 public void handleNotification(Notification notification,Object handback){
124 System.out.println("Notification = " + notification);
125 synchronized(notificationSet){
126 notificationSet.set(true);
127 notificationSet.notify();
128 }
129 }}, null, null);
130 service.start();
131 monitorMBean.start();
132 synchronized(notificationSet){
133 if (!notificationSet.get()){
134 notificationSet.wait(5000);
135 }
136 }
137 assertTrue(notificationSet.get());
138
139 }
140
141
142 }