For a socket service previously created in execution time.
1 socket.stopServiceClass
<socket.stopServiceClass name='name'>
<Service Class /> +
</socket.stopServiceClass>
Attributes | |||||
---|---|---|---|---|---|
Name | Type | Required | Default | Description | |
Aname | string | Name of the service. |
Arguments | |||||
---|---|---|---|---|---|
Name | Type | Required | Unique | Nullable | Description |
EService Class | Java Class | Class which you want to add. |
Returns | |
---|---|
Type | Description |
stream | Array of bytes, response which is returned by the service before the request performed. |
Example
The following example shows how a socket service is created and started. To test itm a socket client which sends a message to the service is declared. The service prints on screen the response and before that the service stops.
Copy
<xsql-script name='dynamicall_add_socket_server'> <body> <socket.stopServiceClass name='echo' host='127.0.0.1' port='5555' backlog='5' so-rcvbuf='128' log-time='5'> public class echo extends NioService { private int READ_MESSAGE_WAIT_TIME = 10; private int WRITE_MESSAGE_WAIT_TIME = 10; @Override public void handleAcceptConnection(AsynchronousSocketChannel asyncSocketChannel) throws Exception { ByteBuffer messageByteBuffer = ByteBuffer.allocate(getMessageReadSize()); // read a message from the client, timeout after 10 seconds Future<Integer> futureReadResult = asyncSocketChannel.read(messageByteBuffer); futureReadResult.get(READ_MESSAGE_WAIT_TIME, TimeUnit.SECONDS); String clientMessage = new String(messageByteBuffer.array()).trim(); messageByteBuffer.clear(); messageByteBuffer.flip(); String responseString = "echo:" + clientMessage + "\n"; messageByteBuffer = ByteBuffer.wrap((responseString.getBytes())); Future<Integer> futureWriteResult = asyncSocketChannel.write(messageByteBuffer); futureWriteResult.get(WRITE_MESSAGE_WAIT_TIME, TimeUnit.SECONDS); if (messageByteBuffer.hasRemaining()) { messageByteBuffer.compact(); } else { messageByteBuffer.clear(); } } } </socket.addServiceClass> <!-- TEST CLIENT --> <!-- declare a socket to connecto to echo server --> <set name='m_socket'> <socket host='127.0.0.1' port='5555' timeout='2' /> </set> <!-- send a message to echo --> <println> SEND: <socket.out.println> <m_socket /> <string>Hello from client</string> </socket.out.println> </println> <!-- read response from echo --> <println> RECV: <socket.in.readLine> <m_socket /> </socket.in.readLine> </println> <!-- close the client socket --> <socket.close><m_socket/></socket.close> <!-- stop socket server --> <socket.stopServiceClass name='echo' /> </body> </xsql-script>