1
package org.jboss.test.remoting.callback.pull.memory.blocking;
3
import org.jboss.logging.Logger;
4
import org.jboss.remoting.InvocationRequest;
5
import org.jboss.remoting.ServerInvocationHandler;
6
import org.jboss.remoting.ServerInvoker;
7
import org.jboss.remoting.callback.Callback;
8
import org.jboss.remoting.callback.HandleCallbackException;
9
import org.jboss.remoting.callback.InvokerCallbackHandler;
10
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
11
import org.jboss.test.remoting.callback.pull.memory.TestCallback;
13
import javax.management.MBeanServer;
14
import java.util.ArrayList;
15
import java.util.Iterator;
16
import java.util.List;
19
* @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
21
public class CallbackInvocationHandler implements ServerInvocationHandler, Runnable
23
private transient List pullListeners = new ArrayList();
24
private transient List pushListeners = new ArrayList();
26
private int numberOfCallbacks = 500;
28
private boolean isDone = false;
30
private int callbackCounter = 0;
32
private byte[] memHolder = null;
34
private static final Logger log = Logger.getLogger(CallbackInvocationHandler.class);
36
public CallbackInvocationHandler()
38
long max = Runtime.getRuntime().maxMemory();
39
System.out.println("max mem: " + max);
40
log.info("max mem: " + max);
41
int memSize = (int) (max * 0.9);
42
System.out.println("90% of max: " + memSize);
43
log.info("90% of max: " + memSize);
44
long free = Runtime.getRuntime().freeMemory();
45
System.out.println("free mem: " + free);
46
log.info("free mem: " + free);
47
long total = Runtime.getRuntime().totalMemory();
48
log.info("total mem: " + total);
51
memHolder = new byte[memSize];
53
else if(free > memSize)
55
memHolder = new byte[memSize];
60
* called to handle a specific invocation
66
public Object invoke(InvocationRequest invocation) throws Throwable
68
System.out.println("invoke() called on server with param: " + invocation.getParameter());
69
log.info("invoke() called on server with param: " + invocation.getParameter());
71
if("getdone".equalsIgnoreCase((String) invocation.getParameter()))
75
return new Boolean(true);
79
return new Boolean(false);
85
numberOfCallbacks = Integer.parseInt((String) invocation.getParameter());
86
System.out.println("Number of callbacks as defined by client = " + numberOfCallbacks);
87
log.info("Number of callbacks as defined by client = " + numberOfCallbacks);
89
catch(NumberFormatException e)
91
System.out.println("Starting callbacks on server.");
92
log.info("Starting callbacks on server.");
93
new Thread(this).start();
94
return "Starting callback";
101
* When an object implementing interface <code>Runnable</code> is used to create a thread, starting the thread causes
102
* the object's <code>run</code> method to be called in that separately executing thread.
104
* The general contract of the method <code>run</code> is that it may take any action whatsoever.
113
System.out.println("Sending " + numberOfCallbacks + " callbacks.");
114
log.info("Sending " + numberOfCallbacks + " callbacks.");
116
synchronized(pullListeners)
118
for(int x = 0; x < numberOfCallbacks; x++)
122
System.out.println("Number of callbacks generated = " + x);
123
log.info("Number of callbacks generated = " + x);
124
// System.out.println("Free mem = " + Runtime.getRuntime().freeMemory());
125
// log.info("Free mem = " + Runtime.getRuntime().freeMemory());
128
// System.out.println("Mem is low, so will be sleeping (slowing test down).");
129
// log.info("Mem is low, so will be sleeping (slowing test down).");
132
// Will also fire callback to listeners if they were to exist using
133
// simple invocation request.
134
synchronized(pullListeners)
136
Iterator itr = pullListeners.iterator();
139
InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
142
callbackHandler.handleCallback(new Callback(getCallbackMessage()));
145
Thread.currentThread().sleep(1000);
148
catch(HandleCallbackException e)
155
// done adding callbacks, now release memory
161
synchronized(pushListeners)
163
Iterator itr = pushListeners.iterator();
166
InvokerCallbackHandler handler = (InvokerCallbackHandler) itr.next();
169
handler.handleCallback(new Callback("Done"));
171
catch(HandleCallbackException e)
184
for(int x = 0; x < 10; x++)
186
synchronized(pullListeners)
188
Iterator itr = pullListeners.iterator();
191
InvokerCallbackHandler handler = (InvokerCallbackHandler) itr.next();
194
handler.handleCallback(new Callback("Done"));
196
catch(HandleCallbackException e)
211
private boolean isMemLow()
213
Runtime runtime = Runtime.getRuntime();
214
long max = runtime.maxMemory();
215
long total = runtime.totalMemory();
216
long free = runtime.freeMemory();
217
float percentage = 100 * free / total;
218
if(max == total && 40 >= percentage)
230
* Adds a callback handler that will listen for callbacks from the server invoker handler.
232
* @param callbackHandler
234
public void addListener(InvokerCallbackHandler callbackHandler)
236
ServerInvokerCallbackHandler sih = (ServerInvokerCallbackHandler) callbackHandler;
238
if(!sih.isPullCallbackHandler())
240
pushListeners.add(callbackHandler);
244
pullListeners.add(callbackHandler);
249
* Removes the callback handler that was listening for callbacks from the server invoker handler.
251
* @param callbackHandler
253
public void removeListener(InvokerCallbackHandler callbackHandler)
255
pullListeners.remove(callbackHandler);
256
pushListeners.remove(callbackHandler);
260
* set the mbean server that the handler can reference
264
public void setMBeanServer(MBeanServer server)
266
// NO OP as do not need reference to MBeanServer for this handler
270
* set the invoker that owns this handler
274
public void setInvoker(ServerInvoker invoker)
276
// NO OP as do not need reference back to the server invoker
279
private Object getCallbackMessage()
282
//byte[] bytes = new byte[5120000];
283
byte[] bytes = new byte[102400];
284
TestCallback callback = new TestCallback(bytes, callbackCounter);