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 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 = 5412;
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;
101
// now need to go get the callbacks until none left.
102
int callbacksReceived = getAllCallbacks(pullCallbackHandler);
104
System.out.println("callbacks received = " + callbacksReceived);
105
totalCallbacks = totalCallbacks + callbacksReceived;
108
System.out.println("total callbacks received: " + totalCallbacks);
109
System.out.println("total callbacks expected: " + numberOfCallbacks);
111
remotingClient.removeListener(pullCallbackHandler);
112
remotingClient.disconnect();
113
remotingClient = null;
117
* calculate how many 102400 byte callback messages it will take to consume 30%
118
* of the vm's memory. The CallbackInvocationHandler will take care of consuming 70%
119
* so need to make sure we have enough callbacks to trigger persistence.
121
private int calculateNumberOfCallbacks()
123
long max = Runtime.getRuntime().maxMemory();
124
int targetMem = (int) (max * 0.3);
125
int num = targetMem / 102400;
129
private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable
132
List callbacks = null;
134
callbacks = remotingClient.getCallbacks(pullCallbackHandler);
135
while(callbacks.size() > 0)
137
System.out.println("callbacks.size() = " + callbacks.size());
138
counter = counter + callbacks.size();
139
for(int i = 0; i < callbacks.size(); i++)
141
((Callback) callbacks.get(i)).getCallbackObject();
144
// need to give time for server to clean up mem
145
Thread.currentThread().sleep(2000);
146
callbacks = remotingClient.getCallbacks(pullCallbackHandler);
151
private boolean checkForCallback() throws Throwable
153
boolean isComplete = false;
155
int waitPeriod = 5000;
158
//isComplete = pushCallbackHandler.isComplete();
159
isComplete = ((Boolean) remotingClient.invoke("getdone")).booleanValue();
164
Thread.currentThread().sleep(waitPeriod);
166
catch(InterruptedException e)
179
public void setupServer(InvokerLocator locator) throws Exception
181
System.out.println("Starting remoting server with locator uri of: " + locator);
182
Connector connector = new Connector();
183
connector.setInvokerLocator(locator.getLocatorURI());
186
CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler();
187
// first parameter is sub-system name. can be any String value.
188
connector.addInvocationHandler("sample", invocationHandler);
193
* Can pass transport and port to be used as parameters.
194
* Valid transports are 'rmi' and 'socket'.
198
public static void main(String[] args)
200
if(args != null && args.length == 2)
203
port = Integer.parseInt(args[1]);
205
String locatorURI = transport + "://" + host + ":" + port;
206
JBossASCallbackTestClient client = new JBossASCallbackTestClient(JBossASCallbackTestClient.class.getName(), locatorURI);
209
client.testPullCallback();
219
public class PushCallbackHandler extends CallbackHandler
224
public class CallbackHandler implements InvokerCallbackHandler
226
boolean isComplete = false;
229
* Will take the callback message and send back to client.
230
* If client locator is null, will store them till client polls to get them.
233
* @throws org.jboss.remoting.callback.HandleCallbackException
236
public void handleCallback(Callback callback) throws HandleCallbackException
238
System.out.println("Received callback value of: " + callback.getCallbackObject());
239
System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
240
System.out.println("Received callback server invoker of: " + callback.getServerLocator());
244
public boolean isComplete()
b'\\ No newline at end of file'