KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > 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.test.remoting.callback.pull.memory.callbackstore;
8
9 import java.util.List JavaDoc;
10 import org.jboss.logging.Logger;
11 import org.jboss.remoting.Client;
12 import org.jboss.remoting.InvokerLocator;
13 import org.jboss.remoting.callback.Callback;
14 import org.jboss.remoting.callback.HandleCallbackException;
15 import org.jboss.remoting.callback.InvokerCallbackHandler;
16
17 import junit.framework.TestCase;
18
19 /**
20  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
21  */

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

124    private int calculateNumberOfCallbacks()
125    {
126       long max = Runtime.getRuntime().maxMemory();
127       int targetMem = (int) (max * 0.3);
128       int num = targetMem / 102400;
129       return num;
130    }
131
132    private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable JavaDoc
133    {
134       int counter = 0;
135       List JavaDoc callbacks = null;
136
137       callbacks = remotingClient.getCallbacks(pullCallbackHandler);
138       while(callbacks.size() > 0)
139       {
140          System.out.println("callbacks.size() = " + callbacks.size());
141          counter = counter + callbacks.size();
142          for(int i = 0; i < callbacks.size(); i++)
143          {
144             ((Callback) callbacks.get(i)).getCallbackObject();
145          }
146
147          // need to give time for server to clean up mem
148
Thread.currentThread().sleep(2000);
149          callbacks = remotingClient.getCallbacks(pullCallbackHandler);
150       }
151       return counter;
152    }
153
154    private boolean checkForCallback() throws Throwable JavaDoc
155    {
156       boolean isComplete = false;
157
158       int waitPeriod = 1000;
159       int numOfWaits = 600000;
160       for(int x = 0; x < numOfWaits; x++)
161       {
162          //isComplete = pushCallbackHandler.isComplete();
163
isComplete = ((Boolean JavaDoc) remotingClient.invoke("getdone")).booleanValue();
164          if(!isComplete)
165          {
166             try
167             {
168                Thread.currentThread().sleep(waitPeriod);
169             }
170             catch(InterruptedException JavaDoc e)
171             {
172                e.printStackTrace();
173             }
174          }
175          else
176          {
177             break;
178          }
179       }
180       return isComplete;
181    }
182
183    public static void main(String JavaDoc[] args)
184    {
185       CallbackTestClient client = new CallbackTestClient();
186       try
187       {
188          client.setUp();
189          client.testPullCallback();
190          client.tearDown();
191       }
192       catch(Throwable JavaDoc throwable)
193       {
194          throwable.printStackTrace();
195       }
196    }
197
198
199    public class PushCallbackHandler extends CallbackHandler
200    {
201
202    }
203
204    public class CallbackHandler implements InvokerCallbackHandler
205    {
206       boolean isComplete = false;
207
208       /**
209        * Will take the callback message and send back to client.
210        * If client locator is null, will store them till client polls to get them.
211        *
212        * @param callback
213        * @throws org.jboss.remoting.callback.HandleCallbackException
214        *
215        */

216       public void handleCallback(Callback callback) throws HandleCallbackException
217       {
218           System.out.println("Received callback value of: " + callback.getCallbackObject());
219           System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
220           System.out.println("Received callback server invoker of: " + callback.getServerLocator());
221           isComplete = true;
222       }
223
224       public boolean isComplete()
225       {
226          return isComplete;
227       }
228    }
229
230 }
Popular Tags