jgroup例子
参考http://express.ruanko.com/ruanko-express_18/webpage/tech7.htmlhttp://puras.iteye.com/blog/81783
下载jgroups-2.12.0.CR2.jar
Receive建立一个线程一直等待
send后,Receive有响应
package com.jgroups;import org.jgroups.ChannelClosedException;import org.jgroups.ChannelNotConnectedException;import org.jgroups.JChannel;import org.jgroups.Message;/** * 发送数据的服务器端 * */public class Send { JChannel channel; //得到本机电脑的 用户名字 String user_name=System.getProperty("user.name", "n/a"); private void start() throws Exception { /** * 参数里指定Channel使用的协议栈,如果是空的,则使用默认的协议栈, * 位于JGroups包里的udp.xml。参数可以是一个以冒号分隔的字符串, * 或是一个XML文件,在XML文件里定义协议栈。 */ //创建一个通道 channel=new JChannel(); //加入一个群 channel.connect("ChatCluster"); //发送事件 sendEvent(); //关闭通道 channel.close(); } /** * 主要发送事件 */ private void sendEvent() { try{ String str="mytest";//发送的字符串 //这里的Message的第一个参数是发送端地址 //第二个是接收端地址 //第三个是发送的字符串 //具体参见jgroup send API Message msg=new Message(null, null, str); //发送channel.send(msg);} catch (ChannelNotConnectedException e){e.printStackTrace();} catch (ChannelClosedException e){e.printStackTrace();} } public static void main(String[] args) throws Exception { //开始发送事件 new Send().start(); }}
package com.jgroups;import org.jgroups.JChannel;import org.jgroups.Message;import org.jgroups.ReceiverAdapter;import org.jgroups.View;/** * 接收收据包 * */public class Receive extends ReceiverAdapter{JChannel channel;String user_name = System.getProperty("user.name", "n/a");public static void main(String[] args) throws Exception{//接收收据端new Receive().start();}private void start() throws Exception{//创建一个通道channel = new JChannel();//创建一个接收器channel.setReceiver(this);//加入一个群channel.connect("ChatCluster");}//覆盖父类的方法@Overridepublic void receive(Message msg){//具体参见msg的参数String receiveData=(String)msg.getObject();System.out.println("发过来的数据是:" +receiveData);}@Overridepublic void viewAccepted(View new_view){System.out.println("** view: " + new_view);}}
---------------------------
http://www.jgroups.org/tutorial/html/ch01.html#d0e122
D:\>java -cp jgroups-2.12.0.CR2.jar org.jgroups.demos.Draw
执行两次,看到两个窗口,画图会同步到另一个窗口
-------------------------------------------
看官方是2.5的,先弄下2.5吧,
E:\java\jgroups\JGroups-2.5.0.bin>java -cpjgroups-all.jar;commons-logging.jar -Djgroups.bind_addr=127.0.0.1 -Djava.net.preferIPv4Stack=true org.jgroups.demos.Draw2011-3-1 10:33:47 org.jgroups.JChannel init信息: JGroups version: 2.5.0-------------------------------------------------------GMS: address is 127.0.0.1:1936-------------------------------------------------------** View=
jgroups.bind_addr如果机器上多ip,指定 一个
java.net.preferIPv4Stack=true 关闭ipv6
页:
[1]