KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > callback > pull > memory > callbackstore > JBossASCallbackTestClient


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.callbackstore;
8
9 import java.util.List JavaDoc;
10 import org.jboss.remoting.Client;
11 import org.jboss.remoting.InvokerLocator;
12 import org.jboss.remoting.callback.Callback;
13 import org.jboss.remoting.callback.HandleCallbackException;
14 import org.jboss.remoting.callback.InvokerCallbackHandler;
15 import org.jboss.remoting.samples.callback.CallbackServer;
16 import org.jboss.remoting.transport.Connector;
17
18 /**
19  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
20  */

21 public class JBossASCallbackTestClient
22 {
23    // Default locator values
24
private static String JavaDoc transport = "socket";
25    private static String JavaDoc host = "localhost";
26    private static int port = 5412;
27
28    private String JavaDoc locatorURI = null;
29
30    private Client remotingClient;
31    private CallbackHandler pullCallbackHandler;
32
33    private boolean isCallbackDone = false;
34
35    private int numberOfCallbacks = 520;
36
37    public JBossASCallbackTestClient(String JavaDoc name, String JavaDoc locatorURI)
38    {
39       this.locatorURI = locatorURI;
40    }
41
42    public void createRemotingClient() throws Exception JavaDoc
43    {
44       InvokerLocator locator = new InvokerLocator(locatorURI);
45       System.out.println("Calling remoting server with locator uri of: " + locatorURI);
46
47       // This could have been new Client(locator), but want to show that subsystem param is null
48
// Could have also been new Client(locator, "sample");
49
remotingClient = new Client(locator, null);
50
51    }
52
53    public void makeInvocation(String JavaDoc param) throws Throwable JavaDoc
54    {
55       Object JavaDoc response = remotingClient.invoke(param, null);
56       System.out.println("Invocation response: " + response);
57    }
58
59    public void testPullCallback() throws Throwable JavaDoc
60    {
61       createRemotingClient();
62       numberOfCallbacks = calculateNumberOfCallbacks();
63       System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks);
64       pullCallbackHandler = new CallbackHandler();
65       // by passing only the callback handler, will indicate pull callbacks
66
remotingClient.addListener(pullCallbackHandler);
67
68       // need to tell server handler how many
69
makeInvocation("" + numberOfCallbacks);
70
71       // now make invocation on server, which should cause a callback to happen
72
makeInvocation("Do something");
73
74       boolean didItWork = checkForCallback();
75
76       System.out.println("Did id work = " + didItWork);
77
78       int totalCallbacks = 0;
79       if(didItWork)
80       {
81          boolean gotExpectedException = false;
82
83          // now need to go get the callbacks until none left.
84
int callbacksReceived = getAllCallbacks(pullCallbackHandler);
85
86          System.out.println("callbacks received = " + callbacksReceived);
87          totalCallbacks = totalCallbacks + callbacksReceived;
88       }
89
90       System.out.println("total callbacks received: " + totalCallbacks);
91       System.out.println("total callbacks expected: " + numberOfCallbacks);
92
93       remotingClient.removeListener(pullCallbackHandler);
94       remotingClient.disconnect();
95       remotingClient = null;
96    }
97
98    /**
99     * calculate how many 102400 byte callback messages it will take to consume 30%
100     * of the vm's memory. The CallbackInvocationHandler will take care of consuming 70%
101     * so need to make sure we have enough callbacks to trigger persistence.
102     */

103    private int calculateNumberOfCallbacks()
104    {
105       long max = Runtime.getRuntime().maxMemory();
106       int targetMem = (int) (max * 0.3);
107       int num = targetMem / 102400;
108       return num;
109    }
110
111    private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable JavaDoc
112    {
113       int counter = 0;
114       List JavaDoc callbacks = null;
115
116       callbacks = remotingClient.getCallbacks(pullCallbackHandler);
117       while(callbacks.size() > 0)
118       {
119          System.out.println("callbacks.size() = " + callbacks.size());
120          counter = counter + callbacks.size();
121          for(int i = 0; i < callbacks.size(); i++)
122          {
123             ((Callback) callbacks.get(i)).getCallbackObject();
124          }
125
126          // need to give time for server to clean up mem
127
Thread.currentThread().sleep(2000);
128          callbacks = remotingClient.getCallbacks(pullCallbackHandler);
129       }
130       return counter;
131    }
132
133    private boolean checkForCallback() throws Throwable JavaDoc
134    {
135       boolean isComplete = false;
136
137       int waitPeriod = 5000;
138       while(true)
139       {
140          //isComplete = pushCallbackHandler.isComplete();
141
isComplete = ((Boolean JavaDoc) remotingClient.invoke("getdone")).booleanValue();
142          if(!isComplete)
143          {
144             try
145             {
146                Thread.currentThread().sleep(waitPeriod);
147             }
148             catch(InterruptedException JavaDoc e)
149             {
150                e.printStackTrace();
151             }
152          }
153          else
154          {
155             break;
156          }
157       }
158       return isComplete;
159    }
160
161    public void setupServer(InvokerLocator locator) throws Exception JavaDoc
162    {
163       System.out.println("Starting remoting server with locator uri of: " + locator);
164       Connector connector = new Connector();
165       connector.setInvokerLocator(locator.getLocatorURI());
166       connector.start();
167
168       CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler();
169       // first parameter is sub-system name. can be any String value.
170
connector.addInvocationHandler("sample", invocationHandler);
171    }
172
173
174    /**
175     * Can pass transport and port to be used as parameters.
176     * Valid transports are 'rmi' and 'socket'.
177     *
178     * @param args
179     */

180    public static void main(String JavaDoc[] args)
181    {
182       if(args != null && args.length == 2)
183       {
184          transport = args[0];
185          port = Integer.parseInt(args[1]);
186       }
187       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
188       JBossASCallbackTestClient client = new JBossASCallbackTestClient(JBossASCallbackTestClient.class.getName(), locatorURI);
189       try
190       {
191          client.testPullCallback();
192       }
193       catch(Throwable JavaDoc e)
194       {
195          e.printStackTrace();
196          System.exit(1);
197       }
198       System.exit(0);
199    }
200
201    public class PushCallbackHandler extends CallbackHandler
202    {
203
204    }
205
206    public class CallbackHandler implements InvokerCallbackHandler
207    {
208       boolean isComplete = false;
209
210       /**
211        * Will take the callback message and send back to client.
212        * If client locator is null, will store them till client polls to get them.
213        *
214        * @param callback
215        * @throws org.jboss.remoting.callback.HandleCallbackException
216        *
217        */

218       public void handleCallback(Callback callback) throws HandleCallbackException
219       {
220          System.out.println("Received callback value of: " + callback.getCallbackObject());
221          System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
222          System.out.println("Received callback server invoker of: " + callback.getServerLocator());
223          isComplete = true;
224       }
225
226       public boolean isComplete()
227       {
228          return isComplete;
229       }
230    }
231
232 }
Popular Tags