組通信之jgroups篇----jgroups接口

3.2.1. MessageListener
Contrary to the pull-style of channels, some building blocks (e.g. PullPushAdapter ) provide an event-like push-style message delivery model. In this case, the entity to be notified of message reception needs to provide a callback to be invoked whenever a message has been received. The MessageListener interface below provides a method to do so:
public interface MessageListener {
    public void receive(Message msg);
    byte[] getState();
    void setState(byte[] state);
}
Method receive() will be called when a message is received. The getState() and setState() methods are used to fetch and set the group state (e.g. when joining). Refer to Section 3.6.12 for a discussion of state transfer.

不同於channel的pull模式,一些building blocks提供了類似事件驅動的push模式.在這種情況下,需要在有消息接收到時被通知的實體得提供一個回調方法在消息被接收時調用.MessageListener 接口提供瞭如下的功能.

函數receive()在有消息被接收時被調用.函數getState()和setState()在加組時被調用,主要用於得到和設置組的狀態.3.6.12節會進一步討論.

 

3.2.2. ExtendedMessageListener
JGroups release 2.3 introduces ExtendedMessageListener enabling partial state transfer (refer to Section 3.6.14 ) while release 2.4 further expands ExtendedMessageListener with streaming state transfer callbacks:

public interface ExtendedMessageListener extends MessageListener {
    byte[] getState(String state_id);
    void setState(String state_id, byte[] state);
    /*** since JGroups 2.4 *****/
    void getState(OutputStream ostream);
    void getState(String state_id, OutputStream ostream);
    void setState(InputStream istream);
    void setState(String state_id, InputStream istream);

JGroups2.3 版本提供了ExtendedMessageListener接口,它能夠實現部分狀態傳輸(3.6.14節會介紹).2.4版本更進一步擴展了狀態的流傳輸功能.

 

3.2.3. MembershipListener
The MembershipListener interface is similar to the MessageListener interface above: every time a new view, a suspicion message, or a block event is received, the corresponding method of the class implementing Membership-Listener will be called.
public interface MembershipListener {
    public void viewAccepted(View new_view);
    public void suspect(Object suspected_mbr);
    public void block();
}
Oftentimes the only method containing any functionality will be viewAccepted() which notifies the receiver that a new member has joined the group or that an existing member has left or crashed. The suspect() callback is invoked by JGroups whenever a member if suspected of having crashed, but not yet excluded.
The block() method is called to notify the member that it will soon be blocked sending messages. This is done by the FLUSH protocol, for example to ensure that nobody is sending messages while a state transfer is in progress.
When block() returns, any thread sending messages will be blocked, until FLUSH unblocks the thread again, e.g. after the state has been transferred successfully.
Therefore, block() can be used to send pending messages or complete some other work.
Note that block() should be brief, or else the entire FLUSH protocol is blocked.

MembershipListener接口類似於MessageListener接口,當有新的視圖消息,懷疑消息,阻塞事件消息被接收時,繼承了MembershipListener類的對象方法會被調用.

一般情況下,只有viewAccepted()函數會被調用,此時,程序被告知接收到新的成員加入組,已存在程序退出組或成員異常消息.suspect()函數被調用是當有成員被懷疑或異常,但還沒被排除出組的情況下.

block()函數被調用是要通知程序繼續發送的消息會被堵塞,這是由FLUSH實現的功能.如爲了保證在傳輸組狀態時無用戶發送消息.當block()函數被調用時,所有發送消息的線程都會被阻塞,直到FLUSH協議重新取消阻塞,如當成功傳輸完組狀態信息後.

所以,block()函數可以發送那些未完成的消息或做些其他的工作

block()函數必須得簡短,否則容易造成整個FLUSH協議的阻塞.

 

3.2.4. ExtendedMembershipListener

The ExtendedMembershipListener interface extends MembershipListener:
public interface ExtendedMembershipListener extends MembershipListener {
    public void unblock();
}
The unblock() method is called to notify the member that the FLUSH protocol has completed and the member can
resume sending messages. If the member did not stop sending messages on block(), FLUSH simply blocked them
and will resume, so no action is required from a member. Implementation of the unblock() callback is optional

ExtendedMembershipListener接口繼承自MembershipListener接口.

unblock()函數被調用是爲了通知應用程序FLUSH協議已經完成,可以重新再發送消息了.如果應用程序並未在block()函數中停止消息發送過程,那FLUSH協議自己會重新啓動被阻塞的消息,所以,應用程序就不必有其他動作.unblock()函數所以是可選的.

 

3.2.5. ChannelListener
public interface ChannelListener {
    void channelConnected(Channel channel);
    void channelDisconnected(Channel channel);
    void channelClosed(Channel channel);
    void channelShunned(); // deprecated in 2.8
    void channelReconnected(Address addr); // deprecated in 2.8
}
A class implementing ChannelListener can use the Channel.setChannelListener() method to register with a channel to obtain information about state changes in a channel. Whenever a channel is closed, disconnected or opened a callback will be invoked.

繼承自ChannelListener的類可以通過Channel的setChannelListener()函數進行註冊來得到當前channel的狀態變化信息,當channel被斷開連接,被關閉,被打開時,一些回調方法會被調用.

 

3.2.6. Receiver
public interface Receiver extends MessageListener, MembershipListener {
}
A Receiver can be used to receive messages and view changes in push-style; rather than having to pull these events from a channel, they will be dispatched to the receiver as soon as they have been received. This saves one thread (application thread, pulling messages from a channel, or the PullPushAdapter thread).

Note that JChannel.receive()has been deprecated and will be removed in 3.0. The preferred way of receiving messages is now via a Receiver callback (push style).

Receiver可以以push模式接收消息和視圖變化信息,而不是通過pull模式從channel拿.這可以節省一個線程.

JChannel的receive()函數在3.0版本中已經不推薦使用了,而是推薦使用Receiver的回調方法.

 

3.2.7. ExtendedReceiver
public interface ExtendedReceiver extends ExtendedMessageListener, MembershipListener {
}
This is a receiver who will be able to handle partial state transfer.

The Extended- interfaces(ExtendedMessageListener, ExtendedReceiver) will be merged with their parents in the 3.0 release of JGroups. The reason is that this will create an API backwards incompatibility, which we didn't want to introduce in the 2.x series.

ExtendedReceiver可以處理部分組狀態傳輸信息.

Extended-接口在3.0版本將會和他們的父類合併,因爲目前會造成API向後不兼容問題.

 

 

發佈了38 篇原創文章 · 獲贊 10 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章