~ubuntu-branches/ubuntu/wily/libjboss-remoting-java/wily

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/callback/pull/memory/blocking/CallbackInvocationHandler.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.jboss.test.remoting.callback.pull.memory.blocking;
2
 
 
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;
12
 
 
13
 
import javax.management.MBeanServer;
14
 
import java.util.ArrayList;
15
 
import java.util.Iterator;
16
 
import java.util.List;
17
 
 
18
 
/**
19
 
 * @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
20
 
 */
21
 
public class CallbackInvocationHandler implements ServerInvocationHandler, Runnable
22
 
{
23
 
   private transient List pullListeners = new ArrayList();
24
 
   private transient List pushListeners = new ArrayList();
25
 
 
26
 
   private int numberOfCallbacks = 500;
27
 
 
28
 
   private boolean isDone = false;
29
 
 
30
 
   private int callbackCounter = 0;
31
 
 
32
 
   private byte[] memHolder = null;
33
 
 
34
 
   private static final Logger log = Logger.getLogger(CallbackInvocationHandler.class);
35
 
 
36
 
   public CallbackInvocationHandler()
37
 
   {
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);
49
 
      if(total != max)
50
 
      {
51
 
         memHolder = new byte[memSize];
52
 
      }
53
 
      else if(free > memSize)
54
 
      {
55
 
         memHolder = new byte[memSize];
56
 
      }
57
 
   }
58
 
 
59
 
   /**
60
 
    * called to handle a specific invocation
61
 
    *
62
 
    * @param invocation
63
 
    * @return
64
 
    * @throws Throwable
65
 
    */
66
 
   public Object invoke(InvocationRequest invocation) throws Throwable
67
 
   {
68
 
      System.out.println("invoke() called on server with param: " + invocation.getParameter());
69
 
      log.info("invoke() called on server with param: " + invocation.getParameter());
70
 
 
71
 
      if("getdone".equalsIgnoreCase((String) invocation.getParameter()))
72
 
      {
73
 
         if(isDone)
74
 
         {
75
 
            return new Boolean(true);
76
 
         }
77
 
         else
78
 
         {
79
 
            return new Boolean(false);
80
 
         }
81
 
      }
82
 
 
83
 
      try
84
 
      {
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);
88
 
      }
89
 
      catch(NumberFormatException e)
90
 
      {
91
 
         System.out.println("Starting callbacks on server.");
92
 
         log.info("Starting callbacks on server.");
93
 
         new Thread(this).start();
94
 
         return "Starting callback";
95
 
      }
96
 
 
97
 
      return null;
98
 
   }
99
 
 
100
 
   /**
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.
103
 
    * <p/>
104
 
    * The general contract of the method <code>run</code> is that it may take any action whatsoever.
105
 
    *
106
 
    * @see Thread#run()
107
 
    */
108
 
   public void run()
109
 
   {
110
 
 
111
 
      try
112
 
      {
113
 
         System.out.println("Sending " + numberOfCallbacks + " callbacks.");
114
 
         log.info("Sending " + numberOfCallbacks + " callbacks.");
115
 
 
116
 
         synchronized(pullListeners)
117
 
         {
118
 
            for(int x = 0; x < numberOfCallbacks; x++)
119
 
            {
120
 
//               if(x % 10 == 0)
121
 
//               {
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());
126
 
//                  if(isMemLow())
127
 
//                  {
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).");
130
 
//                  }
131
 
//               }
132
 
               // Will also fire callback to listeners if they were to exist using
133
 
               // simple invocation request.
134
 
               synchronized(pullListeners)
135
 
               {
136
 
                  Iterator itr = pullListeners.iterator();
137
 
                  while(itr.hasNext())
138
 
                  {
139
 
                     InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
140
 
                     try
141
 
                     {
142
 
                        callbackHandler.handleCallback(new Callback(getCallbackMessage()));
143
 
                        if(isMemLow())
144
 
                        {
145
 
                           Thread.currentThread().sleep(1000);
146
 
                        }
147
 
                     }
148
 
                     catch(HandleCallbackException e)
149
 
                     {
150
 
                        e.printStackTrace();
151
 
                     }
152
 
                  }
153
 
               }
154
 
            }
155
 
            // done adding callbacks, now release memory
156
 
            memHolder = null;
157
 
         }
