KickJava   Java API By Example, From Geeks To Geeks.

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


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.test.remoting.callback.pull.memory.nullstore;
8
9 import java.util.List JavaDoc;
10 import org.jboss.remoting.Client;
11 import org.jboss.remoting.InvokerLocator;
12 import org.jboss.remoting.callback.Callback;
13 import org.jboss.remoting.callback.HandleCallbackException;
14 import org.jboss.remoting.callback.InvokerCallbackHandler;
15 import org.jboss.remoting.samples.callback.CallbackServer;
16 import org.jboss.remoting.transport.Connector;
17
18 /**
19  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
20  */

21 public class JBossASCallbackTestClient
22 {
23    // Default locator values
24
private static String JavaDoc transport = "socket";
25    private static String JavaDoc host = "localhost";
26    private static int port = 5411;
27
28    private String JavaDoc locatorURI = null;
29
30    private Client remotingClient;
31    private CallbackHandler pullCallbackHandler;
32
33    private boolean isCallbackDone = false;
34
35    private int numberOfCallbacks = 520;
36
37    public JBossASCallbackTestClient(String JavaDoc name, String JavaDoc locatorURI)
38    {
39       this.locatorURI = locatorURI;
40    }
41
42    public void createRemotingClient() throws Exception JavaDoc
43    {
44       InvokerLocator locator = new InvokerLocator(locatorURI);
45       System.out.println("Calling remoting server with locator uri of: " + locatorURI);
46
47       // This could have been new Client(locator), but want to show that subsystem param is null
48
// Could have also been new Client(locator, "sample");
49
remotingClient = new Client(locator, null);
50
51    }
52
53    public void makeInvocation(String JavaDoc param) throws Throwable JavaDoc
54    {
55       Object JavaDoc response = remotingClient.invoke(param, null);
56       System.out.println("Invocation response: " + response);
57    }
58
59    public void testPullCallback() throws Throwable JavaDoc
60    {
61       createRemotingClient();
62       numberOfCallbacks = calculateNumberOfCallbacks();
63       System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks);
64       pullCallbackHandler = new CallbackHandler();
65       // by passing only the callback handler, will indicate pull callbacks
66
remotingClient.addListener(pullCallbackHandler);
67
68       // need to tell server handler how many
69
makeInvocation("" + numberOfCallbacks);
70
71       // now make invocation on server, which should cause a callback to happen
72
makeInvocation("Do something");
73
74       boolean didItWork = checkForCallback();
75
76       System.out.println("Did id work = " + didItWork);
77
78       int totalCallbacks = 0;
79       if(didItWork)
80       {
81          boolean gotExpectedException = false;
82
83          try
84          {
85             // now need to go get the callbacks until none left.
86
int callbacksReceived = getAllCallbacks(pullCallbackHandler);
87
88             System.out.println("callbacks received = " + callbacksReceived);
89             totalCallbacks = totalCallbacks + callbacksReceived;
90
91          }
92          catch(RuntimeException JavaDoc re)
93          {
94             System.out.println("Got exception as expected - " + re);
95             gotExpectedException = true;
96          }
97          catch(Throwable JavaDoc thr)
98          {
99             System.out.println("Got unexpected exception - " + thr);
100          }
101
102          if(gotExpectedException)
103          {
104             System.out.println("PASSED");
105          }
106          else
107          {
108             System.out.println("FAILED");
109          }
110       }
111
112       System.out.println("total callbacks received: " + totalCallbacks);
113       System.out.println("total callbacks expected: " + numberOfCallbacks);
114
115       remotingClient.removeListener(pullCallbackHandler);
116       remotingClient.disconnect();
117       remotingClient = null;
118    }
119
120    /**
121     * calculate how many 102400 byte callback messages it will take to consume 30%
122     * of the vm's memory. The CallbackInvocationHandler will take care of consuming 70%
123     * so need to make sure we have enough callbacks to trigger persistence.
124     */

125    private int calculateNumberOfCallbacks()
126    {
127       long max = Runtime.getRuntime().maxMemory();
128       int targetMem = (int) (max * 0.3);
129       int num = targetMem / 102400;
130       return num;
131    }
132
133    private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable JavaDoc
134    {
135       int counter = 0;
136       List JavaDoc callbacks = null;
137
138       callbacks = remotingClient.getCallbacks(pullCallbackHandler);
139       while(callbacks.size() > 0)
140       {
141          System.out.println("callbacks.size() = " + callbacks.size());
142          counter = counter + callbacks.size();
143          for(int i = 0; i < callbacks.size(); i++)
144          {
145             ((Callback) callbacks.get(i)).getCallbackObject();
146          }
147
148          // need to give time for server to clean up mem
149
Thread.currentThread().sleep(2000);
150          callbacks = remotingClient.getCallbacks(pullCallbackHandler);
151       }
152       return counter;
153    }
154
155    private boolean checkForCallback() throws Throwable JavaDoc
156    {
157       boolean isComplete = false;
158
159       int waitPeriod = 1000;
160       int numOfWaits = 360; // means will wait 30 seconds
161
for(int x = 0; x < numOfWaits; x++)
162       {
163          //isComplete = pushCallbackHandler.isComplete();
164
isComplete = ((Boolean JavaDoc) remotingClient.invoke("getdone")).booleanValue();
165          if(!isComplete)
166          {
167             try
168             {
169                Thread.currentThread().sleep(waitPeriod);
170             }
171             catch(InterruptedException JavaDoc e)
172             {
173                e.printStackTrace();
174             }
175          }
176          else
177          {
178             break;
179          }
180       }
181       return isComplete;
182    }
183
184    public void setupServer(InvokerLocator locator) throws Exception JavaDoc
185    {
186       System.out.println("Starting remoting server with locator uri of: " + locator);
187       Connector connector = new Connector();
188       connector.setInvokerLocator(locator.getLocatorURI());
189       connector.start();
190
191       CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler();
192       // first parameter is sub-system name. can be any String value.
193
connector.addInvocationHandler("sample", invocationHandler);
194    }
195
196
197    /**
198     * Can pass transport and port to be used as parameters.
199     * Valid transports are 'rmi' and 'socket'.
200     *
201     * @param args
202     */

203    public static void main(String JavaDoc[] args)
204    {
205       if(args != null && args.length == 2)
206       {
207          transport = args[0];
208          port = Integer.parseInt(args[1]);
209       }
210       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
211       JBossASCallbackTestClient client = new JBossASCallbackTestClient(JBossASCallbackTestClient.class.getName(), locatorURI);
212       try
213       {
214          client.testPullCallback();
215       }
216       catch(Throwable JavaDoc e)
217       {
218          e.printStackTrace();
219          System.exit(1);
220       }
221       System.exit(0);
222    }
223
224    public class PushCallbackHandler extends CallbackHandler
225    {
226
227    }
228
229    public class CallbackHandler implements InvokerCallbackHandler
230    {
231       boolean isComplete = false;
232
233       /**
234        * Will take the callback message and send back to client.
235        * If client locator is null, will store them till client polls to get them.
236        *
237        * @param callback
238        * @throws org.jboss.remoting.callback.HandleCallbackException
239        *
240        */

241       public void handleCallback(Callback callback) throws HandleCallbackException
242       {
243          System.out.println("Received callback value of: " + callback.getCallbackObject());
244          System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
245          System.out.println("Received callback server invoker of: " + callback.getServerLocator());
246          isComplete = true;
247       }
248
249       public boolean isComplete()
250       {
251          return isComplete;
252       }
253    }
254
255 }
Popular Tags