KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > test > NotificationTester


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/test/NotificationTester.java,v 1.3 2005/12/25 03:45:53 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:53 $
28  */

29  
30 package com.sun.cli.jmx.test;
31
32
33 // java imports
34
//
35
import java.util.*;
36 import java.io.*;
37 import java.net.*;
38
39 // RI imports
40
//
41
import javax.management.ObjectName JavaDoc;
42 import javax.management.MBeanServer JavaDoc;
43 import javax.management.remote.JMXServiceURL JavaDoc;
44 import javax.management.remote.JMXConnectorFactory JavaDoc;
45 import javax.management.remote.JMXConnector JavaDoc;
46 import javax.management.MBeanServerConnection JavaDoc;
47 import javax.management.NotificationFilter JavaDoc;
48 import javax.management.NotificationListener JavaDoc;
49 import javax.management.Notification JavaDoc;
50 import javax.management.Attribute JavaDoc;
51
52 import com.sun.cli.jmx.support.CLISupportStrings;
53
54
55 /*
56     Trivial application that displays a string
57 */

58
59 public class NotificationTester extends Thread JavaDoc
60     implements NotificationListener JavaDoc
61 {
62     MBeanServerConnection JavaDoc mServer;
63     JMXConnector JavaDoc mConnection;
64     String JavaDoc mHost;
65     int mPort;
66     ThroughputCallback mThroughputCallback;
67
68     public static interface ThroughputCallback
69     {
70         public void throughputReport( long milliseconds, long numCalls );
71     }
72
73
74         private static void
75     p( Object JavaDoc arg )
76     {
77         System.out.println( arg.toString() );
78     }
79
80
81         public
82     NotificationTester( String JavaDoc host, int port ) throws Exception JavaDoc
83     {
84         mHost = host;
85         mPort = port;
86         
87         mConnection = Connect( host, port );
88         mServer = mConnection.getMBeanServerConnection( );
89     }
90     
91     
92         private JMXConnector JavaDoc
93     Connect( String JavaDoc host, int port ) throws Exception JavaDoc
94     {
95         final JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc( "service:jmx:jmxmp://" + host + ":" + port );
96             
97         JMXConnector JavaDoc conn = JMXConnectorFactory.connect( url );
98         return( conn );
99     }
100
101
102         public void
103     RunSinglePerConnection( int numConnections ) throws Exception JavaDoc
104     {
105         final long startTime = System.currentTimeMillis();
106         
107         final ObjectName JavaDoc name = new ObjectName JavaDoc( CLISupportStrings.CLI_SUPPORT_TARGET );
108         for( int i = 0; i < numConnections; ++i )
109         {
110             JMXConnector JavaDoc conn = Connect( mHost, mPort );
111             MBeanServerConnection JavaDoc server = conn.getMBeanServerConnection( );
112             
113             Object JavaDoc result = server.getAttribute( name, "NbChanges" );
114             conn.close();
115         }
116         
117         final long endTime = System.currentTimeMillis();
118         final long elapsed = endTime - startTime;
119         
120         p( "elapsed = " + elapsed );
121         p( "connections per second = " + numConnections / (elapsed / 1000.0) );
122     }
123     
124         public void
125     RunMultiplePerConnection( int outerCount, int numIterations ) throws Exception JavaDoc
126     {
127         final ObjectName JavaDoc name = new ObjectName JavaDoc( "foo" );
128         //final ObjectName name = new ObjectName( TestShared.kSimpleDynamicName );
129

130         for( int outer = 0; outer < outerCount; ++outer )
131         {
132             final long startTime = System.currentTimeMillis();
133             
134             for( int i = 0; i < numIterations; ++i )
135             {
136                 Object JavaDoc result = mServer.getAttribute( name, "NotifsEmitted" );
137             }
138             
139             final long endTime = System.currentTimeMillis();
140             final long elapsed = endTime - startTime;
141             
142             //p( mName + ": " + numIterations / (elapsed / 1000.0) + " iterations/sec" );
143
mThroughputCallback.throughputReport( elapsed, numIterations );
144         }
145     }
146     
147     class MyFilter implements NotificationFilter JavaDoc, Serializable
148     {
149         // Object mHost; // TestClient
150

151         public MyFilter( NotificationTester host )
152         {
153             // mHost = host;
154
}
155
156         public boolean isNotificationEnabled(Notification JavaDoc notification)
157         {
158             return( true );
159         }
160     }
161     
162     long mNotifCount = 0;
163     
164         public void
165     handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
166     {
167         ++mNotifCount;
168     }
169     
170         public void
171     RunNotifTest( long sleepMillis ) throws Exception JavaDoc
172     {
173         final ObjectName JavaDoc emitterName = new ObjectName JavaDoc( CLISupportStrings.CLI_SIMPLE_TESTEE_TARGET );
174         
175         mNotifCount = 0;
176         boolean success = false;;
177         
178         try
179         {
180             p( "adding listener" );
181             mServer.addNotificationListener( emitterName, this, null, null);
182             
183             p( "setting attribute" );
184             mServer.setAttribute( emitterName, new Attribute JavaDoc( "NotifMillis", new Long JavaDoc( sleepMillis ) ) );
185             
186             p( "starting" );
187             mServer.invoke( emitterName, "startNotif", null, null );
188             success = true;
189             p( "started" );
190         }
191         catch( Exception JavaDoc e )
192         {
193             p( "caught exception: " + e );
194         }
195         
196         long startTime = System.currentTimeMillis();
197         while( true )
198         {
199             Thread.sleep( 500 );
200             
201             final long notifCount = mNotifCount;
202             final long elapsedMillis = System.currentTimeMillis() - startTime;
203             final double rate = (double)notifCount / (elapsedMillis / 1000.0 );
204             System.out.println( "total notifications: " + notifCount + " = " + (rate *10.0)/10.0 + "/sec" );
205         }
206     }
207     
208     String JavaDoc mName;
209     int mOuterLoop;
210     int mNumIterations;
211     
212         public void
213     run()
214     {
215         try
216         {
217             RunMultiplePerConnection( mOuterLoop, mNumIterations );
218         }
219         catch( Exception JavaDoc e )
220         {
221             p( e );
222         }
223     }
224     
225         public void
226     RunMultiplePerConnectionThreaded(
227         String JavaDoc name,
228         int outerLoop,
229         int numIterations,
230         ThroughputCallback throughputCallback) throws Exception JavaDoc
231     {
232         mName = name;
233         mOuterLoop = outerLoop;
234         mNumIterations = numIterations;
235         mThroughputCallback = throughputCallback;
236         
237         this.start();
238     }
239
240
241 };
242
243
244
Popular Tags