由于spring amqp引用doc说“当回复超时(replyTimeout)时,sendAndReceive()方法返回null”。 据我所知,replyTimeout的默认值是5秒。
所以,我正在做的是打开100个线程。每个线程使用sendAndReceive操作将消息发送到一个队列。 RabbitListener监听该队列,接收消息并发送回复。
有些线程在5秒之前得到null。据我所知,RabbitTemplate首先必须等待5秒,如果没有收到回复,则返回null。请帮帮我,我做错了什么?
上下文:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<context:component-scan base-package="ru.cib" />
<rabbit:annotation-driven />
<rabbit:connection-factory id="connFactory" host="localhost" username="guest" password="guest" channel-cache-size="10" />
<rabbit:admin id="admin" connection-factory="connFactory" />
<rabbit:queue name="requestQueue" />
<rabbit:direct-exchange name="directExchange">
<rabbit:bindings>
<rabbit:binding key="directRequestKey" queue="requestQueue" />
</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:template id="requestTemplate" connection-factory="connFactory" channel-transacted="true" reply-timeout="10000" />
<bean id="rabbitTxManager" class="org.springframework.amqp.rabbit.transaction.RabbitTransactionManager">
<property name="connectionFactory" ref="connFactory" />
</bean>
<bean id="rabbitListenerContainerFactory" class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
<property name="connectionFactory" ref="connFactory" />
<property name="concurrentConsumers" value="10" />
<property name="maxConcurrentConsumers" value="20" />
<property name="transactionManager" ref="rabbitTxManager" />
<property name="channelTransacted" value="true" />
</bean>
</beans>
方法:
for (int i = 1; i <= 100; i++) {
pool.submit(new RunnableImpl(i));
}
public void run() {
LOG.info("thread send request {}", i);
Integer resp = (Integer) requestTemplate.convertSendAndReceive("directExchange", "directRequestKey", i);
if (resp == null) {
LOG.error("thread get null for request {}!!!", i);
} else {
LOG.info("thread get response {}", resp);
}
}
@RabbitListener(queues = "requestQueue")
public int receivedRequestMessage(int data, Message request) throws Exception {
LOG.info("receive request {}, {}", data, request);
LOG.info("sleep for 2 seconds");
Thread.sleep(2000);
int reply = data * 100;
LOG.info("sending response {}", reply);
return reply;
}
答案 0 :(得分:0)
我的错误。 sendAndReceive操作效果很好:等待replyTimeout属性中指定的一段时间,然后尝试接收答案。
我对自己的日志感到困惑......