问题陈述:
Spring amqp-outbound网关从另一个线程产生回复(与jms-outbound网关一样,具有不同的队列,使用相关键关联请求/响应)。
无法将此消息与此示例相关联。
Spring集成
<int:gateway id="outboundGateway" service-interface="com.amqp.outbound.gateway.OutboundGateway"
default-reply-channel="defaultReplyChannel" >
<int:method name="process" request-channel="inboundRequestChannel"/>
</int:gateway>
<int:channel id="defaultReplyChannel"/>
<int:channel id="inboundRequestChannel"/>
<int:channel id="enrichedInboundRequestChannel"/>
<int:channel id="processAuthRequestChannel"/>
<int:channel id="postProcessorChannel"/>
<int:chain input-channel="inboundRequestChannel" output-channel="enrichedInboundRequestChannel">
<int:service-activator id="serviceActivator"
ref="ouboundService" method="createRequest"/>
</int:chain>
<int-amqp:outbound-gateway id="outboundGtwyId" header-mapper="headerMapper"
request-channel="enrichedInboundRequestChannel"
reply-channel="defaultReplyChannel"
amqp-template="template"
reply-timeout="30000"
exchange-name="request_exchange"
routing-key="request_exchange_queue"/>
<int-amqp:inbound-channel-adapter id="amqpMessageDriven" queue-names="request_queue"
connection-factory="rabbitConnectionFactory" channel="processAuthRequestChannel"/>
<int:service-activator id="serviceActivator"
ref="ouboundService" input-channel="processAuthRequestChannel" output-channel="postProcessorChannel"
method="processRequest"/>
<int-amqp:outbound-channel-adapter amqp-template="template" channel="postProcessorChannel"
header-mapper="headerMapper" exchange-name="reply_exchange" routing-key="reply_exchange_queue"/>
<bean id="headerMapper" class="org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper"/>
配置
@Bean
public RabbitTemplate template(ConnectionFactory rabbitConnectionFactory){
final RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory);
template.setQueue("reply_queue");
return template;
}
@Bean
public Binding binding(){
return BindingBuilder.bind(this.queue()).to(this.exchange()).with("request_exchange_queue");
}
@Bean
public DirectExchange exchange(){
return new DirectExchange("request_exchange");
}
@Bean
public Queue queue(){
return new Queue("request_queue", true, false, true);
}
@Bean
public Binding bindingReply(){
return BindingBuilder.bind(this.queue()).to(this.exchange()).with("reply_exchange_queue");
}
@Bean
public DirectExchange exchangeReply(){
return new DirectExchange("reply_exchange");
}
@Bean
public Queue replyQueue(){
return new Queue("reply_queue", true, false, true);
}
服务
@Service
public final class OuboundService {
public Message createRequest(String message){
System.out.println("Inside createRequest : "+ message);
final String transactionId = UUID.randomUUID().toString();
final Message builtMessage = MessageBuilder.withBody(message.getBytes())
.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
.setHeader(AmqpHeaders.CORRELATION_ID, transactionId)
.build();
return builtMessage;
}
public Message processRequest(Message message){
System.out.println("Inside process Request : "+ new String(message.getBody()));
System.out.println("Header values : "+message.getMessageProperties().getHeaders());
final Message result = MessageBuilder.withBody("Successful".getBytes()).copyProperties(message.getMessageProperties())
.copyHeaders(message.getMessageProperties().getHeaders()).build();
return result;
}
}
错误:
org.springframework.integration.handler.ReplyRequiredException:处理程序'outboundGtwyId'没有回复,其'requiresReply'属性设置为true。
GitHub源代码(已解决的解决方案)
https://github.com/kingkongprab/spring-amqp-outbound-gateway
答案 0 :(得分:0)
相关性也在Spring AMQP中完成。有关详细信息,请参阅其RabbitTemplate#sendAndRecevie()
。此外,Reference Manual还有关于此事的良好文档。
Spring与其AbstractAmqpOutboundEndpoint
和AmqpInboundGateway
实现的集成提供了开箱即用的请求 - 回复关联解决方案。如果您无法在服务器端使用AmqpInboundGateway
,则应确保correlationId
从收到的请求转移到要回复的回复。是的,您可以使用专用交换进行回复,这是RabbitTemplate#setQueue()
支持等待客户端,出站端的回复。但如果没有适当的correlation
转移,这仍然无法运作。另请参阅https://docs.spring.io/spring-integration/docs/4.3.12.RELEASE/reference/html/amqp.html#amqp-message-headers以获取有关如何在Spring Integration中映射标头(包括correlationId
)的信息。
<强>更新强>
感谢您分享您的申请。
好吧,现在我看到了几个问题:
您肯定错过了replyQueue
绑定:
@Bean
public Binding bindingReply(){
return BindingBuilder.bind(this.replyQueue()).to(this.exchangeReply()).with("reply_exchange_queue");
}
RabbitTemplate
必须使用setReplyAddress()
。您必须为MessageListenerContainer
配置reply_queue
并将RabbitTemplate
作为听众:
@Bean
public RabbitTemplate template(ConnectionFactory rabbitConnectionFactory){
final RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory);
template.setReplyAddress(replyQueue().getName());
return template;
}
@Bean
public MessageListenerContainer replyContainer(RabbitTemplate template) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(template.getConnectionFactory());
container.setQueues(replyQueue());
container.setMessageListener(template);
return container;
}
OuboundService
进行org.springframework.amqp.core.Message
操作无用。频道适配器不了解此类payload
,您的自定义Message
只是另一个body
的序列化org.springframework.amqp.core.Message
。我把它改成了这一切,一切运作良好:
public String createRequest(String message){
System.out.println("Inside createRequest : "+ message);
return message;
}
public Message processRequest(Message message){
System.out.println("Inside process Request : " + message);
return message;
}
无论如何,我建议你重新考虑你的设计,然后回到AmqpInboundGateway
。
correlation
。框架会自动为您完成。