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.nullstore;
25
import org.jboss.remoting.Client;
26
import org.jboss.remoting.InvokerLocator;
27
import org.jboss.remoting.callback.Callback;
28
import org.jboss.remoting.callback.HandleCallbackException;
29
import org.jboss.remoting.callback.InvokerCallbackHandler;
30
import org.jboss.remoting.samples.callback.CallbackServer;
31
import org.jboss.remoting.transport.Connector;
33
import java.util.List;
36
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
38
public class JBossASCallbackTestClient
40
// Default locator values
41
private static String transport = "socket";
42
private static String host = "localhost";
43
private static int port = 5411;
45
private String locatorURI = null;
47
private Client remotingClient;
48
private CallbackHandler pullCallbackHandler;
50
private boolean isCallbackDone = false;
52
private int numberOfCallbacks = 520;
54
public JBossASCallbackTestClient(String name, String locatorURI)
56
this.locatorURI = locatorURI;
59
public void createRemotingClient() throws Exception
61
InvokerLocator locator = new InvokerLocator(locatorURI);
62
System.out.println("Calling remoting server with locator uri of: " + locatorURI);
64
// This could have been new Client(locator), but want to show that subsystem param is null
65
// Could have also been new Client(locator, "sample");
66
remotingClient = new Client(locator);
67
remotingClient.connect();
71
public void makeInvocation(String param) throws Throwable
73
Object response = remotingClient.invoke(param, null);
74
System.out.println("Invocation response: " + response);
77
public void testPullCallback() throws Throwable
79
createRemotingClient();
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);
86
// need to tell server handler how many
87
makeInvocation("" + numberOfCallbacks);
89
// now make invocation on server, which should cause a callback to happen
90
makeInvocation("Do something");
92
boolean didItWork = checkForCallback();
94
System.out.println("Did id work = " + didItWork);
96
int totalCallbacks = 0;
99
boolean gotExpectedException = false;
103
// now need to go get the callbacks until none left.
104
int callbacksReceived = getAllCallbacks(pullCallbackHandler);
106
System.out.println("callbacks received = " + callbacksReceived);
107
totalCallbacks = totalCallbacks + callbacksReceived;
110
catch(RuntimeException re)
112
System.out.println("Got exception as expected - " + re);
113
gotExpectedException = true;
117
System.out.println("Got unexpected exception - " + thr);
120
if(gotExpectedException)
122
System.out.println("PASSED");
126
System.out.println("FAILED");
130
System.out.println("total callbacks received: " + totalCallbacks);
131
System.out.println("total callbacks expected: " + numberOfCallbacks);
133
remotingClient.removeListener(pullCallbackHandler);
134
remotingClient.disconnect();
135
remotingClient = null;
139
* calculate how many 102400 byte callback messages it will take to consume 30%
140
* of the vm's memory. The CallbackInvocationHandler will take care of consuming 70%
141
* so need to make sure we have enough callbacks to trigger persistence.
143
private int calculateNumberOfCallbacks()
145
long max = Runtime.getRuntime().maxMemory();
146
int targetMem = (int) (max * 0.3);
147
int num = targetMem / 102400;
151
private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable
154
List callbacks = null;
156
callbacks = remotingClient.getCallbacks(pullCallbackHandler);
157
while(callbacks.size() > 0)
159
System.out.println("callbacks.size() = " + callbacks.size());
160
counter = counter + callbacks.size();
161
for(int i = 0; i < callbacks.size(); i++)
163
((Callback) callbacks.get(i)).getCallbackObject();
166
// need to give time for server to clean up mem
167
Thread.currentThread().sleep(2000);
168
callbacks = remotingClient.getCallbacks(pullCallbackHandler);
173
private boolean checkForCallback() throws Throwable
175
boolean isComplete = false;
177
int waitPeriod = 1000;
178
int numOfWaits = 360; // means will wait 30 seconds
179
for(int x = 0; x < numOfWaits; x++)
181
//isComplete = pushCallbackHandler.isComplete();
182
isComplete = ((Boolean) remotingClient.invoke("getdone")).booleanValue();
187
Thread.currentThread().sleep(waitPeriod);
189
catch(InterruptedException e)
202
public void setupServer(InvokerLocator locator) throws Exception
204
System.out.println("Starting remoting server with locator uri of: " + locator);
205
Connector connector = new Connector();
206
connector.setInvokerLocator(locator.getLocatorURI());
209
CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler();
210
// first parameter is sub-system name. can be any String value.
211
connector.addInvocationHandler("sample", invocationHandler);
216
* Can pass transport and port to be used as parameters.
217
* Valid transports are 'rmi' and 'socket'.
221
public static void main(String[] args)
223
if(args != null && args.length == 2)
226
port = Integer.parseInt(args[1]);
228
String locatorURI = transport + "://" + host + ":" + port;
229
JBossASCallbackTestClient client = new JBossASCallbackTestClient(JBossASCallbackTestClient.class.getName(), locatorURI);
232
client.testPullCallback();
242
public class PushCallbackHandler extends CallbackHandler
247
public class CallbackHandler implements InvokerCallbackHandler
249
boolean isComplete = false;
252
* Will take the callback message and send back to client.
253
* If client locator is null, will store them till client polls to get them.
256
* @throws org.jboss.remoting.callback.HandleCallbackException
259
public void handleCallback(Callback callback) throws HandleCallbackException
261
System.out.println("Received callback value of: " + callback.getCallbackObject());
262
System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
263
System.out.println("Received callback server invoker of: " + callback.getServerLocator());
267
public boolean isComplete()
b'\\ No newline at end of file'