[jboss-cvs] JBossRemoting/src/main/org/jboss/remoting/samples/callback/acknowledgement ...
Ron Sigal
ron_sigal at yahoo.com
Mon Nov 6 02:22:37 EST 2006
User: rsigal
Date: 06/11/06 02:22:37
Added: src/main/org/jboss/remoting/samples/callback/acknowledgement
Tag: remoting_2_x CallbackAcknowledgeClient.java
CallbackAcknowledgeServer.java
Log:
JBREM-605: Sample for callback acknowledgements.
Revision Changes Path
No revision
No revision
1.1.2.2 +286 -0 JBossRemoting/src/main/org/jboss/remoting/samples/callback/acknowledgement/CallbackAcknowledgeClient.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CallbackAcknowledgeClient.java
===================================================================
RCS file: CallbackAcknowledgeClient.java
diff -N CallbackAcknowledgeClient.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CallbackAcknowledgeClient.java 6 Nov 2006 07:22:37 -0000 1.1.2.2
@@ -0,0 +1,286 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+
+
+/**
+ * Tests Callback acknowledgements.
+ *
+ * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
+ * <p/>
+ * Copyright (c) 2006
+ * </p>
+ */
+package org.jboss.remoting.samples.callback.acknowledgement;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.CallbackPoller;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+
+
+public class CallbackAcknowledgeClient
+{
+ private static String transport = "socket";
+ private static String host;
+ private static int port = 5401;
+
+ private Client client;
+
+
+ /**
+ * Can pass transport and port to be used as parameters.
+ * Valid transports are 'rmi' and 'socket'.
+ *
+ * @param args
+ */
+ public static void main(String[] args)
+ {
+ if(args != null && args.length == 2)
+ {
+ transport = args[0];
+ port = Integer.parseInt(args[1]);
+ }
+ try
+ {
+ host = InetAddress.getLocalHost().getHostName();
+ }
+ catch (UnknownHostException e1)
+ {
+ System.err.println("cannot get local host name");
+ return;
+ }
+ String locatorURI = transport + "://" + host + ":" + port;
+ CallbackAcknowledgeClient client = new CallbackAcknowledgeClient();
+ try
+ {
+ client.createRemotingClient(locatorURI);
+
+ client.testPullCallbackPreprocessAcknowledgement();
+ client.testPullCallbackPostprocessAcknowledgement();
+ client.testPollCallbackPreprocessAcknowledgement();
+ client.testPollCallbackPostprocessAcknowledgement();
+ client.testPushCallbackPreprocessAcknowledgement();
+ client.testPushCallbackPostprocessAcknowledgement();
+
+ client.disconnectRemotingClient();
+ }
+ catch(Throwable e)
+ {
+ e.printStackTrace();
+ }
+ System.out.println("done.");
+ }
+
+
+ public void createRemotingClient(String locatorURI) throws Exception
+ {
+ client = new Client(new InvokerLocator(locatorURI));
+ client.connect();
+ }
+
+
+ public void disconnectRemotingClient()
+ {
+ if (client != null)
+ client.disconnect();
+ }
+
+
+ /**
+ * In this test the connection is configured for true pull callbacks, and
+ * the acknowledgement should be made implicitly by ServerInvokerCallbackHandler
+ * during the call to getCallbacks() (before the callbacks are retrieved by
+ * the client).
+ */
+ public void testPullCallbackPreprocessAcknowledgement()
+ {
+ try
+ {
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ client.addListener(callbackHandler);
+ client.invoke(CallbackAcknowledgeServer.PREPROCESS_TEST);
+ Thread.sleep(2000);
+ List callbacks = client.getCallbacks(callbackHandler);
+ Iterator it = callbacks.iterator();
+ while (it.hasNext())
+ {
+ Callback callback = (Callback) it.next();
+ System.out.println("received pull callback: " + callback.getParameter());
+ }
+ client.removeListener(callbackHandler);
+ }
+ catch (Throwable e)
+ {
+ System.out.println("failure: " + e.getMessage());
+ }
+ }
+
+
+ /**
+ * In this test, the connection is configured for true pull callbacks, and the
+ * acknowledgement should be made by an explicit call to Client.acknowledgeCallback()
+ * after the callback has been retrieved and (presumably) processed.
+ */
+ public void testPullCallbackPostprocessAcknowledgement()
+ {
+ try
+ {
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ client.addListener(callbackHandler);
+ client.invoke(CallbackAcknowledgeServer.POSTPROCESS_TEST);
+ List callbacks = client.getCallbacks(callbackHandler);
+ Thread.sleep(2000);
+ Iterator it = callbacks.iterator();
+ while (it.hasNext())
+ {
+ Callback callback = (Callback) it.next();
+ System.out.println("received pull callback: " + callback.getParameter());
+
+ // Could acknowledge individual callbacks:
+// ArrayList list = new ArrayList(1);
+// list.add(callback);
+// client.acknowledgeCallbacks(callbackHandler, list);
+ }
+
+ // Acknowledge pull callbacks.
+ client.acknowledgeCallbacks(callbackHandler, callbacks);
+
+ client.removeListener(callbackHandler);
+ }
+ catch (Throwable e)
+ {
+ System.out.println("failure: " + e.getMessage());
+ }
+ }
+
+
+ /**
+ * In this test the connection is configured for push callbacks implemented in
+ * Remoting by polling the server for callbacks and pushing them (on the client
+ * side) to the InvokerCallbackHandler. The acknowledgement should be made
+ * implicitly by ServerInvokerCallbackHandler during the call to getCallbacks()
+ * (before the callbacks are retrieved by the client).
+ */
+ public void testPollCallbackPreprocessAcknowledgement()
+ {
+ try
+ {
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ HashMap metadata = new HashMap();
+ metadata.put(CallbackPoller.CALLBACK_POLL_PERIOD, "100");
+ client.addListener(callbackHandler, metadata);
+ client.invoke(CallbackAcknowledgeServer.PREPROCESS_TEST);
+ Thread.sleep(2000);
+ client.removeListener(callbackHandler);
+ }
+ catch (Throwable e)
+ {
+ System.out.println("failure: " + e.getMessage());
+ }
+ }
+
+
+ /**
+ * In this test the connection is configured for push callbacks implemented in
+ * Remoting by polling the server for callbacks and pushing them (on the client
+ * side) to the InvokerCallbackHandler. The acknowledgement should be made
+ * implicitly by CallbackPoller, which it does by calling Client.acknowledgeCallback()
+ * after it has pushed the callback to the InvokerCallbackHandler.
+ */
+ public void testPollCallbackPostprocessAcknowledgement()
+ {
+ try
+ {
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ HashMap metadata = new HashMap();
+ metadata.put(CallbackPoller.CALLBACK_POLL_PERIOD, "100");
+ client.addListener(callbackHandler, metadata);
+ client.invoke(CallbackAcknowledgeServer.POSTPROCESS_TEST);
+ Thread.sleep(2000);
+ client.removeListener(callbackHandler);
+ }
+ catch (Throwable e)
+ {
+ System.out.println("failure: " + e.getMessage());
+ }
+ }
+
+
+ /**
+ * In this test the connection is configured for true push callbacks, and the
+ * acknowledgement should be made by ServerInvokerCallbackHandler.handleCallback()
+ * after it has pushed the Callback to the client.
+ */
+ public void testPushCallbackPreprocessAcknowledgement()
+ {
+ try
+ {
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ client.addListener(callbackHandler, null, null, true);
+ client.invoke(CallbackAcknowledgeServer.PREPROCESS_TEST);
+ Thread.sleep(2000);
+ client.removeListener(callbackHandler);
+ }
+ catch (Throwable e)
+ {
+ System.out.println("failure: " + e.getMessage());
+ }
+ }
+
+
+ /**
+ * In this test the connection is configured for true push callbacks, and the
+ * acknowledgement should be made by ServerInvokerCallbackHandler.handleCallback()
+ * after it has pushed the Callback to the client.
+ */
+ public void testPushCallbackPostprocessAcknowledgement()
+ {
+ try
+ {
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ client.addListener(callbackHandler, null, null, true);
+ client.invoke(CallbackAcknowledgeServer.POSTPROCESS_TEST);
+ client.removeListener(callbackHandler);
+ }
+ catch (Throwable e)
+ {
+ System.out.println("failure: " + e.getMessage());
+ }
+ }
+
+
+ static class TestCallbackHandler implements InvokerCallbackHandler
+ {
+ public void handleCallback(Callback callback) throws HandleCallbackException
+ {
+ System.out.println("received push callback: " + callback.getParameter());
+ }
+ }
+}
1.1.2.2 +176 -0 JBossRemoting/src/main/org/jboss/remoting/samples/callback/acknowledgement/CallbackAcknowledgeServer.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CallbackAcknowledgeServer.java
===================================================================
RCS file: CallbackAcknowledgeServer.java
diff -N CallbackAcknowledgeServer.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CallbackAcknowledgeServer.java 6 Nov 2006 07:22:37 -0000 1.1.2.2
@@ -0,0 +1,176 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+
+
+/**
+ * Tests Callback acknowledgements.
+ *
+ * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
+ * <p/>
+ * Copyright (c) 2006
+ * </p>
+ */
+package org.jboss.remoting.samples.callback.acknowledgement;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+
+import javax.management.MBeanServer;
+
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.CallbackListener;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+
+
+public class CallbackAcknowledgeServer
+{
+ public static final String PREPROCESS_TEST = "preprocessTest";
+ public static final String POSTPROCESS_TEST = "postprocessTest";
+
+ private static String transport = "socket";
+ private static String host;
+ private static int port = 5401;
+
+ private Connector connector;
+
+ /**
+ * Can pass transport and port to be used as parameters.
+ * Default to "socket" and 5401.
+ *
+ * @param args
+ */
+ public static void main(String[] args)
+ {
+ if(args != null && args.length == 2)
+ {
+ transport = args[0];
+ port = Integer.parseInt(args[1]);
+ }
+ try
+ {
+ host = InetAddress.getLocalHost().getHostName();
+ }
+ catch (UnknownHostException e1)
+ {
+ System.err.println("cannot get local host name");
+ return;
+ }
+ String locatorURI = transport + "://" + host + ":" + port;
+ CallbackAcknowledgeServer server = new CallbackAcknowledgeServer();
+ try
+ {
+ server.setupServer(locatorURI);
+
+ // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
+ while(true)
+ {
+ Thread.sleep(1000);
+ }
+
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ public void setupServer(String locatorURI) throws Exception
+ {
+ connector = new Connector(new InvokerLocator(locatorURI));
+ connector.start();
+ connector.addInvocationHandler("test", new TestInvoocationHandler());
+ }
+
+
+ static class TestInvoocationHandler implements ServerInvocationHandler, CallbackListener
+ {
+ InvokerCallbackHandler callbackHandler;
+
+ public void setMBeanServer(MBeanServer server) {}
+
+ public void setInvoker(ServerInvoker invoker) {}
+
+ public Object invoke(InvocationRequest invocation) throws Throwable
+ {
+
+ String command = (String) invocation.getParameter();
+ System.out.println("command: " + command);
+ if (PREPROCESS_TEST.equals(command))
+ {
+ Callback cb = new Callback(PREPROCESS_TEST);
+
+ // Register as preprocess listener and pass callback id.
+ HashMap returnPayload = new HashMap();
+ returnPayload.put(ServerInvokerCallbackHandler.CALLBACK_PREPROCESS_LISTENER, this);
+ returnPayload.put(ServerInvokerCallbackHandler.CALLBACK_ID, "preprocess");
+ cb.setReturnPayload(returnPayload);
+ callbackHandler.handleCallback(cb);
+ }
+ else if (POSTPROCESS_TEST.equals(command))
+ {
+ Callback cb = new Callback(POSTPROCESS_TEST);
+
+ // Register as preprocess listener and pass callback id.
+ HashMap returnPayload = new HashMap();
+ returnPayload.put(ServerInvokerCallbackHandler.CALLBACK_POSTPROCESS_LISTENER, this);
+ returnPayload.put(ServerInvokerCallbackHandler.CALLBACK_ID, "postprocess");
+ cb.setReturnPayload(returnPayload);
+ callbackHandler.handleCallback(cb);
+ }
+ else
+ {
+ throw new Exception("unrecognized test command");
+ }
+
+ return null;
+ }
+
+ public void addListener(InvokerCallbackHandler callbackHandler)
+ {
+ this.callbackHandler = callbackHandler;
+ }
+
+ public void removeListener(InvokerCallbackHandler callbackHandler) {}
+
+ /**
+ * callbackSent() is called to acknowledgement the sending of a callback.
+ */
+ public void callbackSent(Object callbackId, int preOrPost)
+ {
+ if (ServerInvokerCallbackHandler.CALLBACK_ACK_PREPROCESS == preOrPost)
+ {
+ System.out.println("received preprocess acknowledgment for callback: " + callbackId);
+ }
+ else if (ServerInvokerCallbackHandler.CALLBACK_ACK_POSTPROCESS == preOrPost)
+ {
+ System.out.println("received postprocess acknowledgment for callback: " + callbackId);
+ }
+ }
+ }
+}
More information about the jboss-cvs-commits
mailing list