gpt4 book ai didi

java - 混合 tcp-connection-factory + tcp-inbound-gateway + 路由器

转载 作者:行者123 更新时间:2023-12-01 07:56:58 26 4
gpt4 key购买 nike

我可以将不同的消息(不同的序列化/反序列化)发送到同一个 tcp 服务器(相同的主机和端口)并通过路由器的 header 或负载的某些值来区分 tcp-inbound-gateway 吗???

(...)

我想添加一个路由器来选择正确的 tcp-inbound-gateway 取决于 header 或有效负载中的某些字段(例如命名类型数据)。在路由器、tcp-inbound-gateway、tcp-connection-factory、序列化/反序列化之间以什么顺序输入请求(或消息)?我选择 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" />

操作实现:

ResultOne getOperationOne(RequestOne request);

ResultTwo getOperationOne(RequestTwo request);

ResultOne 和 ResultTwo 实现了 ResultBase。在 customSerializerDeserializer 的序列化中,我有:

@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" />

自定义网关:

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

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

最佳答案

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

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

将序列化器/反序列化器合并为一个可能更容易(在消息中添加另一个 header ,以便反序列化器知道要解码的有效负载类型)。

关于java - 混合 tcp-connection-factory + tcp-inbound-gateway + 路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31280138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com