Rabbitmq 使用spring集成和rabbit mq编写示例生产者和消费者
我尝试使用SpringIntegration和RabbitMQ编写示例producer&consumer 这里是我的rabbit.xml配置文件:Rabbitmq 使用spring集成和rabbit mq编写示例生产者和消费者,rabbitmq,spring-integration,Rabbitmq,Spring Integration,我尝试使用SpringIntegration和RabbitMQ编写示例producer&consumer 这里是我的rabbit.xml配置文件: <rabbit:connection-factory id="connectionFactory" /> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" /> <rabbit:admin connection-factor
<rabbit:connection-factory id="connectionFactory" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="queue.request" />
<rabbit:queue name="queue.response" />
<rabbit:direct-exchange name="exchange.main">
<rabbit:bindings>
<rabbit:binding queue="queue.request" key="binding.queue.request"/>
<rabbit:binding queue="queue.response" key="binding.queue.response" />
</rabbit:bindings>
</rabbit:direct-exchange>
这是我的界面
public interface BaseGateway {
@Gateway
public void FireForget(CallMethodMessage method);
@Gateway
public ReturnModel SendWait(CallMethodMessage method);
}
好的,现在我单独运行consumer,然后对于第一个,我使用方法FireForget运行producer,一切正常,cosumer获取它并打印消息,但是当我使用SendWait方法运行时,cosumer获取它但不返回任何内容,producer等待得到响应,如何处理此问题?回复没有相关性
<int-amqp:outbound-channel-adapter channel="request" amqp-template="amqpTemplate"
exchange-name="exchange.main" routing-key="binding.queue.request"
<int-amqp:inbound-channel-adapter channel="response" queue-names="queue.response"
connection-factory="connectionFactory" />
我更改了我的代码,并使用了您提到的出站和入站网关,当生产者和消费者之间传递的对象是String或int之类的基元时,一切正常,但当我尝试传递POJO类时,我得到了以下错误:无法转换byte[[]类型的值您需要一个合适的MessageConverter
;该框架提供了一个方便的JsonMessageConverter
,它使用Jackson将POJO转换为Json或从Json转换为POJO,只要POJO对Jackson友好。默认转换器(SimpleMessageConverter
)处理字符串。字节[]和Java序列化。Java序列化需要可序列化的POJO,否则将发送空消息。因此,使用JsonMessageSerializer
或使您的POJO可序列化(但JSON通常被认为是AMQP的更好选择,因为它对多语言友好)。
@Autowired
BaseGateway baseGateway;
@Test
public void testHelloWorld() {
CallMethodMessage callMethodMessage = new CallMethodMessage();
callMethodMessage.setMethod("test");
callMethodMessage.setArgs(null);
System.out.print("send & receive ");
ReturnModel returnModel = this.baseGateway.SendWait(callMethodMessage);
//this.baseGateway.FireForget(callMethodMessage);
}
public interface BaseGateway {
@Gateway
public void FireForget(CallMethodMessage method);
@Gateway
public ReturnModel SendWait(CallMethodMessage method);
}
<int-amqp:outbound-channel-adapter channel="request" amqp-template="amqpTemplate"
exchange-name="exchange.main" routing-key="binding.queue.request"
<int-amqp:inbound-channel-adapter channel="response" queue-names="queue.response"
connection-factory="connectionFactory" />