混合tcp-connection-factory + tcp-inbound-gateway + router

时间:2015-07-07 22:03:39

标签: java spring-integration

我是否可以向同一个tcp服务器(相同的主机和端口)发送不同的消息(不同的序列化/反序列化),并使用路由器将tcp-inbound-gateway区分为某个头或有效负载值???

(...)

我想添加路由器以选择正确的tcp-inbound-gateway取决于标头或有效负载中的某些字段(例如名为typedata)。以什么顺序在路由器,tcp-inbound-gateway,tcp-connection-factory,serialize / deserialize之间输入请求(或消息)?选择tcp-inbound-gateway时,序列化/反序列化是否会出现问题?这样做的正确方法是什么?

提前致谢。

修改

在服务器中:

<!-- Common -->
<int:channel id="channelConnectionFactoryRequest" />
<int:channel id="channelConnectionFactoryResponse" />

<router method="getDestinationChannel"
    input-channel="channelSConnectionFactoryRequest">
        <beans:bean class="org.mbracero.integration.CustomRouter" />
</router>

<beans:bean id="customSerializerDeserializer"
    class="org.mbracero.integration.serialization.CustomSerializerDeserializer" />

<int-ip:tcp-connection-factory id="customConnectionFactory"
    type="server" port="${payment.port}" single-use="true"
    serializer="customSerializerDeserializer"
    deserializer="customSerializerDeserializer" />

<int-ip:tcp-inbound-gateway id="customInboundGateway"
    connection-factory="customConnectionFactory"
    request-channel="channelCustomConnectionFactoryRequest"
    reply-channel="channelCustomConnectionFactoryResponse"
    error-channel="errorChannel" />

<!-- Custom -->

<beans:bean id="operations"
    class="org.mbracero.integration.applepay.impl.OperationsImpl" />

<!-- Operation One -->
<int:channel id="operationOneRequest" />

<int:service-activator input-channel="operationOneRequest"
    output-channel="operationOneResponse" ref="operations" method="getOperationOne" />

<!-- Operation Two -->
<int:channel id="operationTwoRequest" />

<int:service-activator input-channel="operationTwoRequest"
    output-channel="operationTwoResponse" ref="operations" method="getOperationTwo" />

OperationsImpl:

ResultOne getOperationOne(RequestOne request);

ResultTwo getOperationOne(RequestTwo request);

ResultOne&amp; ResultTwo实现ResultBase。在serialSerializerDeserializer的序列化中,我有:

@Override
public void serialize(ResultBase arg0, OutputStream arg1) throws IOException {

    byte[] xxx = XXX.getBytes();
    arg1.write(xxx);

    byte[] yyy = yyy.getBytes();
    arg1.write(senderName);

    // **Each custom object have a method for serialize their own data**
    arg0.transformDataToByte(arg1);

    arg1.flush();
}

在客户端:

<gateway id="tmGateway"
    service-interface="org.mbracero.integration.CustomGateway" />

<beans:bean id="operationOneSerializerDeserializer"
    class="org.mbracero.integration.serialization.OperationOneSerializerDeserializer" />

<int-ip:tcp-connection-factory id="operationOneFactory"
    type="client" host="127.0.0.1" port="7878" single-use="true"
    serializer="operationOneSerializerDeserializer" deserializer="operationOneSerializerDeserializer" />

<int-ip:tcp-outbound-gateway id="operationOneOutGateway"
    request-channel="operationOneChannel" connection-factory="operationOneFactory"
    request-timeout="5000" reply-timeout="5000" remote-timeout="5000" />


<beans:bean id="operationTwoSerializerDeserializer"
    class="org.mbracero.integration.operationTwoRequestSerializerDeserializer"/>

<int-ip:tcp-connection-factory id="operationTwoFactory"
    type="client" host="127.0.0.1" port="7878" single-use="true"
    serializer="operationTwoSerializerDeserializer"
    deserializer="operationTwoSerializerDeserializer" />

<int-ip:tcp-outbound-gateway id="operationTwoOutGateway"
    request-channel="operationTwoChannel" connection-factory="operationTwoFactory"
    request-timeout="50000" reply-timeout="50000" remote-timeout="50000" />

CustomGateway:

@Gateway(requestChannel="operationOneChannel")
OperationOneResponse sendOperationOne(OperationOneRequest request);

@Gateway(requestChannel="operationTwoChannel")
OperationTwoResponse sendOperationTwo(OperationTwo request);

1 个答案:

答案 0 :(得分:0)

您不能在同一端口上侦听2个服务器连接工厂。 TCP不允许 - 网络堆栈不知道将请求路由到哪个服务器套接字。

客户端没有问题但是,使用单个套接字,服务器必须了解如何反序列化这两种数据类型。

将序列化器/解串器组合成两侧的一个可能更容易(在消息中添加另一个头,以便解串器知道要解码的有效负载类型)。