org.jgroups.demos.RelayDemoRpc Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
package org.jgroups.demos;
import org.jgroups.*;
import org.jgroups.blocks.MethodCall;
import org.jgroups.blocks.RequestOptions;
import org.jgroups.blocks.ResponseMode;
import org.jgroups.blocks.RpcDispatcher;
import org.jgroups.protocols.relay.SiteMaster;
import org.jgroups.util.Rsp;
import org.jgroups.util.RspList;
import org.jgroups.util.Util;
import java.util.*;
/** Demos RELAY. Create 2 *separate* clusters with RELAY as top protocol. Each RELAY has bridge_props="tcp.xml" (tcp.xml
* needs to be present). Then start 2 instances in the first cluster and 2 instances in the second cluster. They should
* find each other, and typing in a window should send the text to everyone, plus we should get 4 responses.
* @author Bela Ban
*/
public class RelayDemoRpc implements Receiver {
protected JChannel ch;
protected RpcDispatcher disp;
protected String local_addr;
protected View view;
protected static final long RPC_TIMEOUT=10000;
public static void main(String[] args) throws Exception {
String props="udp.xml";
String name=null;
for(int i=0; i < args.length; i++) {
if(args[i].equals("-props")) {
props=args[++i];
continue;
}
if(args[i].equals("-name")) {
name=args[++i];
continue;
}
System.out.println("RelayDemo [-props props] [-name name]");
return;
}
RelayDemoRpc demo=new RelayDemoRpc();
demo.start(props, name);
}
public void receive(Message msg) {
}
public void start(String props, String name) throws Exception {
ch=new JChannel(props);
if(name != null)
ch.setName(name);
disp=new RpcDispatcher(ch, this).setReceiver(this);
ch.connect("RelayDemo");
local_addr=ch.getAddress().toString();
MethodCall call=new MethodCall(getClass().getMethod("handleMessage", String.class, String.class));
for(;;) {
String line=Util.readStringFromStdin(": ");
if(line.startsWith("help")) {
System.out.println("unicast // unicasts to all members of local view\n" +
"site + // unicasts to all listed site masters, e.g. \"site sfo lon\"\n" +
"mcast + // anycasts to all local members, plus listed site masters \n" +
" // multicast, RELAY2 will relay to all members of sites");
continue;
}
call.setArgs(line, local_addr);
// unicast to every member of the local cluster
if(line.equalsIgnoreCase("unicast")) {
for(Address dest: view.getMembers()) {
System.out.println("invoking method in " + dest + ": ");
try {
Object rsp=disp.callRemoteMethod(dest, call, new RequestOptions(ResponseMode.GET_ALL, RPC_TIMEOUT));
System.out.println("rsp from " + dest + ": " + rsp);
}
catch(Throwable throwable) {
throwable.printStackTrace();
}
}
}
// unicast to 1 SiteMaster
else if(line.startsWith("site")) {
Collection site_masters=parseSiteMasters(line.substring("site".length()));
for(String site_master: site_masters) {
try {
SiteMaster dest=new SiteMaster(site_master);
System.out.println("invoking method in " + dest + ": ");
Object rsp=disp.callRemoteMethod(dest, call, new RequestOptions(ResponseMode.GET_ALL, RPC_TIMEOUT));
System.out.println("rsp from " + dest + ": " + rsp);
}
catch(Throwable t) {
t.printStackTrace();
}
}
}
// mcast to all local members and N SiteMasters
else if(line.startsWith("mcast")) {
Collection site_masters=parseSiteMasters(line.substring("mcast".length()));
Collection dests=new ArrayList<>(site_masters.size());
for(String site_master: site_masters) {
try {
dests.add(new SiteMaster(site_master));
}
catch(Throwable t) {
System.err.println("failed adding SiteMaster for " + site_master + ": " + t);
}
}
dests.addAll(view.getMembers());
System.out.println("invoking method in " + dests + ": ");
RspList