How to use the cluster communication component tribes

Source: Internet
Author: User
Tags getmessage

Above has carried on the deep analysis to the internal realization mechanism and the principle of tribes, after understanding its design principle to see how to use the tribes, the entire use is quite simple and convenient, only needs four steps:

① defines a message object, because the message object is to be passed between the network, the network transport involves the serialization, therefore needs to implement the Serializable interface.

public class Mymessage implements Serializable {

Private String message;

Public String GetMessage () {

return message;

}

public void Setmessage (String message) {

this.message = message;

}

}

② defines a Channellistener listener, and the processing logic for the message is placed in the Messagereceived method.

public class Mymessagelistener implements channellistener{

Public Boolean accept (Serializable mymessage, Member Member) {

return true;

}

public void messagereceived (Serializable mymessage, Member Member) {

System.out.println (((mymessage) mymessage). GetMessage () + "from" +member.getname ());

}

}

③ defines a Membershiplistener listener, the addition of cluster members and the logical processing of failures, in memberadded the member to join the event logic processing , in logical handling of member failure events in memberdisappeared.

public class Mymemberlistener implements Membershiplistener {

public void memberadded (Member Member) {

System.out.println (Member.getname () + "Added");

}

public void memberdisappeared (Member Member) {

System.out.println (Member.getname () + "disappeared");

}

}

④ The main program, instantiating channellistener , Span style= "FONT-FAMILY:CALIBRI;" >membershiplistener channel because the cluster communication needs to start several nodes to be implemented, the args "R" means that only one node is started and the cluster is joined, while the parameter value "s" indicates that the boot node joins the cluster and sends message "R" parameter, which means that two nodes are started, and then run with the "s" parameter, that is, the third node starts and forwards two idiom nodes, and the first two nodes output "hello  from   tcp://{169, 254, 75, 186}:4002 ", and the member listener outputs a message like this when the node joins or fails" tcp://{169, 254, 75,  186}:4002 added "," tcp://{169, 254, 75, 186}:4000 disappeared ".

public class Tribestest {

public static void Main (string[] args) throws Channelexception,interruptedexception {

Channel MyChannel = new Groupchannel ();

Channellistener Msglistener = new Mymessagelistener ();

Membershiplistener Mbrlistener = new Mymemberlistener ();

Mychannel.addmembershiplistener (Mbrlistener);

Mychannel.addchannellistener (Msglistener);

Mychannel.start (Channel.default);

Switch (Args[0]) {

Case ("R"):

while (true)

Thread.CurrentThread (). Sleep (1000);

Case ("s"):

Mymessage mymsg = new Mymessage ();

Mymsg.setmessage ("Hello");

member[] group = Mychannel.getmembers ();

Mychannel.send (Group, mymsg, Channel.send_options_default);

while (true)

Thread.CurrentThread (). Sleep (1000);

}

}

}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How to use the cluster communication component tribes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.