【转】JGroup 介绍

JGroup

JGroups is a toolkit for reliable multicast communication.
(Note that this doesn't necessarily mean IP Multicast, JGroups can also use transports such as TCP).
It can be used to create groups of processes whose members can send messages to each other. The main features include

  • Group creation and deletion. Group members can be spread across LANs or WANs
  • Joining and leaving of groups
  • Membership detection and notification about joined/left/crashed members
  • Detection and removal of crashed members
  • Sending and receiving of member-to-group messages (point-to-multipoint)
  • Sending and receiving of member-to-member messages (point-to-point)

 

Flexible Protocol Stack

The most powerful feature of JGroups is its flexible protocol stack, which allows developers to adapt it to exactly match their application requirements and network characteristics.
The benefit of this is that you only pay for what you use. By mixing and matching protocols, various differing application requirements can be satisfied.
JGroups comes with a number of protocols (but anyone can write their own), for example

  • Transport protocols: UDP (IP Multicast), TCP, JMS
  • Fragmentation of large messages
  • Reliable unicast and multicast message transmission. Lost messages are retransmitted
  • Failure detection: crashed members are excluded from the membership
  • Ordering protocols: Atomic (all-or-none message delivery), Fifo, Causal, Total Order (sequencer or token based)
  • Membership
  • Encryption

Application Programming Interface

The API of JGroups is very simple (similar to a UDP socket). The code is always the same, regardless of the protocol stack used.

To be able to send/receive messages, a channel has to be created. The reliability of a channel is specified via XML, which then causes the creation of the underlying protocol stack. 
The example below creates a channel and sends/receives 1 message:

JChannel channel=new JChannel("/home/bela/udp.xml");

      channel.setReceiver(new ReceiverAdapter() {

          public void receive(Message msg) {

              System.out.println("received msg from " + msg.getSrc() + ": " + msg.getObject());

          }

      });

      channel.connect("MyCluster");

      channel.send(new Message(null, null, "hello world"));

      channel.close();

 

The channel's properties are specified in the constructor, in this case we refer to an XML file with an absolute path. If we use a relative path, then the file is looked up on the classpath.

The XML file contains a list of protocols to be used by the new channel. Protocols included might be transport, discovery, fragmentation, reliable transmission, flow control and state transfer.

To join a cluster, connect() is called. It returns when the member has successfully joined the cluster named "MyCluster", or when it has created a new cluster (if it is the first member).

Then a message is sent using the send() method. A message contains the receiver's address ('null' = all group members), the sender's address ('null', will be filled in by the protocol stack before sending the message) and a byte buffer. In the example, the String "hello world" is set to be the message's contents. It is serialized into the message's byte buffer.

Since the message is sent to all members, the sender will also receive it (unless the socket option 'receive own multicasts' is set to 'true'). This is done via the receive() callback, which was registered with the channel before.

Finally, the member closes the channel and thus leaves the cluster. This results in a notification being sent to all members who are registered for membership change notifications.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值