我正在学习 Spring-Integration,并对 Gateway 和 Service-Activators 有基本的了解。我喜欢网关的概念。Spring Integration 在运行时为网关生成代理。此代理对网关的消费者隐藏所有消息传递细节。此外,生成的代理也可能关联请求和回复。
出于学习的目的,我开始使用原始 Spring Integration 功能而不是使用网关来实现请求和回复相关性。我可以在请求标头中设置相关标识符,但在接收通道回复时无法指定相关标识符。以下(在问题的末尾)是相同的代码片段。此外,相关内容如何针对消息代理(例如 RabbitMQ)起作用?RabbitMQ 是否提供了检索带有特定标头(相关标识符)的消息的能力?
public class RemoteProxyCalculatorService implements CalculatorService
{
public int Square(int n)
{
UUID uuid = SendRequest(n, "squareRequestChannel");
int squareOfn = ReceiveReply("squareReplyChannel", uuid);
return squareOfn;
}
private <T> UUID SendRequest(T payload, String requestChannel)
{
UUID requestID = UUID.randomUUID();
Message<T> inputMessage = MessageBuilder.withPayload(payload)
.setCorrelationId(requestID)
.build();
MessageChannel channel = (MessageChannel)context.getBean(requestChannel, MessageChannel.class);
channel.send(inputMessage);
return requestID;
}
@SuppressWarnings("unchecked")
private <T> T ReceiveReply(String replyChannel, UUID requestID)
{
//How to consume requestID so as to receive only the reply related to the request posted by this thread
PollableChannel channel = (PollableChannel)context.getBean(replyChannel);
Message<?> groupMessage = channel.receive();
return (T)groupMessage.getPayload();
}
private ClassPathXmlApplicationContext context;
}
谢谢。