【发布时间】:2016-12-04 12:19:12
【问题描述】:
我是 Spring 集成的新手,我的要求是我将收到一个对我的 Spring 集成的 http 请求,然后我需要按照以下步骤操作。
1) 获取 Http 请求 2)发送给第三方(MQ) 3) 将收到来自 MQ 的响应 4) 处理消息并发送回客户端。
我已经完成了这个应用程序并且工作正常,但是当我同时收到多个请求时,我面临的问题是,在向 Thridaprty 发送请求时,我在我的标头中构建了一些 UUID 和 correlID 并保存在我的本地缓存,因此如果多个请求同时出现,我的代码将创建相同的 correlID。
我怀疑在同一毫秒内有多个请求存在问题。
请指导我出了什么问题。
这是我的配置,适用于单个请求。
<int-http:inbound-gateway id="requestGateway"
supported-methods="GET"
request-channel="requestChannel"
reply-channel="replyChannel"
path="${cuteRequestURL}"
reply-timeout="${cuteRequestTimeout}"
>
</int-http:inbound-gateway>
<int:channel id="requestChannel"/>
<bean id="mapToSI" class="aero.sita.fw.cutelvihttp.helper.TransformLinkedMultiValueMapToSIMsg"/>
<int:transformer id="abc"
input-channel="requestChannel"
output-channel="Map"
ref="mapToSI"
method="transformLinkeMapToSpringIntegrationMessage" />
<int:channel id="Map"/>
<bean id="constructRequiredParams" class="SomeParamLogic">
<constructor-arg name="Value1" value="A" />
<constructor-arg name="Value2" value="B" />
<constructor-arg name="Value3" value="C" />
</bean>
<int:transformer id="constructMessageHeaderVendor"
input-channel="Map"
output-channel="inputRequestChannel"
ref="constructRequiredParams"
method="amend" />
<int:channel id="inputRequestChannel" />
<int:service-activator input-channel="inputRequestChannel"
output-channel="responseValidatorChannel"
method="xyz"
ref="serviceActivator"/>
<int:channel id="responseValidatorChannel"/>
<bean id="serviceActivator" class="ServiceActivator"/>
<int:channel id="replyChannel"/>
<int:channel id="dataChannel" />
<int:service-activator input-channel="dataChannel"
ref="destinationQueue"
method="transform"/>
<bean id="destinationQueue" class="XYZ">
<property name="requestQueue" value="ProducerName" />
<property name="replyQueue" value="Replier" />
</bean>
<int-xml:validating-filter discard-channel="errorChannel"
id="jmsInValidator"
input-channel="dataChannel"
output-channel="nullChannel"
schema-location="ThirdParty.xsd"
schema-type="xml-schema"
throw-exception-on-rejection="true" />
<int-xml:validating-filter discard-channel="errorChannel"
id="jmsInValidator"
input-channel="responseValidatorChannel"
output-channel="replyChannel"
schema-location="classpath:Client.xsd"
schema-type="xml-schema"
throw-exception-on-rejection="true" />
【问题讨论】:
-
不确定为什么要显示集成配置以确认您自己的代码中的竞争条件生成了该 uuid 和相关性。我认为你应该展示那个。甚至更好的是编写一些测试用例来检测这种竞争条件。您应该考虑使用现有的 uuid 算法来避免冲突。还有
latch屏障来保护资源免受并发访问。任何方式的问题看起来都与 Spring 集成相去甚远 -
感谢 Artem Bilan 的回复。
-
感谢 Artem Bilan 的回复。让我重新提出我的问题,Spring 集成是线程安全的吗?一旦我收到响应发送,我正在使用服务激活器来监听第三方响应(请求/回复)返回 HTTP 客户端。 HTTP - >我的应用程序是同步的,我的应用程序 - > MQ Applicaiotn是Aysnchrouns,所以服务激活器将在这里维护线程安全,这就是我想问的,请注意,根据你的建议,我改变了UUID的创建它的工作正常问题已解决,现在我想了解线程安全
-
是的。由于大多数 Spring Integration 组件都是无状态的,因此没有任何线程冲突。只有聚合器与
MesageStore有某种状态,但它经过了很好的测试。所以,试着在你自己的代码中找到一个瓶颈 -
感谢您确认 SI 框架无状态