0

我無法在rabbitmq代理的「CustomerQ」隊列中發送消息。我通過spring啓動配置rabbitmq broker作爲嵌入式服務器。RabbitMQ嵌入式代理不是從彈簧啓動應用程序啓動

package com.testlab.chapter2; 

    import org.springframework.amqp.core.Queue; 
    import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.context.annotation.Bean; 
    import org.springframework.context.annotation.Lazy; 
    import org.springframework.stereotype.Component; 



    @Component 
    @Lazy 
class Sender { 

    RabbitMessagingTemplate template; 

    @Autowired 
    Sender(RabbitMessagingTemplate template){ 
    this.template = template; 
    } 

    @Bean 
    Queue queue() { 
    return new Queue("CustomerQ", false); 
    } 

    public void send(String message){ 
    System.out.println(template.getRabbitTemplate().getConnectionFactory()); 

    template.convertAndSend("CustomerQ", message); 
    } 
    } 

**application.properties file configuration:** 

    spring.rabbitmq.host=localhost 
    spring.rabbitmq.port=5672 
    spring.rabbitmq.username=guest 
    spring.rabbitmq.password=guest 

我得到以下錯誤,當代碼試圖連接/把任何消息隊列 錯誤:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.messaging.MessagingException: java.net.ConnectException: Connection refused: connect; nested exception is org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect] with root cause

java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_25] at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_25] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) ~[na:1.8.0_25] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_25] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_25] at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_25] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_25] at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_25]

我會感謝您對這個幫助。

回答

2

有沒有「嵌入式RabbitMQ經紀人」這樣的事情。

您必須單獨安裝並啓動它。它不是用Java編寫的,它是Erlang。

是什麼讓你相信Boot嵌入了一個經紀人?

+0

謝謝羅素。我認爲春季啓動也支持RabbitMQ。因爲我沒有意識到它是用java編寫的,所以我感覺很糟糕。我認爲spring引導支持下面提到的嵌入式activemq代理鏈接: [link](http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-messaging.html)。我的代碼工作正常,因爲我連接到rabbitmq外部代理。 – JavaSat

相關問題