rabbitmq和spring整合后发送消息失败,一直报java.util.concurrent.TimeoutException

梦蝶飘雪 发布于 2016/09/27 14:18
阅读 15K+
收藏 0

在做spring和rabbitmq整合后,一直不能发送和接受消息。可以确定的是,rabbitmq server配置没问题,因为,如果rabbitmq不和spring整合,rabbitmq可以正常的发送和接受消息。

我的配置如下和代码如下

spring-rabbitmq.xml配置如下

<?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:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/rabbit
    http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd" >


    <rabbit:connection-factory id="rabbitmqConnectionFactory"
                               username="${rmq.manager.user}"
                               password="${rmq.manager.password}"
                               host="${rmq.ip}"
                               port="${rmq.port}"
                               virtual-host="${rmq.vhost}" />


    <rabbit:admin connection-factory="rabbitmqConnectionFactory"/>


    <rabbit:template connection-factory="rabbitmqConnectionFactory" message-converter="jsonMessageConverter"
                     exchange="yangExchange" id="amqpTemplate" routing-key="yang.hello"  />


    <rabbit:queue id="hello" durable="true" auto-delete="false" exclusive="false" name="hello"/>


    <rabbit:topic-exchange name="yangExchange" id="yangExchange" durable="true">
            <rabbit:bindings>
                <rabbit:binding queue="hello" pattern="yang.*"/>
            </rabbit:bindings>
    </rabbit:topic-exchange>

    <!-- queue litener  观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象-->
    <rabbit:listener-container connection-factory="rabbitmqConnectionFactory" acknowledge="auto">
        <rabbit:listener queue-names="hello" method="onMessage" ref="rabbmitmqQueueListener"/>
    </rabbit:listener-container>

    <bean id="rabbmitmqQueueListener" class="com.autohome.statistics.mq.listener.RabbmitmqQueueListener" />
    <!-- spring amqp默认的是jackson 的一个插件,目的将生产者生产的数据转换为json存入消息队列,由于fastjson的速度快于jackson,这里替换为fastjson的一个实现 -->
    <bean id="jsonMessageConverter"  class="com.autohome.statistics.mq.util.FastJsonMessageConverter" />

</beans>


rabbitmq.properties配置如下

rmq.ip=192.168.1.20
rmq.producer.num=20
rmq.port=15672
rmq.manager.user=admin
rmq.manager.password=admin
rmq.vhost=/vhost1

消费者RabbmitmqQueueListener代码如下:

@Component("rabbmitmqQueueListener")
public class RabbmitmqQueueListener implements MessageListener {


    public void onMessage(Message message) {
        try {
            String msg = new String(message.getBody(), "utf-8");
            System.out.println(msg);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

生产者代码如下

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-mybatis.xml"})
public class TestMQ {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Test
    public void testProducer() throws InterruptedException {
        amqpTemplate.convertAndSend("queue_one_key,第一条测试");
        Thread.sleep(1000L);
    }
}

说明spring-mybatis.xml文件会加载spring-rabbitmq.xml和rabbitmq.properties文件。

报错信息如下

[DEBUG]-[Thread: http-bio-8090-exec-7]-[org.springframework.web.servlet.FrameworkServlet.processRequest()]: Could not complete request
java.util.concurrent.TimeoutException
at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:77)
at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:111)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:37)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:367)
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:293)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:678)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:739)
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:191)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:371)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils$1.createConnection(ConnectionFactoryUtils.java:80)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.doGetTransactionalResourceHolder(ConnectionFactoryUtils.java:130)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.getTransactionalResourceHolder(ConnectionFactoryUtils.java:67)
at org.springframework.amqp.rabbit.connection.RabbitAccessor.getTransactionalResourceHolder(RabbitAccessor.java:106)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:828)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:822)
at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:377)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:417)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:389)
at com.autohome.statistics.controller.RabbitmqController.testRabbitmq(RabbitmqController.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

求大神们指教哪里的问题

加载中
0
slowrookie
slowrookie
我也出现了这个问题,但我使用的是docker rabbitmq,由于没有映射5672端口,无法访问。

原因:是访问port不是15672,15672是api和管理界面的port.
解决:1.检查5672端口是否能访问
   2.将15672更换为5672

返回顶部
顶部