2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.callback.pull.memory.callbackstore;
25
import junit.framework.TestCase;
26
import org.jboss.logging.Logger;
27
import org.jboss.remoting.Client;
28
import org.jboss.remoting.InvokerLocator;
29
import org.jboss.remoting.callback.Callback;
30
import org.jboss.remoting.callback.HandleCallbackException;
31
import org.jboss.remoting.callback.InvokerCallbackHandler;
33
import java.util.List;
36
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
38
public class CallbackTestClient extends TestCase
40
// Default locator values
41
private static String transport = "socket";
42
private static String host = "localhost";
43
private static int port = 5412;
45
private String locatorURI = transport + "://" + host + ":" + port;
47
private Client remotingClient;
48
private CallbackHandler pullCallbackHandler;
50
private boolean isCallbackDone = false;
52
private int numberOfCallbacks = 520;
54
private static final Logger log = Logger.getLogger(CallbackTestClient.class);
56
public void createRemotingClient() throws Exception
58
InvokerLocator locator = new InvokerLocator(getInvokerLocatorURI());
59
System.out.println("Calling remoting server with locator uri of: " + getInvokerLocatorURI());
61
// This could have been new Client(locator), but want to show that subsystem param is null
62
// Could have also been new Client(locator, "sample");
63
remotingClient = new Client(locator);
64
remotingClient.connect();
68
protected String getInvokerLocatorURI()
73
public void makeInvocation(String param) throws Throwable
75
Object response = remotingClient.invoke(param, null);
76
System.out.println("Invocation response: " + response);
79
public void setUp() throws Exception
81
createRemotingClient();
84
public void tearDown() throws Exception
86
if(remotingClient != null)
88
if(pullCallbackHandler != null)
92
remotingClient.removeListener(pullCallbackHandler);
94
catch(Throwable throwable)
96
throw new Exception(throwable);
99
remotingClient.disconnect();
103
public void testPullCallback() throws Throwable
105
numberOfCallbacks = calculateNumberOfCallbacks();
106
System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks);
107
pullCallbackHandler = new CallbackHandler();
108
// by passing only the callback handler, will indicate pull callbacks
109
remotingClient.addListener(pullCallbackHandler);
111
// need to tell server handler how many
112
makeInvocation("" + numberOfCallbacks);
114
// now make invocation on server, which should cause a callback to happen
115
makeInvocation("Do something");
117
boolean didItWork = checkForCallback();
119
System.out.println("Did it work = " + didItWork);
120
log.debug("Did it work = " + didItWork);
122
int totalCallbacks = 0;
125
// now need to go get the callbacks until none left.
126
int callbacksReceived = getAllCallbacks(pullCallbackHandler);
128
System.out.println("callbacks received = " + callbacksReceived);
129
log.debug("callbacks received = " + callbacksReceived);
130
totalCallbacks = totalCallbacks + callbacksReceived;
133
System.out.println("total callbacks received: " + totalCallbacks);
134
log.debug("total callbacks received: " + totalCallbacks);
135
System.out.println("total callbacks expected: " + numberOfCallbacks);
136
log.debug("total callbacks expected: " + numberOfCallbacks);
138
assertEquals(numberOfCallbacks, totalCallbacks);
142
* calculate how many 102400 byte callback messages it will take to consume 30%
143
* of the vm's memory. The CallbackInvocationHandler will take care of consuming 70%
144
* so need to make sure we have enough callbacks to trigger persistence.
146
private int calculateNumberOfCallbacks()
148
long max = Runtime.getRuntime().maxMemory();
149
int targetMem = (int) (max * 0.1);
150
int num = targetMem / 102400;
154
private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable
157
List callbacks = null;
159
callbacks = remotingClient.getCallbacks(pullCallbackHandler);
160
while(callbacks.size() > 0)
162
System.out.println("callbacks.size() = " + callbacks.size());
163
counter = counter + callbacks.size();
164
for(int i = 0; i < callbacks.size(); i++)
166
((Callback) callbacks.get(i)).getCallbackObject();
169
// need to give time for server to clean up mem
170
Thread.currentThread().sleep(2000);
171
callbacks = remotingClient.getCallbacks(pullCallbackHandler);
176
private boolean checkForCallback() throws Throwable
178
boolean isComplete = false;
180
int waitPeriod = 1000;
181
int numOfWaits = 600000;
182
for(int x = 0; x < numOfWaits; x++)
184
//isComplete = pushCallbackHandler.isComplete();
185
isComplete = ((Boolean) remotingClient.invoke("getdone")).booleanValue();
190
Thread.currentThread().sleep(waitPeriod);
192
catch(InterruptedException e)
205
public static void main(String[] args)
207
CallbackTestClient client = new CallbackTestClient();
211
client.testPullCallback();
214
catch(Throwable throwable)
216
throwable.printStackTrace();
221
public class PushCallbackHandler extends CallbackHandler
226
public class CallbackHandler implements InvokerCallbackHandler
228
boolean isComplete = false;
231
* Will take the callback message and send back to client.
232
* If client locator is null, will store them till client polls to get them.
235
* @throws org.jboss.remoting.callback.HandleCallbackException
238
public void handleCallback(Callback callback) throws HandleCallbackException
240
System.out.println("Received callback value of: " + callback.getCallbackObject());
241
System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
242
System.out.println("Received callback server invoker of: " + callback.getServerLocator());
246
public boolean isComplete()
b'\\ No newline at end of file'