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.
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.
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.
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.
25
* Demonstrates Callback acknowledgements.
27
* @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
32
package org.jboss.remoting.samples.callback.acknowledgement;
34
import java.net.InetAddress;
35
import java.net.UnknownHostException;
36
import java.util.HashMap;
38
import javax.management.MBeanServer;
40
import org.jboss.remoting.InvocationRequest;
41
import org.jboss.remoting.InvokerLocator;
42
import org.jboss.remoting.ServerInvocationHandler;
43
import org.jboss.remoting.ServerInvoker;
44
import org.jboss.remoting.callback.Callback;
45
import org.jboss.remoting.callback.CallbackListener;
46
import org.jboss.remoting.callback.InvokerCallbackHandler;
47
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
48
import org.jboss.remoting.transport.Connector;
49
import org.jboss.util.id.GUID;
52
public class CallbackAcknowledgeServer
54
public static final String APPLICATION_ACKNOWLDEGEMENTS = "applicationAcknowledgements";
55
public static final String REMOTING_ACKNOWLDEGEMENTS = "remotingAcknowledgements";
57
private static String transport = "socket";
58
private static String host;
59
private static int port = 5401;
61
private Connector connector;
64
* Can pass transport and port to be used as parameters.
65
* Default to "socket" and 5401.
69
public static void main(String[] args)
71
if(args != null && args.length == 2)
74
port = Integer.parseInt(args[1]);
78
host = InetAddress.getLocalHost().getHostName();
80
catch (UnknownHostException e1)
82
System.err.println("cannot get local host name");
85
String locatorURI = transport + "://" + host + ":" + port;
86
CallbackAcknowledgeServer server = new CallbackAcknowledgeServer();
89
server.setupServer(locatorURI);
91
// wait forever, let the user kill us at any point (at which point, the client will detect we went down)
104
public void setupServer(String locatorURI) throws Exception
106
connector = new Connector(new InvokerLocator(locatorURI));
108
connector.addInvocationHandler("test", new TestInvoocationHandler());
112
static class TestInvoocationHandler implements ServerInvocationHandler, CallbackListener
114
InvokerCallbackHandler callbackHandler;
117
public void setMBeanServer(MBeanServer server) {}
119
public void setInvoker(ServerInvoker invoker) {}
121
public Object invoke(InvocationRequest invocation) throws Throwable
123
String command = (String) invocation.getParameter();
124
System.out.println("command: " + command);
126
Callback cb = new Callback("callback " + ++counter);
128
// Register as listener and pass callback id.
129
HashMap returnPayload = new HashMap();
130
returnPayload.put(ServerInvokerCallbackHandler.CALLBACK_LISTENER, this);
131
returnPayload.put(ServerInvokerCallbackHandler.CALLBACK_ID, new GUID());
132
cb.setReturnPayload(returnPayload);
134
if (REMOTING_ACKNOWLDEGEMENTS.equals(command))
136
returnPayload.put(ServerInvokerCallbackHandler.REMOTING_ACKNOWLEDGES_PUSH_CALLBACKS, "true");
139
callbackHandler.handleCallback(cb);
143
public void addListener(InvokerCallbackHandler callbackHandler)
145
this.callbackHandler = callbackHandler;
148
public void removeListener(InvokerCallbackHandler callbackHandler) {}
151
* callbackSent() is called to acknowledgement the sending of a callback.
153
public void acknowledgeCallback(InvokerCallbackHandler callbackHandler, Object callbackId, Object response)
155
System.out.println("received acknowledgment for callback: " + callbackId);
156
System.out.println("response: " + response);
157
System.out.println("");