1
package org.jboss.test.remoting.callback.pull.memory.blocking;
3
import junit.framework.TestCase;
4
import org.jboss.remoting.Client;
5
import org.jboss.remoting.InvokerLocator;
6
import org.jboss.remoting.callback.Callback;
7
import org.jboss.remoting.callback.HandleCallbackException;
8
import org.jboss.remoting.callback.InvokerCallbackHandler;
9
import org.jboss.test.remoting.callback.pull.memory.TestCallback;
11
import java.util.List;
14
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
16
public class CallbackTestClient extends TestCase
18
// Default locator values
19
private static String transport = "socket";
20
private static String host = "localhost";
21
private static int port = 5411;
23
private String locatorURI = transport + "://" + host + ":" + port;
25
private Client remotingClient;
26
private CallbackHandler pullCallbackHandler;
28
private boolean isCallbackDone = false;
30
private int numberOfCallbacks = 520;
32
public void createRemotingClient() throws Exception
34
InvokerLocator locator = new InvokerLocator(locatorURI);
35
System.out.println("Calling remoting server with locator uri of: " + locatorURI);
37
// This could have been new Client(locator), but want to show that subsystem param is null
38
// Could have also been new Client(locator, "sample");
39
remotingClient = new Client(locator);
40
remotingClient.connect();
44
public void makeInvocation(String param) throws Throwable
46
Object response = remotingClient.invoke(param, null);
47
System.out.println("Invocation response: " + response);
50
public void setUp() throws Exception
52
createRemotingClient();
55
public void tearDown() throws Exception
57
if(remotingClient != null)
59
if(pullCallbackHandler != null)
63
remotingClient.removeListener(pullCallbackHandler);
65
catch(Throwable throwable)
67
throw new Exception(throwable);
70
remotingClient.disconnect();
74
public void testPullCallback() throws Throwable
76
numberOfCallbacks = calculateNumberOfCallbacks();
77
System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks);
78
pullCallbackHandler = new CallbackHandler();
79
// by passing only the callback handler, will indicate pull callbacks
80
remotingClient.addListener(pullCallbackHandler);
82
// need to tell server handler how many
83
makeInvocation("" + numberOfCallbacks);
85
// now make invocation on server, which should cause a callback to happen
86
makeInvocation("Do something");
88
boolean didItWork = checkForCallback();
90
System.out.println("Did id work = " + didItWork);
92
int totalCallbacks = 0;
95
// boolean gotExpectedException = false;
99
// now need to go get the callbacks until none left.
100
int callbacksReceived = getAllCallbacks(pullCallbackHandler);
102
System.out.println("callbacks received = " + callbacksReceived);
103
totalCallbacks = totalCallbacks + callbacksReceived;
106
// catch(RuntimeException re)
108
// System.out.println("Got exception as expected - " + re);
109
// gotExpectedException = true;
113
System.out.println("Got unexpected exception - " + thr);
116
System.out.println("total callbacks received: " + totalCallbacks);
117
System.out.println("total callbacks expected: " + numberOfCallbacks);
120
assertEquals(numberOfCallbacks, totalCallbacks);
124
List callbacks = remotingClient.getCallbacks(pullCallbackHandler);
125
System.out.println("callbacks size = " + callbacks.size());
126
assertEquals(10, callbacks.size());
129
// if(gotExpectedException)
131
// System.out.println("PASSED");
135
// System.out.println("FAILED");
137
// assertTrue(gotExpectedException);
144
* calculate how many 102400 byte callback messages it will take to consume 30%
145
* of the vm's memory. The CallbackInvocationHandler will take care of consuming 70%
146
* so need to make sure we have enough callbacks to trigger persistence.
148
private int calculateNumberOfCallbacks()
150
long max = Runtime.getRuntime().maxMemory();
151
int targetMem = (int) (max * 0.1);
152
int num = targetMem / 102400;
156
private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable
160
List callbacks = null;
162
callbacks = remotingClient.getCallbacks(pullCallbackHandler);
163
while(callbacks.size() > 0)
165
System.out.println("callbacks.size() = " + callbacks.size());
166
counter = counter + callbacks.size();
167
System.out.println("callback counter = " + counter);
168
for(int i = 0; i < callbacks.size(); i++)
170
Object o = ((Callback) callbacks.get(i)).getCallbackObject();
171
TestCallback cb = (TestCallback)o;
172
cbNum = cb.getCallbackNumber();
173
System.out.println("Callback number: " + cbNum);
175
assertEquals(counter, cbNum);
177
// need to give time for server to clean up mem
178
Thread.currentThread().sleep(2000);
179
callbacks = remotingClient.getCallbacks(pullCallbackHandler);
184
private boolean checkForCallback() throws Throwable
186
boolean isComplete = false;
188
int waitPeriod = 1000;
189
// int numOfWaits = 360;
191
for(int x = 0; x < numOfWaits; x++)
193
//isComplete = pushCallbackHandler.isComplete();
194
isComplete = ((Boolean) remotingClient.invoke("getdone")).booleanValue();
199
Thread.currentThread().sleep(waitPeriod);
201
catch(InterruptedException e)
217
public class PushCallbackHandler extends CallbackHandler
222
public class CallbackHandler implements InvokerCallbackHandler
224
boolean isComplete = false;
227
* Will take the callback message and send back to client.
228
* If client locator is null, will store them till client polls to get them.
231
* @throws org.jboss.remoting.callback.HandleCallbackException
234
public void handleCallback(Callback callback) throws HandleCallbackException
236
System.out.println("Received callback value of: " + callback.getCallbackObject());
237
System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
238
System.out.println("Received callback server invoker of: " + callback.getServerLocator());
242
public boolean isComplete()