I know that this is a bit late, If your workflow is synchronous, you can use "request-response" in the TCP endpoint. If your workflow needs to be asynchronous and still need to use the same socket,then
you can achieve this by adding a service override in the tcp-connector for inbound and outbound tcp endpoint and reference these connectors from your endpoint.
<tcp:connector name="TCP_Inbound" doc:name="TCP connector"
clientSoTimeout="${client.so.timeout}" receiveBacklog="0"
receiveBufferSize="0" sendBufferSize="0"
serverSoTimeout="${server.so.timeout}" socketSoLinger="0"
validateConnections="true" keepAlive="true">
<reconnect-forever />
<service-overrides messageReceiver="custom.TCPMessageReceiver" />
</tcp:connector>
<tcp:connector name="TCP_Outbound" doc:name="TCP connector"
clientSoTimeout="${client.so.timeout}" receiveBacklog="0"
receiveBufferSize="0" sendBufferSize="0"
serverSoTimeout="${server.so.timeout}" socketSoLinger="0"
validateConnections="true" keepAlive="true">
<reconnect-forever />
<service-overrides dispatcherFactory="custom.TcpDispatcherFactory" />
</tcp:connector>
In the receiver section, the service override class should extend the TcpMessageReceiver class and you can override the below method to add the client socket to the mule session.
/* (non-Javadoc)
* @see org.mule.transport.tcp.TcpMessageReceiver.TcpWorker#preRouteMuleMessage(org.mule.api.MuleMessage)
* This class shares the socket object to be used by the outbound endpoint.
*/
@Override
protected void preRouteMuleMessage(final MuleMessage message) throws Exception
{
super.preRouteMuleMessage(message);
final SocketAddress clientAddress = socket.getRemoteSocketAddress();
if (clientAddress != null)
{
message.setOutboundProperty(MuleProperties.MULE_REMOTE_CLIENT_ADDRESS, clientAddress.toString());
}
message.setOutboundProperty("ClientSocket", socket );
}
Now you can reference the Outbound property "ClientSocket" in your muleflow and pass it back to the outbound TCP using the dispatcher.
In the dispatcher section, you have to have a service-override for the DispatcherFactory class which should extend the AbstractMessageDispatcher class and override the doDispatch method as below:
protected synchronized void doDispatch(MuleEvent event) throws Exception
{
/* Get the socket shared from the inbound endpoint receiver class */
Socket socket = (Socket) event.getMessage().getSessionProperty("Socket");
dispatchToSocket(socket,event);
}