0
votes

what happens if more then one TCP outbound endpoint is created with same host and port, both the endpoints will use same socket i.e. connection or different connection. How to get socket object in mule test flow.

I am using here 2 endpoints with same address and port, need to verify that both use same connection or different connection.

<flow name="testOutBoundTCP">
     <inbound-endpoint ref="outer" />
     <outbound-endpoint ref = "clientEndpoint2"/>
     <logger message="2 : #[message.payload]" category="INFO"></logger>
     <test:component appendString=" Received" logMessageDetails="true"/>
    </flow>

    <flow name="testOutBoundTCP1">
     <inbound-endpoint ref="outer1" />
     <outbound-endpoint ref = "clientEndpoint"/>
     <logger message="1 : #[message.payload]" category="INFO"></logger>
     <test:component appendString=" Received" logMessageDetails="true" />
    </flow>


    <flow name="testComponent">
        <inbound-endpoint ref="clientEndpoint" />
        <test:component/>
    </flow>
1

1 Answers

0
votes

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);
    }