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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/callback/pull/memory/callbackstore/JBossASCallbackTestClient.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
 
/*
2
 
* JBoss, Home of Professional Open Source
3
 
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
 
* by the @authors tag. See the copyright.txt in the distribution for a
5
 
* full listing of individual contributors.
6
 
*
7
 
* This is free software; you can redistribute it and/or modify it
8
 
* under the terms of the GNU Lesser General Public License as
9
 
* published by the Free Software Foundation; either version 2.1 of
10
 
* the License, or (at your option) any later version.
11
 
*
12
 
* This software is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 
* Lesser General Public License for more details.
16
 
*
17
 
* You should have received a copy of the GNU Lesser General Public
18
 
* License along with this software; if not, write to the Free
19
 
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
 
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21
 
*/
22
 
 
23
 
package org.jboss.test.remoting.callback.pull.memory.callbackstore;
24
 
 
25
 
import org.jboss.remoting.Client;
26
 
import org.jboss.remoting.InvokerLocator;
27
 
import org.jboss.remoting.callback.Callback;
28
 
import org.jboss.remoting.callback.HandleCallbackException;
29
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
30
 
import org.jboss.remoting.samples.callback.CallbackServer;
31
 
import org.jboss.remoting.transport.Connector;
32
 
 
33
 
import java.util.List;
34
 
 
35
 
/**
36
 
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
37
 
 */
38
 
public class JBossASCallbackTestClient
39
 
{
40
 
   // Default locator values
41
 
   private static String transport = "socket";
42
 
   private static String host = "localhost";
43
 
   private static int port = 5412;
44
 
 
45
 
   private String locatorURI = null;
46
 
 
47
 
   private Client remotingClient;
48
 
   private CallbackHandler pullCallbackHandler;
49
 
 
50
 
   private boolean isCallbackDone = false;
51
 
 
52
 
   private int numberOfCallbacks = 520;
53
 
 
54
 
   public JBossASCallbackTestClient(String name, String locatorURI)
55
 
   {
56
 
      this.locatorURI = locatorURI;
57
 
   }
58
 
 
59
 
   public void createRemotingClient() throws Exception
60
 
   {
61
 
      InvokerLocator locator = new InvokerLocator(locatorURI);
62
 
      System.out.println("Calling remoting server with locator uri of: " + locatorURI);
63
 
 
64
 
      // This could have been new Client(locator), but want to show that subsystem param is null
65
 
      // Could have also been new Client(locator, "sample");
66
 
      remotingClient = new Client(locator);
67
 
      remotingClient.connect();
68
 
 
69
 
   }
70
 
 
71
 
   public void makeInvocation(String param) throws Throwable
72
 
   {
73
 
      Object response = remotingClient.invoke(param, null);
74
 
      System.out.println("Invocation response: " + response);
75
 
   }
76
 
 
77
 
   public void testPullCallback() throws Throwable
78
 
   {
79
 
      createRemotingClient();
80
 
      numberOfCallbacks = calculateNumberOfCallbacks();
81
 
      System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks);
82
 
      pullCallbackHandler = new CallbackHandler();
83
 
      // by passing only the callback handler, will indicate pull callbacks
84
 
      remotingClient.addListener(pullCallbackHandler);
85
 
 
86
 
      // need to tell server handler how many
87
 
      makeInvocation("" + numberOfCallbacks);
88
 
 
89
 
      // now make invocation on server, which should cause a callback to happen
90
 
      makeInvocation("Do something");
91
 
 
92
 
      boolean didItWork = checkForCallback();
93
 
 
94
 
      System.out.println("Did id work = " + didItWork);
95
 
 
96
 
      int totalCallbacks = 0;
97
 
      if(didItWork)
98
 
      {
99
 
         boolean gotExpectedException = false;
100
 
 
101
 
         // now need to go get the callbacks until none left.
102
 
         int callbacksReceived = getAllCallbacks(pullCallbackHandler);
103
 
 
104
 
         System.out.println("callbacks received = " + callbacksReceived);
105
 
         totalCallbacks = totalCallbacks + callbacksReceived;
106
 
      }
107
 
 
108
 
      System.out.println("total callbacks received: " + totalCallbacks);
109
 
      System.out.println("total callbacks expected: " + numberOfCallbacks);
110
 
 
111
 
      remotingClient.removeListener(pullCallbackHandler);
112
 
      remotingClient.disconnect();
113
 
      remotingClient = null;
114
 
   }
115
 
 
116
 
   /**
117
 
    * calculate how many 102400 byte callback messages it will take to consume 30%
118
 
    * of the vm's memory.  The CallbackInvocationHandler will take care of consuming 70%
119
 
    * so need to make sure we have enough callbacks to trigger persistence.
120
 
    */