158
 
 
159
 
         isDone = true;
160
 
 
161
 
         synchronized(pushListeners)
162
 
         {
163
 
            Iterator itr = pushListeners.iterator();
164
 
            while(itr.hasNext())
165
 
            {
166
 
               InvokerCallbackHandler handler = (InvokerCallbackHandler) itr.next();
167
 
               try
168
 
               {
169
 
                  handler.handleCallback(new Callback("Done"));
170
 
               }
171
 
               catch(HandleCallbackException e)
172
 
               {
173
 
                  e.printStackTrace();
174
 
               }
175
 
            }
176
 
         }
177
 
 
178
 
         Thread.sleep(5000);
179
 
 
180
 
         System.gc();
181
 
 
182
 
         Thread.sleep(5000);
183
 
 
184
 
         for(int x = 0; x < 10; x++)
185
 
         {
186
 
         synchronized(pullListeners)
187
 
         {
188
 
            Iterator itr = pullListeners.iterator();
189
 
            while(itr.hasNext())
190
 
            {
191
 
               InvokerCallbackHandler handler = (InvokerCallbackHandler) itr.next();
192
 
               try
193
 
               {
194
 
                  handler.handleCallback(new Callback("Done"));
195
 
               }
196
 
               catch(HandleCallbackException e)
197
 
               {
198
 
                  e.printStackTrace();
199
 
               }
200
 
            }
201
 
         }
202
 
         }
203
 
      }
204
 
      catch(Throwable e)
205
 
      {
206
 
         e.printStackTrace();
207
 
      }
208
 
 
209
 
   }
210
 
 
211
 
   private boolean isMemLow()
212
 
   {
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)
219
 
      {
220
 
         return true;
221
 
      }
222
 
      else
223
 
      {
224
 
         return false;
225
 
      }
226
 
   }
227
 
 
228
 
 
229
 
   /**
230
 
    * Adds a callback handler that will listen for callbacks from the server invoker handler.
231
 
    *
232
 
    * @param callbackHandler
233
 
    */
234
 
   public void addListener(InvokerCallbackHandler callbackHandler)
235
 
   {
236
 
      ServerInvokerCallbackHandler sih = (ServerInvokerCallbackHandler) callbackHandler;
237
 
 
238
 
      if(!sih.isPullCallbackHandler())
239
 
      {
240
 
         pushListeners.add(callbackHandler);
241
 
      }
242
 
      else
243
 
      {
244
 
         pullListeners.add(callbackHandler);
245
 
      }
246
 
   }
247
 
 
248
 
   /**
249
 
    * Removes the callback handler that was listening for callbacks from the server invoker handler.
250
 
    *
251
 
    * @param callbackHandler
252
 
    */
253
 
   public void removeListener(InvokerCallbackHandler callbackHandler)
254
 
   {
255
 
      pullListeners.remove(callbackHandler);
256
 
      pushListeners.remove(callbackHandler);
257
 
   }
258
 
 
259
 
   /**
260
 
    * set the mbean server that the handler can reference
261
 
    *
262
 
    * @param server
263
 
    */
264
 
   public void setMBeanServer(MBeanServer server)
265
 
   {
266
 
      // NO OP as do not need reference to MBeanServer for this handler
267
 
   }
268
 
 
269
 
   /**
270
 
    * set the invoker that owns this handler
271
 
    *
272
 
    * @param invoker
273
 
    */
274
 
   public void setInvoker(ServerInvoker invoker)
275
 
   {
276
 
      // NO OP as do not need reference back to the server invoker
277
 
   }
278
 
 
279
 
   private Object getCallbackMessage()
280
 
   {
281
 
      callbackCounter++;
282
 
      //byte[] bytes = new byte[5120000];
283
 
      byte[] bytes = new byte[102400];
284
 
      TestCallback callback = new TestCallback(bytes, callbackCounter);
285
 
      return callback;
286
 
   }
287
 
 
288
 
 
289
 
}