如果使用相同的主机和端口创建多个TCP出站端点会发生什么,两个端点将使用相同的套接字,即连接或不同的连接。如何在mule测试流程中获取套接字对象。
我在这里使用2个具有相同地址和端口的端点,需要验证两者是否使用相同的连接或不同的连接。
<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>
答案 0 :(得分:0)
我知道这有点晚了,如果您的工作流程是同步的,您可以使用&#34;请求 - 响应&#34;在TCP端点中。如果您的工作流程需要异步并且仍然需要使用相同的套接字,那么 您可以通过在tcp-connector中为入站和出站tcp端点添加服务覆盖来实现此目的,并从端点引用这些连接器。
<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>
在receiver部分中,服务覆盖类应扩展TcpMessageReceiver类,您可以覆盖以下方法将客户端套接字添加到mule会话。
/* (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 );
}
现在您可以引用Outbound属性&#34; ClientSocket&#34;在您的muleflow中,使用调度程序将其传递回出站TCP。 在dispatcher部分中,您必须具有DispatcherFactory类的服务覆盖,该类应扩展AbstractMessageDispatcher类并覆盖doDispatch方法,如下所示:
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);
}