1 7 package org.jboss.test.remoting.callback.pull.memory; 8 9 import java.util.ArrayList ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import javax.management.MBeanServer ; 13 import org.jboss.remoting.InvocationRequest; 14 import org.jboss.remoting.ServerInvocationHandler; 15 import org.jboss.remoting.ServerInvoker; 16 import org.jboss.remoting.callback.Callback; 17 import org.jboss.remoting.callback.HandleCallbackException; 18 import org.jboss.remoting.callback.InvokerCallbackHandler; 19 import org.jboss.remoting.callback.ServerInvokerCallbackHandler; 20 21 24 public class CallbackInvocationHandler implements ServerInvocationHandler, Runnable 25 { 26 private transient List pullListeners = new ArrayList (); 27 private transient List pushListeners = new ArrayList (); 28 29 private int numberOfCallbacks = 500; 30 31 private boolean isDone = false; 32 33 private int callbackCounter = 0; 34 35 private byte[] memHolder = null; 36 37 public CallbackInvocationHandler() 38 { 39 long max = Runtime.getRuntime().maxMemory(); 40 System.out.println("max mem: " + max); 41 int memSize = (int) (max * 0.7); 42 System.out.println("70% of max: " + memSize); 43 long free = Runtime.getRuntime().freeMemory(); 44 System.out.println("free mem: " + free); 45 long total = Runtime.getRuntime().totalMemory(); 46 System.out.println("total mem: " + total); 47 if(total != max) 48 { 49 memHolder = new byte[memSize]; 50 } 51 else if(free > memSize) 52 { 53 memHolder = new byte[memSize]; 54 } 55 } 56 57 64 public Object invoke(InvocationRequest invocation) throws Throwable 65 { 66 if("getdone".equalsIgnoreCase((String ) invocation.getParameter())) 67 { 68 if(isDone) 69 { 70 return new Boolean (true); 71 } 72 else 73 { 74 return new Boolean (false); 75 } 76 } 77 78 try 79 { 80 numberOfCallbacks = Integer.parseInt((String ) invocation.getParameter()); 81 } 82 catch(NumberFormatException e) 83 { 84 new Thread (this).start(); 85 return "Starting callback"; 86 } 87 88 return null; 89 } 90 91 99 public void run() 100 { 101 102 try 103 { 104 synchronized(pullListeners) 105 { 106 for(int x = 0; x < numberOfCallbacks; x++) 107 { 108 if(x % 10 == 0) 109 { 110 System.out.println("x = " + x); 111 System.out.println("Free mem = " + Runtime.getRuntime().freeMemory()); 112 } 113 synchronized(pullListeners) 116 { 117 Iterator itr = pullListeners.iterator(); 118 while(itr.hasNext()) 119 { 120 InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next(); 121 try 122 { 123 callbackHandler.handleCallback(new Callback(getCallbackMessage())); 124 if(isMemLow()) 125 { 126 Thread.currentThread().sleep(1000); 127 } 128 } 129 catch(HandleCallbackException e) 130 { 131 e.printStackTrace(); 132 } 133 } 134 } 135 } 136 memHolder = null; 138 } 139 140 isDone = true; 141 142 synchronized(pushListeners) 143 { 144 Iterator itr = pushListeners.iterator(); 145 while(itr.hasNext()) 146 { 147 InvokerCallbackHandler handler = (InvokerCallbackHandler) itr.next(); 148 try 149 { 150 handler.handleCallback(new Callback("Done")); 151 } 152 catch(HandleCallbackException e) 153 { 154 e.printStackTrace(); 155 } 156 } 157 } 158 } 159 catch(Throwable e) 160 { 161 e.printStackTrace(); 162 } 163 164 } 165 166 private boolean isMemLow() 167 { 168 Runtime runtime = Runtime.getRuntime(); 169 long max = runtime.maxMemory(); 170 long total = runtime.totalMemory(); 171 long free = runtime.freeMemory(); 172 float percentage = 100 * free / total; 173 if(max == total && 40 >= percentage) 174 { 175 return true; 176 } 177 else 178 { 179 return false; 180 } 181 } 182 183 184 189 public void addListener(InvokerCallbackHandler callbackHandler) 190 { 191 ServerInvokerCallbackHandler sih = (ServerInvokerCallbackHandler) callbackHandler; 192 193 if(!sih.isPullCallbackHandler()) 194 { 195 pushListeners.add(callbackHandler); 196 } 197 else 198 { 199 pullListeners.add(callbackHandler); 200 } 201 } 202 203 208 public void removeListener(InvokerCallbackHandler callbackHandler) 209 { 210 pullListeners.remove(callbackHandler); 211 pushListeners.remove(callbackHandler); 212 } 213 214 219 public void setMBeanServer(MBeanServer server) 220 { 221 } 223 224 229 public void setInvoker(ServerInvoker invoker) 230 { 231 } 233 234 private Object getCallbackMessage() 235 { 236 callbackCounter++; 237 byte[] bytes = new byte[102400]; 239 TestCallback callback = new TestCallback(bytes, callbackCounter); 240 return callback; 241 } 242 243 244 } | Popular Tags |