KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > callback > pull > memory > callbackstore > 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.callbackstore;
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 = 5412;
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          // now need to go get the callbacks until none left.
111
int callbacksReceived = getAllCallbacks();
112
113          System.out.println("callbacks received = " + callbacksReceived);
114          totalCallbacks = totalCallbacks + callbacksReceived;
115       }
116
117       System.out.println("total callbacks received: " + totalCallbacks);
118       System.out.println("total callbacks expected: " + numberOfCallbacks);
119
120       assertEquals(numberOfCallbacks, totalCallbacks);
121
122
123       // remove callback handler from server
124
// remotingClient.removeListener(pushCallbackHandler);
125

126
127
128 // List callbacks = remotingClient.getCallbacks();
129
// Iterator itr = callbacks.iterator();
130
// while(itr.hasNext())
131
// {
132
// Object obj = itr.next();
133
// if(obj instanceof Callback)
134
// {
135
// System.out.println("Callback value = " + ((Callback)obj).getCallbackObject());
136
// }
137
// else
138
// {
139
// System.out.println("Callback value = " + obj);
140
// }
141
// }
142
//
143
// // remove callback handler from server
144
remotingClient.removeListener(pullCallbackHandler);
145       remotingClient.disconnect();
146       remotingClient = null;
147    }
148
149    /**
150     * calculate how many 102400 byte callback messages it will take to consume 30%
151     * of the vm's memory. The CallbackInvocationHandler will take care of consuming 70%
152     * so need to make sure we have enough callbacks to trigger persistence.
153     */

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

230    public void setupServer(InvokerLocator locator) throws Exception JavaDoc
231    {
232       System.out.println("Starting remoting server with locator uri of: " + locator);
233       Connector connector = new Connector();
234       connector.setInvokerLocator(locator.getLocatorURI());
235       connector.start();
236
237       CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler();
238       // first parameter is sub-system name. can be any String value.
239
connector.addInvocationHandler("sample", invocationHandler);
240    }
241
242
243    /**
244     * Can pass transport and port to be used as parameters.
245     * Valid transports are 'rmi' and 'socket'.
246     *
247     * @param args
248     */

249    public static void main(String JavaDoc[] args)
250    {
251       if(args != null && args.length == 2)
252       {
253          transport = args[0];
254          port = Integer.parseInt(args[1]);
255       }
256       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
257       CallbackTestClient client = new CallbackTestClient(CallbackTestClient.class.getName(), locatorURI);
258       try
259       {
260          MultipleTestRunner runner = new MultipleTestRunner();
261          runner.doRun(client, true);
262       }
263       catch(Throwable JavaDoc e)
264       {
265          e.printStackTrace();
266          System.exit(1);
267       }
268       System.exit(0);
269    }
270
271    public class PushCallbackHandler extends CallbackHandler
272    {
273
274    }
275
276    public class CallbackHandler implements InvokerCallbackHandler
277    {
278       boolean isComplete = false;
279
280       /**
281        * Will take the callback message and send back to client.
282        * If client locator is null, will store them till client polls to get them.
283        *
284        * @param invocation
285        * @throws org.jboss.remoting.HandleCallbackException
286        *
287        */

288       public void handleCallback(InvocationRequest invocation) throws HandleCallbackException
289       {
290          if(invocation instanceof Callback)
291          {
292             Callback callback = (Callback) invocation;
293             System.out.println("Received callback value of: " + callback.getCallbackObject());
294             System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
295             System.out.println("Received callback server invoker of: " + callback.getServerLocator());
296          }
297          else
298          {
299             System.out.println("Received callback value of: " + invocation.getParameter());
300          }
301          isComplete = true;
302       }
303
304       public boolean isComplete()
305       {
306          return isComplete;
307       }
308    }
309
310 }
Popular Tags