KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > 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.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
16 import junit.framework.TestCase;
17
18 /**
19  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
20  */

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

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

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