Class ClientChannel
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close this channel, relinquishing any underlying resources.voidConnects thisClientChannelto a switch server using suppliednameandnameOrUri.voidConnects thisClientChannelto a switch server using supplied arguments.voidconnect(String name, Properties properties) Connects thisClientChannelto the switch server using suppliednameandproperties.voiddisableMulticast(String name) Disables the reception of multicast messages identified byname.voidenableMulticast(String name, MulticastListener listener) Enables the reception of multicast messages identified byname.getName()Returns the name of thisClientChannel, ornullif thisClientChannelis not connected.Returns the name of the switch connected to thisClientChannel, ornullif thisClientChannelis not connected.Returns the connection URI of thisClientChannel, ornullif thisClientChannelis not connected.Returns the version of the switch connected to thisClientChannel, ornullif thisClientChannelis not connected.Returns the unexpected reply listener of thisClientChannel.Returns the implementation version of thisClientChannel.protected voidhandleInbound(Message inbound) Handles an inbound message received by this connector.voidsendMessage(Message message) Sends (writes) the supplied message to the output stream of a channel.voidsendMulticast(String name, Serializable data) Sends an multicast message built using the suppliednameanddata.sendRequest(String destination, Serializable data) Sends a request message to adestinationand then blocks forever waiting for the reply message.sendRequest(String destination, Serializable data, int timeout) Sends a request message to adestinationand then blocks waiting for the reply message for at mosttimeoutmilliseconds.voidsetUnexpectedReplyListener(ReplyListener listener) Changes the unexpected reply listener of thisClientChannel.Methods inherited from class 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 newClientChannelinstance.
-
-
Method Details
-
getName
Returns the name of thisClientChannel, ornullif thisClientChannelis not connected.- Returns:
- the name or
nullif not connected.
-
getNetSwitchName
Returns the name of the switch connected to thisClientChannel, ornullif thisClientChannelis not connected.- Returns:
- the name or
nullif not connected.
-
getNetSwitchVersion
Returns the version of the switch connected to thisClientChannel, ornullif thisClientChannelis not connected.- Returns:
- the version or
nullif not connected.
-
getNetSwitchURI
Returns the connection URI of thisClientChannel, ornullif thisClientChannelis not connected.- Returns:
- the connection URI or
nullif 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
nullif 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:
handleInboundin classChannelBase- Parameters:
inbound- the inbound message to be handled.- Throws:
NullPointerException- ifinboundisnull.Exception- if another error occurs.
-
sendMulticast
Sends an multicast message built using the suppliednameanddata. 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- ifnameisnull.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, thehandleMulticastmethod of suppliedlistenerwill 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. Ifnamehas the special value"*", then this method disables the reception of all multicast messages that have been enabled using theenableMulticastmethod.- Parameters:
name- name of the multicast messages or"*".- Throws:
NullPointerException- ifnameisnull.Exception- if another error occurs.
-
sendRequest
Sends a request message to adestinationand 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- ifdestinationisnull.Exception- if another error occurs.
-
sendRequest
public Message sendRequest(String destination, Serializable data, int timeout) throws SocketTimeoutException, Exception Sends a request message to adestinationand then blocks waiting for the reply message for at mosttimeoutmilliseconds. The value ofdestinationmust have the following format:[SwitchName:]ServerName[.ServiceName]
where optional items are shown within"["and"]"brackets, and the characters":"and"."stand for themselves. If the optionalSwitchNameis not specified, the message is sent to the connected switch.ServerNameis the name of a server, a listener or a connector. The optionalServiceNameis 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
Argumenttimeoutspecifies the amount of time to wait for the synchronous reply message. Iftimeoutis 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- ifdestinationisnull.IllegalArgumentException- iftimeoutis negative.IllegalStateException- if already waiting for a reply message.SocketTimeoutException- iftimeoutexpired 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- ifmessageisnull.IllegalStateException- if the channel is not connected.Exception- if another error occurs.
-
connect
Connects thisClientChannelto the switch server using suppliednameandproperties. Thenameis used to identify this client channel with the switch. Thepropertiesprovide 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 thisClientChannelto a switch server using supplied arguments. Thenameidentifies thisClientChannelwith the switch server. ArgumentnameOrURIspecifies the protocol, address and listen port of the Switch. Argumentsusernameandpasswordprovide 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 thisClientChannelto a switch server using suppliednameandnameOrUri. Thenameidentifies thisClientChannelwith the switch server. ArgumentnameOrURIspecifies 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:
closein interfaceAutoCloseable- Overrides:
closein classChannelBase
-