KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > 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.test.remoting.callback.pull.memory;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import javax.management.MBeanServer JavaDoc;
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 /**
22  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
23  */

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

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

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                // Will also fire callback to listeners if they were to exist using
114
// simple invocation request.
115
synchronized(pullListeners)
116                {
117                   Iterator JavaDoc 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             // done adding callbacks, now release memory
137
memHolder = null;
138          }
139
140          isDone = true;
141
142          synchronized(pushListeners)
143          {
144             Iterator JavaDoc 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 JavaDoc e)
160       {
161          e.printStackTrace();
162       }
163
164    }
165
166    private boolean isMemLow()
167    {
168       Runtime JavaDoc 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    /**
185     * Adds a callback handler that will listen for callbacks from the server invoker handler.
186     *
187     * @param callbackHandler
188     */

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    /**
204     * Removes the callback handler that was listening for callbacks from the server invoker handler.
205     *
206     * @param callbackHandler
207     */

208    public void removeListener(InvokerCallbackHandler callbackHandler)
209    {
210       pullListeners.remove(callbackHandler);
211       pushListeners.remove(callbackHandler);
212    }
213
214    /**
215     * set the mbean server that the handler can reference
216     *
217     * @param server
218     */

219    public void setMBeanServer(MBeanServer JavaDoc server)
220    {
221       // NO OP as do not need reference to MBeanServer for this handler
222
}
223
224    /**
225     * set the invoker that owns this handler
226     *
227     * @param invoker
228     */

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