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          //register a DynamicService from mx4j
56          service = new SimpleService();
57          
58          serviceName= new ObjectName("examples","mbean","simple");
59          server.registerMBean(service,serviceName);
60          // start the connector server
61          //JMXServiceURL url=new JMXServiceURL("service:jmx:jms:///vm://localhost");
62          JMXServiceURL url=new JMXServiceURL("service:jmx:jms:///tcp://localhost:6000");
63          // Create and start the RMIConnectorServer
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          // Connect a JSR 160 JMXConnector to the server side
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          // Retrieve an MBeanServerConnection that represent the MBeanServer the remote
81          // connector server is bound to
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          // Now let's register a Monitor
100         // We would like to know if we have peaks in activity, so we can use JMX's
101         // GaugeMonitor
102         GaugeMonitor monitorMBean=new GaugeMonitor();
103         ObjectName monitorName=new ObjectName("examples","monitor","gauge");
104         server.registerMBean(monitorMBean,monitorName);
105         // Setup the monitor: we want to be notified if we have too many clients or too less
106         monitorMBean.setThresholds(new Integer(8),new Integer(4));
107         // Setup the monitor: we want to know if a threshold is exceeded
108         monitorMBean.setNotifyHigh(true);
109         monitorMBean.setNotifyLow(true);
110        
111         monitorMBean.setDifferenceMode(false);
112         // Setup the monitor: link to the service MBean
113         monitorMBean.addObservedObject(serviceName);
114         monitorMBean.setObservedAttribute("SimpleCounter");
115         // Setup the monitor: a short granularity period
116         monitorMBean.setGranularityPeriod(50L);
117         // Setup the monitor: register a listener
118         MBeanServerConnection connection=connector.getMBeanServerConnection();
119         final AtomicBoolean notificationSet = new AtomicBoolean(false);
120         //Add a notification listener to the connection - to 
121         //test for notifications across lingo
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 }