java.lang.Object
cl.netswitch.lib.aio.ChannelBase
cl.netswitch.lib.client.ClientChannel
- All Implemented Interfaces:
AutoCloseable
Communication channel between a client application and the NetSwitch
server. Through this channel, an application can send service requests,
receive multicasts, and obtain other services.
The communication channel is established as late as possible. Yet, once
established, it is kept running until an operator executes a " stop
"
console command. If the communication channel is lost for any other reason,
it is automatically re-established as soon as requiered.
The following example uses a ClientChannel
named SAMPLECLI
to
send a request to a server named SAMPLESER
. If the reply message
status is not SUCCESS
an exception is thrown with the returned error.
If successful, the reply message data is displayed.
import cl.netswitch.lib.client.ClientChannel; import cl.netswitch.lib.message.Message; public class SampleClient { public static void main(String[] args) throws Exception { // Create a client channel named "SAMPLECLI" ClientChannel channel = new ClientChannel(); channel.connect("SAMPLECLI", "tcp://localhost:20102"); // Request "HELLO" service from "SAMPLESER" server Message reply = channel.sendRequest("SAMPLESER.HELLO", "Hello world!!"); if (reply.getStatus() != Message.SUCCESS) throw new Exception(reply.getText()); // Display the reply returned from "SAMPLESER" server System.out.println("The reply data: " + reply.getText()); } }
Property | Description | Type | Default |
---|---|---|---|
netswitch.client.«name».channel.password |
Password use to authenticate the client channel | String |
none |
netswitch.client.«name».compression.threshold |
Minimum size that a message must have before it is compressed (bytes) | Integer |
0 (disabled) |
netswitch.client.«name».keep.alive |
Enable or disable the socket SO_KEEPALIVE option | Boolean |
false |
netswitch.client.«name».reply.timeout |
Maximum time to wait for reply of connect message (milliseconds) | Integer |
10000 |
netswitch.client.«name».reader.buffer.free |
Free size of the reader buffer (bytes) | Integer |
4096 |
netswitch.client.«name».reader.buffer.max |
Maximum size of the reader buffer (bytes) | Integer |
0 (unbounded) |
netswitch.client.«name».reader.buffer.min |
Minimum size of the reader buffer (bytes) | Integer |
8192 |
netswitch.client.«name».reader.buffer.warn |
Warning size of the reader buffer (bytes) | Integer |
8192 |
netswitch.client.«name».switch.uri |
Protocol, address and port of the NetSwitch server | String |
NETSWITCH |
netswitch.client.«name».tcp.nodelay |
Enable or disable the socket TCP_NODELAY option | Boolean |
true |
netswitch.client.«name».trace |
Trace inbound and outbound messages (for debugging) | Boolean |
false |
netswitch.client.«name».trust.store |
Path of the JKS trust store file used for SSL connections | String |
none |
netswitch.client.«name».user.name |
Username used to login the channel with the NetSwitch server | String |
none |
netswitch.client.«name».user.password |
Password used to login the channel with the NetSwitch server | String |
none |
netswitch.client.«name».writer.queue.size |
Maximum size of the writer queue (messages) | Integer |
64 |
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close this channel, relinquishing any underlying resources.void
Connects thisClientChannel
to a switch server using suppliedname
andnameOrUri
.void
Connects thisClientChannel
to a switch server using supplied arguments.void
connect
(String name, Properties properties) Connects thisClientChannel
to the switch server using suppliedname
andproperties
.void
disableMulticast
(String name) Disables the reception of multicast messages identified byname
.void
enableMulticast
(String name, MulticastListener listener) Enables the reception of multicast messages identified byname
.getName()
Returns the name of thisClientChannel
, ornull
if thisClientChannel
is not connected.Returns the name of the switch connected to thisClientChannel
, ornull
if thisClientChannel
is not connected.Returns the connection URI of thisClientChannel
, ornull
if thisClientChannel
is not connected.Returns the version of the switch connected to thisClientChannel
, ornull
if thisClientChannel
is not connected.Returns the unexpected reply listener of thisClientChannel
.Returns the implementation version of thisClientChannel
.protected void
handleInbound
(Message inbound) Handles an inbound message received by this connector.void
sendMessage
(Message message) Sends (writes) the supplied message to the output stream of a channel.void
sendMulticast
(String name, Serializable data) Sends an multicast message built using the suppliedname
anddata
.sendRequest
(String destination, Serializable data) Sends a request message to adestination
and then blocks forever waiting for the reply message.sendRequest
(String destination, Serializable data, int timeout) Sends a request message to adestination
and then blocks waiting for the reply message for at mosttimeout
milliseconds.void
setUnexpectedReplyListener
(ReplyListener listener) Changes the unexpected reply listener of thisClientChannel
.Methods inherited from class cl.netswitch.lib.aio.ChannelBase
connect, fine, getCompressionThreshold, getLocalAddress, getLogger, getLoggerHeader, getReaderBufferFreeSize, getReaderBufferMaxSize, getReaderBufferMinSize, getReaderBufferWarnSize, getReaderBytes, getReaderMessages, getRemoteAddress, getSoKeepAlive, getSslHandshakeTimeout, getTag, getTcpNoDelay, getTrustStoreFile, getWriterBytes, getWriterMessages, getWriterQueueSize, info, isClosed, isConnected, isLoggable, isSecure, isTracing, setCompressionThreshold, setLogger, setLoggerHeader, setReaderBufferFreeSize, setReaderBufferMaxSize, setReaderBufferMinSize, setReaderBufferWarnSize, setSoKeepAlive, setSslHandshakeTimeout, setTcpNoDelay, setTracing, setTrustStoreFile, setWriterQueueSize, severe, severe, warning, writeMessage, writeMessage
-
Constructor Details
-
ClientChannel
public ClientChannel()Constructs a newClientChannel
instance.
-
-
Method Details
-
getName
Returns the name of thisClientChannel
, ornull
if thisClientChannel
is not connected.- Returns:
- the name or
null
if not connected.
-
getNetSwitchName
Returns the name of the switch connected to thisClientChannel
, ornull
if thisClientChannel
is not connected.- Returns:
- the name or
null
if not connected.
-
getNetSwitchVersion
Returns the version of the switch connected to thisClientChannel
, ornull
if thisClientChannel
is not connected.- Returns:
- the version or
null
if not connected.
-
getNetSwitchURI
Returns the connection URI of thisClientChannel
, ornull
if thisClientChannel
is not connected.- Returns:
- the connection URI or
null
if not connected.
-
getVersion
Returns the implementation version of thisClientChannel
.- Returns:
- the implementation version of this
ClientChannel
.
-
getUnexpectedReplyListener
Returns the unexpected reply listener of thisClientChannel
.- Returns:
- the unexpected reply listener or
null
if none defined.
-
setUnexpectedReplyListener
Changes the unexpected reply listener of thisClientChannel
.- Parameters:
listener
- the new unexpected reply listener (can be null).
-
handleInbound
Handles an inbound message received by this connector.- Specified by:
handleInbound
in classChannelBase
- Parameters:
inbound
- the inbound message to be handled.- Throws:
NullPointerException
- ifinbound
isnull
.Exception
- if another error occurs.
-
sendMulticast
Sends an multicast message built using the suppliedname
anddata
. The message will be dispatched to all client and/or server applications that have enabled the reception of multicast messages with the specifiedname
.- Parameters:
name
- the name of the multicast message.data
- data to include in the multicast message (can be null).- Throws:
NullPointerException
- ifname
isnull
.Exception
- if another error occurs.
-
enableMulticast
Enables the reception of multicast messages identified byname
. Multicast messages are generated when particular event occurs (e.g., the price of a stock has reached a certain limit). When a multicast message arrives, thehandleMulticast
method of suppliedlistener
will be called.- Parameters:
name
- name of the multicast message.listener
- the listener of multicast messages.- Throws:
NullPointerException
- if an argument isnull
.Exception
- if another error occurs.
-
disableMulticast
Disables the reception of multicast messages identified byname
. Ifname
has the special value"*"
, then this method disables the reception of all multicast messages that have been enabled using theenableMulticast
method.- Parameters:
name
- name of the multicast messages or"*"
.- Throws:
NullPointerException
- ifname
isnull
.Exception
- if another error occurs.
-
sendRequest
Sends a request message to adestination
and then blocks forever waiting for the reply message.This convenience method produces the same result as the following invocation:
sendRequest(destination, data, 0);
- Parameters:
destination
- the request destinationswitch:server.service
.data
- the data to include in the request message (can be null).- Returns:
- the reply message returned from the switch server.
- Throws:
NullPointerException
- ifdestination
isnull
.Exception
- if another error occurs.
-
sendRequest
public Message sendRequest(String destination, Serializable data, int timeout) throws SocketTimeoutException, Exception Sends a request message to adestination
and then blocks waiting for the reply message for at mosttimeout
milliseconds. The value ofdestination
must have the following format:[SwitchName:]ServerName[.ServiceName]
where optional items are shown within"["
and"]"
brackets, and the characters":"
and"."
stand for themselves. If the optionalSwitchName
is not specified, the message is sent to the connected switch.ServerName
is the name of a server, a listener or a connector. The optionalServiceName
is the name of a service provided by the server, or the name of a channel of a listener. The following are examples of valid destinations:POS_DEVICE NETSWITCH.LOG_ERROR HUB:IMSER.ADD_CONTACT
Argumenttimeout
specifies the amount of time to wait for the synchronous reply message. Iftimeout
is zero (0), the call waits forever for the reply message.- Parameters:
destination
- the request destinationswitch:server.service
.data
- the data to include in the request message (can be null).timeout
- the maximum time to wait for a reply (in milliseconds).- Returns:
- the reply message returned from the switch server.
- Throws:
NullPointerException
- ifdestination
isnull
.IllegalArgumentException
- iftimeout
is negative.IllegalStateException
- if already waiting for a reply message.SocketTimeoutException
- iftimeout
expired while waiting for reply.Exception
- if another error occurs.
-
sendMessage
Sends (writes) the supplied message to the output stream of a channel.- Parameters:
message
- the message to be written.- Throws:
NullPointerException
- ifmessage
isnull
.IllegalStateException
- if the channel is not connected.Exception
- if another error occurs.
-
connect
Connects thisClientChannel
to the switch server using suppliedname
andproperties
. Thename
is used to identify this client channel with the switch. Theproperties
provide the connection and configuration parameters of the client.- Parameters:
name
- the name of this client channel.properties
- the properties of this client channel.- Throws:
NullPointerException
- if an argument isnull
.IllegalStateException
- if the client is already connected.Exception
- if another error occurs.
-
connect
public void connect(String name, String nameOrUri, String username, String password) throws Exception Connects thisClientChannel
to a switch server using supplied arguments. Thename
identifies thisClientChannel
with the switch server. ArgumentnameOrURI
specifies the protocol, address and listen port of the Switch. Argumentsusername
andpassword
provide the credentials of a switch user.- Parameters:
name
- the name of this client channel.nameOrUri
- an environment name or connection URI.username
- the name of a user of the switch server.password
- the password of a user of the switch server.- Throws:
NullPointerException
- if an argument isnull
.IllegalStateException
- if the client is already connected.Exception
- if another error occurs.
-
connect
Connects thisClientChannel
to a switch server using suppliedname
andnameOrUri
. Thename
identifies thisClientChannel
with the switch server. ArgumentnameOrURI
specifies the protocol, address and listen port of the switch server.- Parameters:
name
- the name of this client channel.nameOrUri
- an environment name or connection URI.- Throws:
NullPointerException
- if an argument isnull
.IllegalStateException
- if the client is already connected.Exception
- if another error occurs.
-
close
public void close()Close this channel, relinquishing any underlying resources.- Specified by:
close
in interfaceAutoCloseable
- Overrides:
close
in classChannelBase
-