KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > callback > pull > memory > nullstore > CallbackTestClient


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.callback.pull.memory.nullstore;
8
9 import org.jboss.dtf.MultipleTestRunner;
10 import org.jboss.remoting.AbstractInvokerTest;
11 import org.jboss.remoting.Callback;
12 import org.jboss.remoting.Client;
13 import org.jboss.remoting.HandleCallbackException;
14 import org.jboss.remoting.InvocationRequest;
15 import org.jboss.remoting.InvokerCallbackHandler;
16 import org.jboss.remoting.InvokerLocator;
17 import org.jboss.remoting.transport.Connector;
18 import org.jboss.samples.callback.CallbackServer;
19
20 import java.util.List JavaDoc;
21
22 /**
23  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
24  */

25 public class CallbackTestClient extends AbstractInvokerTest
26 {
27    // Default locator values
28
private static String JavaDoc transport = "socket";
29    private static String JavaDoc host = "localhost";
30    private static int port = 5411;
31
32    private String JavaDoc locatorURI = null;
33
34    private Client remotingClient;
35    private CallbackHandler pullCallbackHandler;
36 // private PushCallbackHandler pushCallbackHandler;
37

38    private boolean isCallbackDone = false;
39
40    private int numberOfCallbacks = 520;
41
42    public CallbackTestClient(String JavaDoc name)
43    {
44       super(name);
45    }
46
47    public CallbackTestClient(String JavaDoc name, String JavaDoc locatorURI)
48    {
49       super(name);
50       this.locatorURI = locatorURI;
51    }
52
53    public CallbackTestClient(String JavaDoc name, int numberOfInstances)
54    {
55       super(name, numberOfInstances);
56    }
57
58    public CallbackTestClient(String JavaDoc name, String JavaDoc transport, int port)
59    {
60       super(name, transport, port);
61    }
62
63    public CallbackTestClient(String JavaDoc name, String JavaDoc transport, int port, int numberOfInstances)
64    {
65       super(name, transport, port, numberOfInstances);
66    }
67
68
69    public void createRemotingClient() throws Exception JavaDoc
70    {
71       InvokerLocator locator = new InvokerLocator(locatorURI);
72       System.out.println("Calling remoting server with locator uri of: " + locatorURI);
73
74       // This could have been new Client(locator), but want to show that subsystem param is null
75
// Could have also been new Client(locator, "sample");
76
remotingClient = new Client(locator, null);
77
78    }
79
80    public void makeInvocation(String JavaDoc param) throws Throwable JavaDoc
81    {
82       Object JavaDoc response = remotingClient.invoke(param, null);
83       System.out.println("Invocation response: " + response);
84    }
85
86    public void testPullCallback() throws Throwable JavaDoc
87    {
88       createRemotingClient();
89       numberOfCallbacks = calculateNumberOfCallbacks();
90       System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks);
91       pullCallbackHandler = new CallbackHandler();
92       // by passing only the callback handler, will indicate pull callbacks
93
remotingClient.addListener(pullCallbackHandler);
94
95 // setupCallbackServer();
96

97       // need to tell server handler how many
98
makeInvocation("" + numberOfCallbacks);
99
100       // now make invocation on server, which should cause a callback to happen
101
makeInvocation("Do something");
102
103       boolean didItWork = checkForCallback();
104
105       System.out.println("Did id work = " + didItWork);
106
107       int totalCallbacks = 0;
108       if(didItWork)
109       {
110          boolean gotExpectedException = false;
111
112          try
113          {
114             // now need to go get the callbacks until none left.
115
int callbacksReceived = getAllCallbacks();
116
117             System.out.println("callbacks received = " + callbacksReceived);
118             totalCallbacks = totalCallbacks + callbacksReceived;
119
120          }
121          catch(RuntimeException JavaDoc re)
122          {
123             System.out.println("Got exception as expected - " + re);
124             gotExpectedException = true;
125          }
126          catch(Throwable JavaDoc thr)
127          {
128             System.out.println("Got unexpected exception - " + thr);
129          }
130
131          if(gotExpectedException)
132          {
133             System.out.println("PASSED");
134          }
135          else
136          {
137             System.out.println("FAILED");
138          }
139          assertTrue(gotExpectedException);
140       }
141
142       System.out.println("total callbacks received: " + totalCallbacks);
143       System.out.println("total callbacks expected: " + numberOfCallbacks);
144
145
146       // remove callback handler from server
147
// remotingClient.removeListener(pushCallbackHandler);
148

149
150
151 // List callbacks = remotingClient.getCallbacks();
152
// Iterator itr = callbacks.iterator();
153
// while(itr.hasNext())
154
// {
155
// Object obj = itr.next();
156
// if(obj instanceof Callback)
157
// {
158
// System.out.println("Callback value = " + ((Callback)obj).getCallbackObject());
159
// }
160
// else
161
// {
162
// System.out.println("Callback value = " + obj);
163
// }
164
// }
165
//
166
// // remove callback handler from server
167
remotingClient.removeListener(pullCallbackHandler);
168       remotingClient.disconnect();
169       remotingClient = null;
170    }
171
172    /**
173     * calculate how many 102400 byte callback messages it will take to consume 30%
174     * of the vm's memory. The CallbackInvocationHandler will take care of consuming 70%
175     * so need to make sure we have enough callbacks to trigger persistence.
176     */