121
 
   private int calculateNumberOfCallbacks()
122
 
   {
123
 
      long max = Runtime.getRuntime().maxMemory();
124
 
      int targetMem = (int) (max * 0.3);
125
 
      int num = targetMem / 102400;
126
 
      return num;
127
 
   }
128
 
 
129
 
   private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable
130
 
   {
131
 
      int counter = 0;
132
 
      List callbacks = null;
133
 
 
134
 
      callbacks = remotingClient.getCallbacks(pullCallbackHandler);
135
 
      while(callbacks.size() > 0)
136
 
      {
137
 
         System.out.println("callbacks.size() = " + callbacks.size());
138
 
         counter = counter + callbacks.size();
139
 
         for(int i = 0; i < callbacks.size(); i++)
140
 
         {
141
 
            ((Callback) callbacks.get(i)).getCallbackObject();
142
 
         }
143
 
 
144
 
         // need to give time for server to clean up mem
145
 
         Thread.currentThread().sleep(2000);
146
 
         callbacks = remotingClient.getCallbacks(pullCallbackHandler);
147
 
      }
148
 
      return counter;
149
 
   }
150
 
 
151
 
   private boolean checkForCallback() throws Throwable
152
 
   {
153
 
      boolean isComplete = false;
154
 
 
155
 
      int waitPeriod = 5000;
156
 
      while(true)
157
 
      {
158
 
         //isComplete = pushCallbackHandler.isComplete();
159
 
         isComplete = ((Boolean) remotingClient.invoke("getdone")).booleanValue();
160
 
         if(!isComplete)
161
 
         {
162
 
            try
163
 
            {
164
 
               Thread.currentThread().sleep(waitPeriod);
165
 
            }
166
 
            catch(InterruptedException e)
167
 
            {
168
 
               e.printStackTrace();
169
 
            }
170
 
         }
171
 
         else
172
 
         {
173
 
            break;
174
 
         }
175
 
      }
176
 
      return isComplete;
177
 
   }
178
 
 
179
 
   public void setupServer(InvokerLocator locator) throws Exception
180
 
   {
181
 
      System.out.println("Starting remoting server with locator uri of: " + locator);
182
 
      Connector connector = new Connector();
183
 
      connector.setInvokerLocator(locator.getLocatorURI());
184
 
      connector.start();
185
 
 
186
 
      CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler();
187
 
      // first parameter is sub-system name.  can be any String value.
188
 
      connector.addInvocationHandler("sample", invocationHandler);
189
 
   }
190
 
 
191
 
 
192
 
   /**
193
 
    * Can pass transport and port to be used as parameters.
194
 
    * Valid transports are 'rmi' and 'socket'.
195
 
    *
196
 
    * @param args
197
 
    */
198
 
   public static void main(String[] args)
199
 
   {
200
 
      if(args != null && args.length == 2)
201
 
      {
202
 
         transport = args[0];
203
 
         port = Integer.parseInt(args[1]);
204
 
      }
205
 
      String locatorURI = transport + "://" + host + ":" + port;
206
 
      JBossASCallbackTestClient client = new JBossASCallbackTestClient(JBossASCallbackTestClient.class.getName(), locatorURI);
207
 
      try
208
 
      {
209
 
         client.testPullCallback();
210
 
      }
211
 
      catch(Throwable e)
212
 
      {
213
 
         e.printStackTrace();
214
 
         System.exit(1);
215
 
      }
216
 
      System.exit(0);
217
 
   }
218
 
 
219
 
   public class PushCallbackHandler extends CallbackHandler
220
 
   {
221
 
 
222
 
   }
223
 
 
224
 
   public class CallbackHandler implements InvokerCallbackHandler
225
 
   {
226
 
      boolean isComplete = false;
227
 
 
228
 
      /**
229
 
       * Will take the callback message and send back to client.
230
 
       * If client locator is null, will store them till client polls to get them.
231
 
       *
232
 
       * @param callback
233
 
       * @throws org.jboss.remoting.callback.HandleCallbackException
234
 
       *
235
 
       */
236
 
      public void handleCallback(Callback callback) throws HandleCallbackException
237
 
      {
238
 
         System.out.println("Received callback value of: " + callback.getCallbackObject());
239
 
         System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
240
 
         System.out.println("Received callback server invoker of: " + callback.getServerLocator());
241
 
         isComplete = true;
242
 
      }
243
 
 
244
 
      public boolean isComplete()
245
 
      {
246
 
         return isComplete;
247
 
      }
248
 
   }
249
 
 
250
 
}
 
 
b'\\ No newline at end of file'