« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9704812 建立时间:2004年12月20日 |

| |
[java语言]jgroup使用RpcDispatcher和自身进行通讯的例子 原创空间, 软件技术, 电脑与网络
邢红瑞 发表于 2006/5/28 11:05:29 |
主要是callRemoteMethods的调用
import org.jgroups.*;import org.jgroups.util.RspList;import org.jgroups.util.Util;import org.jgroups.blocks.RpcDispatcher;import org.jgroups.blocks.GroupRequest;
import java.util.Vector;public class RpcDispatcherSimpleClient implements ChannelListener { public void print(String a, String b) { System.out.println(a + b); }
Channel channel; RpcDispatcher disp; RspList rsp_list; private String props = "UDP:" + "PING(num_initial_members=2;timeout=3000):" + "FD:" + "pbcast.PBCAST(gossip_interval=5000;gc_lag=50):" + "UNICAST:" + "FRAG:" + "pbcast.GMS:" + "pbcast.STATE_TRANSFER";
public void start(int num, long interval) throws Exception { channel = new JChannel(props); channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE); disp = new RpcDispatcher(channel, null, null, this); channel.addChannelListener(this); channel.connect("RpcDispatcherTestGroup");
for (int i = 0; i < num; i++) { System.out.println("Requesting num=" + num + ", interval=" + interval);
View v = channel.getView(); Vector members = new Vector(v.getMembers()); System.out.println("sending to " + members);
Util.sleep(interval);
if (members.size() > 1) { members.remove(0);
rsp_list = disp.callRemoteMethods(members, "print", new Object[]{"Key_" + i, "Value_" + i}, new Class[]{String.class, String.class}, GroupRequest.GET_ALL, 30000); System.out.println("Responses: " + rsp_list); } } System.out.println("Closing channel"); channel.close(); System.out.println("Closing channel: -- done");
System.out.println("Stopping dispatcher"); disp.stop(); System.out.println("Stopping dispatcher: -- done"); }
public static void main(String[] args) { int num = 10; long interval = 1000; for (int i = 0; i < args.length; i++) { if (args[i].equals("-num")) { num = Integer.parseInt(args[++i]); continue; } if (args[i].equals("-interval")) { interval = Long.parseLong(args[++i]); continue; } help(); return; }
try { new RpcDispatcherSimpleClient().start(num, interval); } catch (Exception e) { System.err.println(e); } }
private static void help() { System.out.println("RpcDispatcherTest [-help] [-num <number of msgs>] [-interval <sleep in ms between calls>]"); }
public void channelConnected(Channel channel) { //To change body of implemented methods use File | Settings | File Templates. }
public void channelDisconnected(Channel channel) { //To change body of implemented methods use File | Settings | File Templates. }
public void channelClosed(Channel channel) { //To change body of implemented methods use File | Settings | File Templates. }
public void channelShunned() { //To change body of implemented methods use File | Settings | File Templates. }
public void channelReconnected(Address address) { //To change body of implemented methods use File | Settings | File Templates. }}以上代码 没有用于启明星辰任何产品中(包括UTM)。 |
|
|