177    private int calculateNumberOfCallbacks()
178    {
179       long max = Runtime.getRuntime().maxMemory();
180       int targetMem = (int) (max * 0.3);
181       int num = targetMem / 102400;
182       return num;
183    }
184
185    private int getAllCallbacks() throws Throwable JavaDoc
186    {
187       int counter = 0;
188       List JavaDoc callbacks = null;
189
190       callbacks = remotingClient.getCallbacks();
191       while(callbacks.size() > 0)
192       {
193          System.out.println("callbacks.size() = " + callbacks.size());
194          counter = counter + callbacks.size();
195          for(int i = 0; i < callbacks.size(); i++)
196          {
197             ((Callback) callbacks.get(i)).getCallbackObject();
198          }
199
200          // need to give time for server to clean up mem
201
Thread.currentThread().sleep(2000);
202          callbacks = remotingClient.getCallbacks();
203       }
204       return counter;
205    }
206
207    private boolean checkForCallback() throws Throwable JavaDoc
208    {
209       boolean isComplete = false;
210
211       int waitPeriod = 1000;
212       int numOfWaits = 360; // means will wait 30 seconds
213
for(int x = 0; x < numOfWaits; x++)
214       {
215          //isComplete = pushCallbackHandler.isComplete();
216
isComplete = ((Boolean JavaDoc) remotingClient.invoke("getdone")).booleanValue();
217          if(!isComplete)
218          {
219             try
220             {
221                Thread.currentThread().sleep(waitPeriod);
222             }
223             catch(InterruptedException JavaDoc e)
224             {
225                e.printStackTrace();
226             }
227          }
228          else
229          {
230             break;
231          }
232       }
233       return isComplete;
234    }
235
236 // private void setupCallbackServer() throws Throwable
237
// {
238
// // Using loctor with port value one higher than the target server
239
// String callbackLocatorURI = transport + "://" + host + ":" + (port + 1);
240
// InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
241
//
242
// // call to create remoting server to
243
// // receive client callbacks.
244
// setupServer(callbackLocator);
245
//
246
// pushCallbackHandler = new PushCallbackHandler();
247
// // Callback handle object will be passed back as part of the callback object
248
// String callbackHandleObject = "pushcallback";
249
// // by passing only the callback handler, will indicate pull callbacks
250
// remotingClient.addListener(pushCallbackHandler, callbackLocator, callbackHandleObject);
251
// }
252

253    public void setupServer(InvokerLocator locator) throws Exception JavaDoc
254    {
255       System.out.println("Starting remoting server with locator uri of: " + locator);
256       Connector connector = new Connector();
257       connector.setInvokerLocator(locator.getLocatorURI());
258       connector.start();
259
260       CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler();
261       // first parameter is sub-system name. can be any String value.
262
connector.addInvocationHandler("sample", invocationHandler);
263    }
264
265
266    /**
267     * Can pass transport and port to be used as parameters.
268     * Valid transports are 'rmi' and 'socket'.
269     *
270     * @param args
271     */

272    public static void main(String JavaDoc[] args)
273    {
274       if(args != null && args.length == 2)
275       {
276          transport = args[0];
277          port = Integer.parseInt(args[1]);
278       }
279       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
280       CallbackTestClient client = new CallbackTestClient(CallbackTestClient.class.getName(), locatorURI);
281       try
282       {
283          MultipleTestRunner runner = new MultipleTestRunner();
284          runner.doRun(client, true);
285       }
286       catch(Throwable JavaDoc e)
287       {
288          e.printStackTrace();
289          System.exit(1);
290       }
291       System.exit(0);
292    }
293
294    public class PushCallbackHandler extends CallbackHandler
295    {
296
297    }
298
299    public class CallbackHandler implements InvokerCallbackHandler
300    {
301       boolean isComplete = false;
302
303       /**
304        * Will take the callback message and send back to client.
305        * If client locator is null, will store them till client polls to get them.
306        *
307        * @param invocation
308        * @throws org.jboss.remoting.HandleCallbackException
309        *
310        */

311       public void handleCallback(InvocationRequest invocation) throws HandleCallbackException
312       {
313          if(invocation instanceof Callback)
314          {
315             Callback callback = (Callback) invocation;
316             System.out.println("Received callback value of: " + callback.getCallbackObject());
317             System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
318             System.out.println("Received callback server invoker of: " + callback.getServerLocator());
319          }
320          else
321          {
322             System.out.println("Received callback value of: " + invocation.getParameter());
323          }
324          isComplete = true;
325       }
326
327       public boolean isComplete()
328       {
329          return isComplete;
330       }
331    }
332
333 }
Popular Tags