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向后不兼容问题.