3
votes

Can I send distinct messages (distinct serialize/deserialize) to the same tcp server (same host and port) and differentiate the tcp-inbound-gateway by some value of header or payload with a router???

(...)

I want to add a router for select the correct tcp-inbound-gateway depends on some field in header or payload (for example named typedata). In what order would enter the request (or message) between router, tcp-inbound-gateway, tcp-connection-factory, serialize/deserialize? Will I have problems with the serialization/deserialization for chose the tcp-inbound-gateway? What is the correct way to do this?

Thanks in advance.


EDIT

In server:

<!-- 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 & ResultTwo implements ResultBase. And in serialize of customSerializerDeserializer I have:

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

In client:

<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

1 Answers

0
votes

You cannot have 2 server connection factories listening on the same port. TCP doesn't allow it - the network stack wouldn't know which server socket to route the request to.

There's no problem on the client side but, with a single socket, the server would have to understand how to deserialize both data types.

It's probably easier to combine the serializers/deserializers into one on both sides (add another header to the message so the deserializer knows what type of payload to decode).