Spring integration Spring集成桥-请求头
我有一个SI应用程序,它使用桥接器桥接一些通道。我希望通过桥复制请求头。调试应用程序时,我发现BridgeHandler有一个方法将copyRequestHeaders()设置为false。 公共类BridgeHandler扩展了AbstractReplyProducingMessageHandler{Spring integration Spring集成桥-请求头,spring-integration,bridge,request-headers,Spring Integration,Bridge,Request Headers,我有一个SI应用程序,它使用桥接器桥接一些通道。我希望通过桥复制请求头。调试应用程序时,我发现BridgeHandler有一个方法将copyRequestHeaders()设置为false。 公共类BridgeHandler扩展了AbstractReplyProducingMessageHandler{ @Override public String getComponentType() { return "bridge"; } @Override protected Object h
@Override
public String getComponentType() {
return "bridge";
}
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return requestMessage;
}
@Override
protected boolean shouldCopyRequestHeaders() {
return false;
}
}
如果您注意到,在rabbit.xml中设置的GUID头不会传播到router.xml,因此日志的GUID为空。如果没有网桥,则会打印GUID。您不需要这样做。
基于BridgeHandler
这一逻辑非常透明:
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return requestMessage;
}
由于我们的输出
是消息
,并且我们没有copyRequestHeaders()
,因此没有任何更改,一切都对您有利
我想知道是什么让你带着这样的问题来找我们,因为根本没有问题
confusedMy bridge是从ChannelA到ChannelB的。当消息到达ChannelA时,我会进行一个标头enricher并添加一个新标头。当消息通过上述createOutputMessage()方法到达ChannelB时,我在请求标头中看不到新标头,我的假设是shouldCopyRequestHeaders()不是为我复制的。如果我错了,请纠正我。好的!共享,请从我们这边播放整个测试用例。这真的不可能…中间可能有其他内容。我添加了一些示例代码。如果有任何问题,请告诉我。在createOutputMessage()中已粘贴的代码示例,仅当(!this.shouldCopyRequestHeaders())时,消息才会按原样发送到输出通道由于该方法默认返回'false',因此始终会生成新消息,这会释放消息头。这是我的理解。因为代码太多,所以这没有多大帮助。我看到您有多个订阅,例如
RabbininboundChannel
,以及routerInputChannel
。因为它们是Direct通道
s通过循环方式将消息处理到目标订阅方。这可能会使您感到困惑,因为有时消息不会发送到正确的组件。我猜在您的情况下是标头enricher。如果(!this.shouldCopyRequestHeaders()){return(message)output;}我遗漏了什么?您在本条款中看到了什么新信息?
<int:channel id="rabbitInboundChannel" />
<rabbit:connection-factory id="amqpConnectionFactoryInbound"
host="${rabbit.host}" port="${rabbit.port}"
username="${rabbit.username}" password="${rabbit.password}" channel-cache-
size="${rabbit.channelCacheSize}"
connection-factory="rabbitConnectionFactoryInbound" />
<beans:bean id="rabbitConnectionFactoryInbound"
class="com.rabbitmq.client.ConnectionFactory">
<beans:property name="requestedHeartbeat"
value="${rabbit.requestedHeartBeat}" />
</beans:bean>
<!-- Inbound Adapter to AMQP RabbitMq -->
<int-amqp:inbound-channel-adapter id="rabbitMQInboundChannelAdapter"
channel="rabbitInboundChannel" concurrent-
consumers="${rabbit.concurrentConsumers}" task-executor="rabbit-
executor" connection-factory="amqpConnectionFactoryInbound"
message-converter="byteArrayToStringConverter" queue-
names="${rabbit.queue}" error-channel="errorChannelId"
prefetch-count="${rabbit.prefetchCount}" />
<header-enricher input-channel="rabbitInboundChannel" output-
channel="rabbitLoggingChannel">
<int:header name="Operation" value="${operation.rabbit}" overwrite="true" />
**<int:header name="GUID" expression="#{ 'T(java.util.UUID).randomUUID().toString()' }" />**
<int:header name="operationStartTime" expression="#{ 'T(java.lang.System).currentTimeMillis()' }" />
</header-enricher>
<int:channel id="rabbitLoggingChannel">
<int:interceptors>
<int:wire-tap channel="loggerChannel" />
</int:interceptors>
</int:channel>
<task:executor id="rabbit-executor" rejection-policy="CALLER_RUNS" pool-
size="${rabbit.poolSize}" queue-capacity="${rabbit.queueSize}" />
<int:channel id="routerInputChannel" />
<int:header-enricher input-channel="routerInputChannel" output-
channel="routerLoggingChannel">
<int:header name="Operation" value="${operation.router}" overwrite="true"
/>
<int:header name="file_name" expression="headers['GUID'] + '.xml'" />
<int:header name="operationStartTime" expression="#{
'T(java.lang.System).currentTimeMillis()' }" overwrite="true" />
<int:error-channel ref="errorChannelId" />
</int:header-enricher>
<int:recipient-list-router id="recipientListRouter" input-
channel="routerInputChannel">
<int:recipient channel="filePersistChannel" selector-expression="new
String(payload).length()>0" />
<int:recipient channel="transformerInputChannel" />
</int:recipient-list-router>
<int:channel id="routerLoggingChannel">
<int:interceptors>
<int:wire-tap channel="loggerChannel" />
</int:interceptors>
</int:channel>
2017-04-24 13:26:33,360 [rabbit-executor-4] INFO
Operation="RABBIT_INBOUND_ADAPTER" Status="Success" DurationMs="0"
GUID="8d5c67c8-a0fb-4a7e-99dc-f545159dde7e"
2017-04-24 13:26:33,361 [rabbit-executor-4] INFO Operation="ROUTER"
Status="Success" DurationMs="0" GUID=" "
2017-04-24 13:26:33,364 [rabbit-executor-4] INFO Operation="FILE_PERSISTER"
Status="Success" DurationMs="3" GUID=" "
2017-04-24 13:26:33,381 [rabbit-executor-5] INFO
Operation="ORDER_EVENT_TRANSFORMER" Status="Success" DurationMs="27"
GUID=" "
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return requestMessage;
}
protected Message<?> createOutputMessage(Object output, MessageHeaders requestHeaders) {
AbstractIntegrationMessageBuilder<?> builder = null;
if (output instanceof Message<?>) {
if (!this.shouldCopyRequestHeaders()) {
return (Message<?>) output;
}
builder = this.getMessageBuilderFactory().fromMessage((Message<?>) output);
}
else if (output instanceof AbstractIntegrationMessageBuilder) {
builder = (AbstractIntegrationMessageBuilder<?>) output;
}
else {
builder = this.getMessageBuilderFactory().withPayload(output);
}
if (this.shouldCopyRequestHeaders()) {
builder.copyHeadersIfAbsent(requestHeaders);
}
return builder.build();
}