public class JChannel extends Channel
JChannel is instantiated using an appropriate form of a protocol stack description. Protocol stack can be described using a file, URL or a stream containing XML stack description.
Channel.State
Modifier and Type | Field and Description |
---|---|
protected java.util.List<AddressGenerator> |
address_generators |
protected java.lang.String |
cluster_name |
protected java.util.concurrent.ConcurrentMap<java.lang.String,java.lang.Object> |
config |
static java.lang.String |
DEFAULT_PROTOCOL_STACK
The default protocol stack used by the default constructor
|
protected boolean |
flush_supported
True if a flush protocol is available, false otherwise (set by CONFIG event from FLUSH protocol)
|
protected Address |
local_addr |
protected View |
my_view |
protected java.lang.String |
name |
protected DiagnosticsHandler.ProbeHandler |
probe_handler |
protected ProtocolStack |
prot_stack |
protected long |
received_bytes |
protected long |
received_msgs |
protected long |
sent_bytes |
protected long |
sent_msgs |
protected Promise<StateTransferResult> |
state_promise |
protected boolean |
state_transfer_supported
True if a state transfer protocol is available, false otherwise (set by CONFIG event from STATE_TRANSFER protocol)
|
protected boolean |
stats
Collect statistics
|
channel_listeners, discard_own_messages, log, receiver, socket_factory, state, up_handler
Constructor and Description |
---|
JChannel()
Constructs a
JChannel instance with the protocol stack
specified by the DEFAULT_PROTOCOL_STACK member. |
JChannel(boolean create_protocol_stack)
Creates a JChannel without a protocol stack; used for programmatic creation of channel and protocol stack
|
JChannel(java.util.Collection<Protocol> protocols)
Creates a channel from an array of protocols.
|
JChannel(org.w3c.dom.Element properties)
Constructs a JChannel instance with the protocol stack configuration contained by the specified XML element.
|
JChannel(java.io.File properties)
Constructs a JChannel instance with the protocol stack configuration contained by the specified file.
|
JChannel(java.io.InputStream input)
Creates a channel with a configuration based on an input stream.
|
JChannel(JChannel ch)
Creates a channel with the same configuration as the channel passed to this constructor.
|
JChannel(Protocol... protocols)
Creates a channel from an array of protocols.
|
JChannel(ProtocolStackConfigurator configurator)
Constructs a JChannel with the protocol stack configuration contained by the protocol stack configurator parameter.
|
JChannel(java.lang.String props)
Constructs a JChannel instance with the protocol stack configuration based upon the specified properties parameter.
|
JChannel(java.net.URL properties)
Constructs a JChannel instance with the protocol stack configuration indicated by the specified URL.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_close(boolean disconnect)
Disconnects and closes the channel.
|
protected void |
_connect(Event connect_event) |
protected boolean |
_preConnect(java.lang.String cluster_name) |
void |
addAddressGenerator(AddressGenerator address_generator)
Sets the new
AddressGenerator . |
protected void |
checkClosed()
health check
throws a ChannelClosed exception if the channel is closed |
protected void |
checkClosedOrNotConnected() |
void |
close()
Destroys the channel and its associated resources (e.g., the protocol stack).
|
void |
connect(java.lang.String cluster_name)
Connects the channel to a group.
|
void |
connect(java.lang.String cluster_name,
Address target,
long timeout)
Connects this channel to a group and gets a state from a specified state provider.
|
void |
connect(java.lang.String cluster_name,
Address target,
long timeout,
boolean useFlushIfPresent)
Connects this channel to a group and gets a state from a specified state provider.
This method invokes
If the FLUSH protocol is in the channel's stack definition, only one flush round is executed for both connecting and
fetching the state rather than two flushes if we invoke |
protected void |
connect(java.lang.String cluster_name,
boolean useFlushIfPresent)
Connects the channel to a group.
|
void |
disconnect()
Disconnects the channel if it is connected.
|
java.lang.Object |
down(Event evt)
Sends an event down the protocol stack.
|
protected java.util.Map<java.lang.String,java.lang.Long> |
dumpChannelStats() |
java.util.Map<java.lang.String,java.lang.Object> |
dumpStats()
Returns a map of statistics of the various protocols and of the channel itself.
|
java.util.Map<java.lang.String,java.lang.Object> |
dumpStats(java.lang.String protocol_name) |
java.util.Map<java.lang.String,java.lang.Object> |
dumpStats(java.lang.String protocol_name,
java.util.List<java.lang.String> attrs) |
java.lang.String |
dumpTimerQueue() |
void |
enableStats(boolean stats) |
boolean |
flushSupported() |
protected Address |
generateAddress() |
Address |
getAddress()
Returns the channel's own address.
|
java.lang.String |
getAddressAsString() |
java.lang.String |
getAddressAsUUID() |
AddressGenerator |
getAddressGenerator()
Deprecated.
Doesn't make any sense as there's list of address generators, will be removed in 4.0
|
java.lang.String |
getClusterName()
Returns the cluster name of the group of which the channel is a member.
|
java.lang.String |
getName()
Returns the logical name of this channel if set.
|
java.lang.String |
getName(Address member)
Returns the logical name of a given member.
|
int |
getNumberOfTasksInTimer() |
java.lang.String |
getProperties()
Returns the protocol stack configuration in string format.
|
ProtocolStack |
getProtocolStack()
Returns the protocol stack
|
long |
getReceivedBytes() |
long |
getReceivedMessages() |
long |
getSentBytes() |
long |
getSentMessages() |
void |
getState(Address target,
long timeout)
Retrieves the full state from the target member.
|
void |
getState(Address target,
long timeout,
boolean useFlushIfPresent)
Retrieves state from the target member.
|
protected void |
getState(Address target,
long timeout,
java.util.concurrent.Callable<java.lang.Boolean> flushInvoker) |
protected TimeScheduler |
getTimer() |
int |
getTimerThreads() |
static java.lang.String |
getVersion() |
View |
getView()
Gets the current view.
|
java.lang.String |
getViewAsString() |
protected void |
init()
Initializes all variables.
|
protected void |
init(JChannel ch) |
protected void |
init(ProtocolStackConfigurator configurator) |
protected java.lang.Object |
invokeCallback(int type,
java.lang.Object arg) |
JChannel |
name(java.lang.String name)
Names a channel, same as
Channel.setName(String) |
java.lang.String |
printProtocolSpec(boolean include_properties)
Returns a pretty-printed form of all the protocols.
|
JChannel |
receiver(Receiver r) |
boolean |
removeAddressGenerator(AddressGenerator address_generator) |
void |
resetStats() |
void |
send(Address dst,
byte[] buf)
Sends a message.
|
void |
send(Address dst,
byte[] buf,
int offset,
int length)
Sends a message to a destination.
|
void |
send(Address dst,
java.lang.Object obj)
Helper method to create a Message with given parameters and invoke
Channel.send(Message) . |
void |
send(Message msg)
Sends a message.
|
protected void |
setAddress()
Generates new UUID and sets local address.
|
void |
setAddressGenerator(AddressGenerator address_generator)
Deprecated.
|
void |
setName(java.lang.String name)
Sets the logical name for the channel.
|
void |
setProtocolStack(ProtocolStack stack) |
void |
startFlush(boolean automatic_resume)
Performs the flush of the cluster, ie.
|
void |
startFlush(java.util.List<Address> flushParticipants,
boolean automatic_resume)
Performs the flush of the cluster but only for the specified flush participants.
|
protected void |
startStack(java.lang.String cluster_name) |
boolean |
statsEnabled() |
void |
stopFlush()
Stops the current flush of the cluster.
|
void |
stopFlush(java.util.List<Address> flushParticipants)
Stops the current flush of the cluster for the specified flush participants.
|
protected void |
stopStack(boolean stop,
boolean destroy) |
java.lang.String |
toString(boolean details) |
java.lang.Object |
up(Event evt)
Callback method
Called by the ProtocolStack when a message is received. |
void |
up(MessageBatch batch)
Callback invoked by the protocol stack to deliver a message batch
|
addChannelListener, clearChannelListeners, getDiscardOwnMessages, getReceiver, getSocketFactory, getState, getUpHandler, isClosed, isConnected, isConnecting, isOpen, notifyChannelClosed, notifyChannelConnected, notifyChannelDisconnected, removeChannelListener, setDiscardOwnMessages, setReceiver, setSocketFactory, setUpHandler
public static final java.lang.String DEFAULT_PROTOCOL_STACK
protected Address local_addr
protected java.util.List<AddressGenerator> address_generators
protected java.lang.String name
protected java.lang.String cluster_name
protected View my_view
protected ProtocolStack prot_stack
protected final Promise<StateTransferResult> state_promise
protected boolean state_transfer_supported
protected volatile boolean flush_supported
protected final java.util.concurrent.ConcurrentMap<java.lang.String,java.lang.Object> config
protected boolean stats
protected long sent_msgs
protected long received_msgs
protected long sent_bytes
protected long received_bytes
protected final DiagnosticsHandler.ProbeHandler probe_handler
public JChannel(boolean create_protocol_stack)
create_protocol_stack
- If true, the default configuration will be used. If false, no protocol stack
will be createdpublic JChannel() throws java.lang.Exception
JChannel
instance with the protocol stack
specified by the DEFAULT_PROTOCOL_STACK
member.java.lang.Exception
- If problems occur during the initialization of the protocol stack.public JChannel(java.io.File properties) throws java.lang.Exception
properties
- A file containing a JGroups XML protocol stack configuration.java.lang.Exception
- If problems occur during the configuration or initialization of the protocol stack.public JChannel(org.w3c.dom.Element properties) throws java.lang.Exception
properties
- An XML element containing a JGroups XML protocol stack configuration.java.lang.Exception
- If problems occur during the configuration or initialization of the protocol stack.public JChannel(java.net.URL properties) throws java.lang.Exception
properties
- A URL pointing to a JGroups XML protocol stack configuration.java.lang.Exception
- If problems occur during the configuration or initialization of the protocol stack.public JChannel(java.lang.String props) throws java.lang.Exception
props
- A file containing a JGroups XML configuration, a URL pointing to an XML configuration, or an old
style plain configuration string.java.lang.Exception
- If problems occur during the configuration or initialization of the protocol stack.public JChannel(java.io.InputStream input) throws java.lang.Exception
input
- An input stream, pointing to a streamed configurationjava.lang.Exception
public JChannel(ProtocolStackConfigurator configurator) throws java.lang.Exception
All of the public constructors of this class eventually delegate to this method.
configurator
- A protocol stack configurator containing a JGroups protocol stack configuration.java.lang.Exception
- If problems occur during the initialization of the protocol stack.public JChannel(Protocol... protocols) throws java.lang.Exception
close()
, the protocol
list should not be reused, ie. new JChannel(protocols) would reuse the same protocol list, and this
might lead to problems !protocols
- The list of protocols, from bottom to top, ie. the first protocol in the list is the transport,
the last the top protocoljava.lang.Exception
public JChannel(java.util.Collection<Protocol> protocols) throws java.lang.Exception
close()
, the protocol
list should not be reused, ie. new JChannel(protocols) would reuse the same protocol list, and this
might lead to problems !protocols
- The list of protocols, from bottom to top, ie. the first protocol in the list is the transport,
the last the top protocoljava.lang.Exception
public JChannel(JChannel ch) throws java.lang.Exception
ch
- java.lang.Exception
public ProtocolStack getProtocolStack()
getProtocolStack
in class Channel
public void setProtocolStack(ProtocolStack stack)
public java.lang.String getProperties()
getProperties
in class Channel
public boolean statsEnabled()
public void enableStats(boolean stats)
public void resetStats()
public long getSentMessages()
public long getSentBytes()
public long getReceivedMessages()
public long getReceivedBytes()
public int getNumberOfTasksInTimer()
public int getTimerThreads()
public java.lang.String dumpTimerQueue()
public java.lang.String printProtocolSpec(boolean include_properties)
public void connect(java.lang.String cluster_name) throws java.lang.Exception
Channel
All channels with the same name form a group, that means all messages sent to the group will be received by all channels connected to the same cluster name.
protected void connect(java.lang.String cluster_name, boolean useFlushIfPresent) throws java.lang.Exception
java.lang.Exception
connect(String)
public void connect(java.lang.String cluster_name, Address target, long timeout) throws java.lang.Exception
Channel
This method essentially invokes
connect
and
getState
methods successively.
If FLUSH protocol is in channel's stack definition only one flush is executed for both connecting and
fetching state rather than two flushes if we invoke
connect
and
getState
in succession.
If the channel is closed an exception will be thrown.
- Specified by:
connect
in class Channel
- Parameters:
cluster_name
- the cluster name to connect to. Cannot be null.
target
- the state provider. If null state will be fetched from coordinator, unless this channel is coordinator.
timeout
- the timeout for state transfer.
- Throws:
java.lang.Exception
- Connecting to the cluster or state transfer was not successful
java.lang.IllegalStateException
- The channel is closed and therefore cannot be used
-
connect
public void connect(java.lang.String cluster_name,
Address target,
long timeout,
boolean useFlushIfPresent)
throws java.lang.Exception
Connects this channel to a group and gets a state from a specified state provider.
This method invokes connect()
and then getState
.
If the FLUSH protocol is in the channel's stack definition, only one flush round is executed for both connecting and
fetching the state rather than two flushes if we invoke connect
and getState
in succession.
If the channel is already connected, an error message will be printed to the error log.
If the channel is closed a ChannelClosed exception will be thrown.
- Parameters:
cluster_name
- The cluster name to connect to. Cannot be null.
target
- The state provider. If null, the state will be fetched from the coordinator, unless this channel
is the coordinator.
timeout
- The timeout for the state transfer.
- Throws:
java.lang.Exception
- The protocol stack cannot be started, or the JOIN failed
java.lang.IllegalStateException
- The channel is closed or disconnected
StateTransferException
- State transfer was not successful
-
disconnect
public void disconnect()
Description copied from class: Channel
Disconnects the channel if it is connected. If the channel is closed or disconnected, this
operation is ignored
The channel can then be connected to the same or a different cluster again.
- Specified by:
disconnect
in class Channel
- See Also:
Channel.connect(String)
-
close
public void close()
Description copied from class: Channel
Destroys the channel and its associated resources (e.g., the protocol stack). After a channel
has been closed, invoking methods on it throws the ChannelClosed
exception (or
results in a null operation). It is a null operation if the channel is already closed.
If the channel is connected to a group, disconnect()
will be called first.
-
dumpStats
public java.util.Map<java.lang.String,java.lang.Object> dumpStats()
Description copied from class: Channel
Returns a map of statistics of the various protocols and of the channel itself.
-
dumpStats
public java.util.Map<java.lang.String,java.lang.Object> dumpStats(java.lang.String protocol_name,
java.util.List<java.lang.String> attrs)
-
dumpStats
public java.util.Map<java.lang.String,java.lang.Object> dumpStats(java.lang.String protocol_name)
-
dumpChannelStats
protected java.util.Map<java.lang.String,java.lang.Long> dumpChannelStats()
-
send
public void send(Message msg)
throws java.lang.Exception
Description copied from class: Channel
Sends a message. The message contains
- a destination address (Address). A
null
address sends the message to all
group members.
- a source address. Can be left empty as it will be assigned automatically
- a byte buffer. The message contents.
- several additional fields. They can be used by application programs (or patterns). E.g. a
message ID, flags etc
-
send
public void send(Address dst,
java.lang.Object obj)
throws java.lang.Exception
Description copied from class: Channel
Helper method to create a Message with given parameters and invoke Channel.send(Message)
.
- Specified by:
send
in class Channel
- Parameters:
dst
- Destination address for message. If null, message will be sent to all current group
members
obj
- A serializable object. Will be marshalled into the byte buffer of the Message. If it
is not serializable, an exception will be thrown
- Throws:
java.lang.Exception
- exception thrown if message sending was not successful
-
send
public void send(Address dst,
byte[] buf)
throws java.lang.Exception
Description copied from class: Channel
Sends a message. See Channel.send(Address,byte[],int,int)
for details
-
send
public void send(Address dst,
byte[] buf,
int offset,
int length)
throws java.lang.Exception
Description copied from class: Channel
Sends a message to a destination.
- Specified by:
send
in class Channel
- Parameters:
dst
- The destination address. If null, the message will be sent to all cluster nodes (=
group members)
buf
- The buffer to be sent
offset
- The offset into the buffer
length
- The length of the data to be sent. Has to be <= buf.length - offset. This will send
length
bytes starting at offset
- Throws:
java.lang.Exception
- If send() failed
-
getView
public View getView()
Description copied from class: Channel
Gets the current view. The view may only be available after a successful
connect()
. The result of calling this method on an unconnected channel is
implementation defined (may return null). Calling this method on a closed channel returns a
null view.
-
getViewAsString
public java.lang.String getViewAsString()
-
getVersion
public static java.lang.String getVersion()
-
getAddress
public Address getAddress()
Description copied from class: Channel
Returns the channel's own address. The result of calling this method on an unconnected channel
is implementation defined (may return null). Calling this method on a closed channel returns
null. Addresses can be used as destination in the send()
operation.
- Specified by:
getAddress
in class Channel
- Returns:
- The channel's address (opaque)
-
getAddressAsString
public java.lang.String getAddressAsString()
-
getAddressAsUUID
public java.lang.String getAddressAsUUID()
-
getName
public java.lang.String getName()
Description copied from class: Channel
Returns the logical name of this channel if set.
-
getName
public java.lang.String getName(Address member)
Description copied from class: Channel
Returns the logical name of a given member. The lookup is from the local cache of logical
address / logical name mappings and no remote communication is performed.
-
setName
public void setName(java.lang.String name)
Description copied from class: Channel
Sets the logical name for the channel. The name will stay associated with this channel for the
channel's lifetime (until close() is called). This method should be called before
calling connect().
-
name
public JChannel name(java.lang.String name)
Description copied from class: Channel
Names a channel, same as Channel.setName(String)
-
getClusterName
public java.lang.String getClusterName()
Description copied from class: Channel
Returns the cluster name of the group of which the channel is a member. This is the object
that was the argument to connect()
. Calling this method on a closed channel
returns null
.
- Specified by:
getClusterName
in class Channel
- Returns:
- The cluster name
-
getAddressGenerator
@Deprecated
public AddressGenerator getAddressGenerator()
Deprecated. Doesn't make any sense as there's list of address generators, will be removed in 4.0
Returns the first AddressGenerator
in the list, or null if none is set
- Returns:
- Since:
- 2.12
-
setAddressGenerator
@Deprecated
public void setAddressGenerator(AddressGenerator address_generator)
Deprecated. Use addAddressGenerator(org.jgroups.stack.AddressGenerator)
instead
-
addAddressGenerator
public void addAddressGenerator(AddressGenerator address_generator)
Sets the new AddressGenerator
. New addresses will be generated using the new generator. This
should not be done while a channel is connected, but before connecting.
- Parameters:
address_generator
-
- Since:
- 2.12
-
removeAddressGenerator
public boolean removeAddressGenerator(AddressGenerator address_generator)
-
getState
public void getState(Address target,
long timeout)
throws java.lang.Exception
Description copied from class: Channel
Retrieves the full state from the target member.
State transfer is initiated by invoking getState on this channel. The state provider in turn
invokes MessageListener.getState(java.io.OutputStream)
callback and sends a state to
this node, the state receiver. After the state arrives to the state receiver
MessageListener.setState(java.io.InputStream)
callback is invoked to install the
state.
- Specified by:
getState
in class Channel
- Parameters:
target
- The state provider. If null the coordinator is used by default
timeout
- The number of milliseconds to wait for the operation to complete successfully. 0
waits until the state has been received
- Throws:
java.lang.IllegalStateException
- The channel was closed or disconnected, or the flush (if present) failed
StateTransferException
- raised if there was a problem during the state transfer
java.lang.Exception
- See Also:
MessageListener.getState(java.io.OutputStream)
,
MessageListener.setState(java.io.InputStream)
-
getState
public void getState(Address target,
long timeout,
boolean useFlushIfPresent)
throws java.lang.Exception
Retrieves state from the target member. See getState(Address,long)
for details.
- Throws:
java.lang.Exception
-
_preConnect
protected boolean _preConnect(java.lang.String cluster_name)
throws java.lang.Exception
- Throws:
java.lang.Exception
-
_connect
protected void _connect(Event connect_event)
throws java.lang.Exception
- Throws:
java.lang.Exception
-
getState
protected void getState(Address target,
long timeout,
java.util.concurrent.Callable<java.lang.Boolean> flushInvoker)
throws java.lang.Exception
- Throws:
java.lang.Exception
-
up
public java.lang.Object up(Event evt)
Callback method
Called by the ProtocolStack when a message is received.
- Parameters:
evt
- the event carrying the message from the protocol stack
-
up
public void up(MessageBatch batch)
Callback invoked by the protocol stack to deliver a message batch
-
down
public java.lang.Object down(Event evt)
Sends an event down the protocol stack. Note that - contrary to send(Message)
, if the event is a message,
no checks are performed whether the channel is closed or disconnected.
-
toString
public java.lang.String toString(boolean details)
-
invokeCallback
protected java.lang.Object invokeCallback(int type,
java.lang.Object arg)
-
init
protected final void init(ProtocolStackConfigurator configurator)
throws java.lang.Exception
- Throws:
java.lang.Exception
-
init
protected final void init(JChannel ch)
throws java.lang.Exception
- Throws:
java.lang.Exception
-
init
protected void init()
Initializes all variables. Used after close() or disconnect(),
to be ready for new connect()
-
startStack
protected void startStack(java.lang.String cluster_name)
throws java.lang.Exception
- Throws:
java.lang.Exception
-
setAddress
protected void setAddress()
Generates new UUID and sets local address. Sends down a REMOVE_ADDRESS (if existing address was present) and
a SET_LOCAL_ADDRESS
-
generateAddress
protected Address generateAddress()
-
checkClosed
protected void checkClosed()
health check
throws a ChannelClosed exception if the channel is closed
-
checkClosedOrNotConnected
protected void checkClosedOrNotConnected()
-
_close
protected void _close(boolean disconnect)
Disconnects and closes the channel. This method does the following things
- Calls
this.disconnect
if the disconnect parameter is true
- Calls
ProtocolStack.stop
on the protocol stack
- Calls
ProtocolStack.destroy
on the protocol stack
- Sets the channel closed and channel connected flags to true and false
- Notifies any channel listener of the channel close operation
-
stopStack
protected void stopStack(boolean stop,
boolean destroy)
-
flushSupported
public boolean flushSupported()
- Specified by:
flushSupported
in class Channel
-
startFlush
public void startFlush(boolean automatic_resume)
throws java.lang.Exception
Description copied from class: Channel
Performs the flush of the cluster, ie. all pending application messages are flushed out of the cluster and
all members ack their reception. After this call returns, no member will be allowed to send any
messages until Channel.stopFlush()
is called.
In the case of flush collisions (another member attempts flush at roughly the same time) start flush will
fail by throwing an Exception. Applications can re-attempt flushing after certain back-off period.
JGroups provides a helper random sleep time backoff algorithm for flush using Util class.
- Specified by:
startFlush
in class Channel
- Parameters:
automatic_resume
- if true call Channel.stopFlush()
after the flush
- Throws:
java.lang.Exception
- See Also:
Util.startFlush(Channel, List, int, long, long)
-
startFlush
public void startFlush(java.util.List<Address> flushParticipants,
boolean automatic_resume)
throws java.lang.Exception
Description copied from class: Channel
Performs the flush of the cluster but only for the specified flush participants.
All pending messages are flushed out but only for the flush participants. The remaining
members in the cluster are not included in the flush. The list of flush participants should be
a proper subset of the current view.
If this flush is not automatically resumed it is an obligation of the application to invoke
the matching Channel.stopFlush(List)
method with the same list of members used in
Channel.startFlush(List, boolean)
.
- Specified by:
startFlush
in class Channel
automatic_resume
- if true call Channel.stopFlush()
after the flush
- Throws:
java.lang.Exception
- See Also:
Channel.startFlush(boolean)
,
Util.startFlush(Channel, List, int, long, long)
-
stopFlush
public void stopFlush()
Description copied from class: Channel
Stops the current flush of the cluster. Cluster members are unblocked and allowed to send new
and pending messages.
- Specified by:
stopFlush
in class Channel
- See Also:
Channel.startFlush(boolean)
,
Channel.startFlush(List, boolean)
-
stopFlush
public void stopFlush(java.util.List<Address> flushParticipants)
Description copied from class: Channel
Stops the current flush of the cluster for the specified flush participants. Flush
participants are unblocked and allowed to send new and pending messages.
It is an obligation of the application to invoke the matching
Channel.startFlush(List, boolean)
method with the same list of members prior to invocation of
this method.
-
getTimer
protected TimeScheduler getTimer()
Copyright © 1998-2020 Red Hat. All Rights Reserved.