org.jgroups.blocks

## Class MessageListenerAdapter

- java.lang.Object
- - org.jgroups.blocks.MessageListenerAdapter

All Implemented Interfaces:
  ExtendedMessageListener, MessageListener

    public class MessageListenerAdapter
    extends java.lang.Object
    implements ExtendedMessageListener

This class provides multiplexing possibilities for `MessageListener` instances. Usually, we have more than one instance willing to listen to incoming messages, but only one that can produce state for group. `PullPushAdapter` allows only one instance of `MessageListener` to be registered for message notification. With help of this class you can overcome this limitation.

Author:
  Roman Rokytskyy (rrokytskyy@acm.org)

### Field Summary

Fields
`protected java.util.HashSet` `messageListeners`
`protected MessageListener[]` `messageListenersCache`
`protected MessageListener` `stateListener`

### Constructor Summary

Constructors
Constructor and Description
`MessageListenerAdapter()` Create default instance of this class.
`MessageListenerAdapter(MessageListener mainListener)` Create instance of this class.

### Method Summary

Methods
`void` `addMessageListener(MessageListener listener)` Add message listener to this adapter.
`byte[]` `getState()` Get state from state listener if present.
`void` `getState(java.io.OutputStream ostream)` Allows an application to write a state through a provided OutputStream.
`byte[]` `getState(java.lang.String state_id)` Allows an application to provide a partial state as a byte array
`void` `getState(java.lang.String state_id, java.io.OutputStream ostream)` Allows an application to write a partial state through a provided OutputStream.
`void` `receive(Message msg)` Receive message from group.
`void` `removeMessageListener(MessageListener listener)` Remove message listener from this adapter.
`void` `setState(byte[] state)` Set state of ths group.
`void` `setState(java.io.InputStream istream)` Allows an application to read a state through a provided InputStream.
`void` `setState(java.lang.String state_id, byte[] state)` Allows an application to read a partial state indicated by state_id from a given state byte array parameter.
`void` `setState(java.lang.String state_id, java.io.InputStream istream)` Allows an application to read a partial state through a provided InputStream.
`void` `setStateListener(MessageListener listener)` Register `listener` for state notification events.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### stateListener

    protected MessageListener stateListener

#### messageListeners

    protected final java.util.HashSet messageListeners

#### messageListenersCache

    protected MessageListener[] messageListenersCache

### Constructor Detail

#### MessageListenerAdapter

    public MessageListenerAdapter()

Create default instance of this class. Newly created instance will have no message or state listeners. You have to use `addMessageListener(MessageListener)` or `removeMessageListener(MessageListener)` to add or remove message listeners, and `setStateListener(MessageListener)` to set listener that will participate in state transfer.

#### MessageListenerAdapter

    public MessageListenerAdapter(MessageListener mainListener)

Create instance of this class. `mainListener` is a main listener instance that received message notifications and can get and set group state.

Parameters:
  `mainListener` - instance of `MessageListener` that will provide state messages.

### Method Detail

#### getState

    public byte[] getState()

Get state from state listener if present.

Specified by:
  `getState` in interface `MessageListener`
Returns:
  current state of the group state or `null` if no state listeners were registered.

#### getState

    public byte[] getState(java.lang.String state_id)

Description copied from interface: `ExtendedMessageListener`
Allows an application to provide a partial state as a byte array

Specified by:
  `getState` in interface `ExtendedMessageListener`
Parameters:
  `state_id` - id of the partial state requested
Returns:
  partial state for the given state_id

#### getState

    public void getState(java.io.OutputStream ostream)

Description copied from interface: `ExtendedMessageListener`
Allows an application to write a state through a provided OutputStream. An application is obligated to always close the given OutputStream reference.

Specified by:
  `getState` in interface `ExtendedMessageListener`
Parameters:
  `ostream` - the OutputStream
See Also:
  `OutputStream.close()`

#### getState

    public void getState(java.lang.String state_id,
                java.io.OutputStream ostream)

Description copied from interface: `ExtendedMessageListener`
Allows an application to write a partial state through a provided OutputStream. An application is obligated to always close the given OutputStream reference.

Specified by:
  `getState` in interface `ExtendedMessageListener`
Parameters:
  `state_id` - id of the partial state requested
  `ostream` - the OutputStream
See Also:
  `OutputStream.close()`

#### receive

    public void receive(Message msg)

Receive message from group. This method will send this message to each message listener that was registered in this adapter.

Specified by:
  `receive` in interface `MessageListener`
Parameters:
  `msg` - message to distribute within message listeners.

#### setState

    public void setState(byte[] state)

Set state of ths group. This method will delegate call to state listener if it was previously registered.

Specified by:
  `setState` in interface `MessageListener`

#### setState

    public void setState(java.lang.String state_id,
                byte[] state)

Description copied from interface: `ExtendedMessageListener`
Allows an application to read a partial state indicated by state_id from a given state byte array parameter.

Specified by:
  `setState` in interface `ExtendedMessageListener`
Parameters:
  `state_id` - id of the partial state requested
  `state` - partial state for the given state_id

#### setState

    public void setState(java.io.InputStream istream)

Description copied from interface: `ExtendedMessageListener`
Allows an application to read a state through a provided InputStream. An application is obligated to always close the given InputStream reference.

Specified by:
  `setState` in interface `ExtendedMessageListener`
Parameters:
  `istream` - the InputStream
See Also:
  `InputStream.close()`

#### setState

    public void setState(java.lang.String state_id,
                java.io.InputStream istream)

Description copied from interface: `ExtendedMessageListener`
Allows an application to read a partial state through a provided InputStream. An application is obligated to always close the given InputStream reference.

Specified by:
  `setState` in interface `ExtendedMessageListener`
Parameters:
  `state_id` - id of the partial state requested
  `istream` - the InputStream
See Also:
  `InputStream.close()`

#### addMessageListener

    public final void addMessageListener(MessageListener listener)

Add message listener to this adapter. This method registers `listener` for message notification.

Note, state notification will not be used.

#### removeMessageListener

    public void removeMessageListener(MessageListener listener)

Remove message listener from this adapter. This method deregisters `listener` from message notification.

#### setStateListener

    public void setStateListener(MessageListener listener)

Register `listener` for state notification events. There can be only one state listener per adapter.