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