KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > callback > pull > memory > CallbackInvocationHandler


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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 JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
24  */

25 public class CallbackInvocationHandler implements ServerInvocationHandler, Runnable JavaDoc
26 {
27    private transient List JavaDoc pullListeners = new ArrayList JavaDoc();
28    private transient List JavaDoc pushListeners = new ArrayList JavaDoc();
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    /**
59     * called to handle a specific invocation
60     *
61     * @param invocation
62     * @return
63     * @throws Throwable
64     */

65    public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
66    {
67       if("getdone".equalsIgnoreCase((String JavaDoc) invocation.getParameter()))
68       {
69          return new Boolean JavaDoc(isDone);
70       }
71
72       try
73       {
74          numberOfCallbacks = Integer.parseInt((String JavaDoc) invocation.getParameter());
75       }
76       catch(NumberFormatException JavaDoc e)
77       {
78          new Thread JavaDoc(this).start();
79          return "Starting callback";
80       }
81
82       return null;
83    }
84
85    /**
86     * When an object implementing interface <code>Runnable</code> is used to create a thread, starting the thread causes
87     * the object's <code>run</code> method to be called in that separately executing thread.
88     * <p/>
89     * The general contract of the method <code>run</code> is that it may take any action whatsoever.
90     *
91     * @see Thread#run()
92     */

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                // Will also fire callback to listeners if they were to exist using
108
// simple invocation request.
109
synchronized(pullListeners)
110                {
111                   Iterator JavaDoc 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             // done adding callbacks, now release memory
131
memHolder = null;
132          }
133
134          isDone = true;
135
136          synchronized(pushListeners)
137          {
138             Iterator JavaDoc 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 JavaDoc e)
154       {
155          e.printStackTrace();
156       }
157
158    }
159
160    private boolean isMemLow()
161    {
162       Runtime JavaDoc 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    /**
179     * Adds a callback handler that will listen for callbacks from the server invoker handler.
180     *
181     * @param callbackHandler
182     */

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    /**
198     * Removes the callback handler that was listening for callbacks from the server invoker handler.
199     *
200     * @param callbackHandler
201     */

202    public void removeListener(InvokerCallbackHandler callbackHandler)
203    {
204       pullListeners.remove(callbackHandler);
205       pushListeners.remove(callbackHandler);
206    }
207
208    /**
209     * set the mbean server that the handler can reference
210     *
211     * @param server
212     */

213    public void setMBeanServer(MBeanServer JavaDoc server)
214    {
215       // NO OP as do not need reference to MBeanServer for this handler
216
}
217
218    /**
219     * set the invoker that owns this handler
220     *
221     * @param invoker
222     */

223    public void setInvoker(ServerInvoker invoker)
224    {
225       // NO OP as do not need reference back to the server invoker
226
}
227
228    private Object JavaDoc getCallbackMessage()
229    {
230       callbackCounter++;
231       //byte[] bytes = new byte[5120000];
232
byte[] bytes = new byte[102400];
233       TestCallback callback = new TestCallback(bytes, callbackCounter);
234       return callback;
235    }
236
237
238 }
Popular Tags