Java Spring amqp-更新的调用侦听器

Java Spring amqp-更新的调用侦听器,java,spring,rabbitmq,amqp,spring-rabbit,Java,Spring,Rabbitmq,Amqp,Spring Rabbit,对不起,我是amqp领域的新手。我尝试编写简单的应用程序: public class HelloApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); final TestSender bean = context.getBean(TestSender.class);

对不起,我是
amqp
领域的新手。我尝试编写简单的应用程序:

public class HelloApp {

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

    final TestSender bean = context.getBean(TestSender.class);
    bean.sendMessage();

    }
}



@Component
public class TestSender {

    @Autowired
    private RabbitTemplate template;

    public void sendMessage() {

        final Message message = new Message("Sth".getBytes(), new MessageProperties());

        template.send(message);
        System.out.println("Was sent");
    }

}


@Component
public class MessageReceiver implements ChannelAwareMessageListener{

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {

        System.out.println("RECEIVE "+message.getBody().toString());

    }
.xml
文件:

spring amqp.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.xsd http://www.springframework.org/schema/rabbit        http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <rabbit:connection-factory id="connectionFactory" host="localhost"/>
    <rabbit:admin connection-factory="connectionFactory"/>

    <rabbit:template connection-factory="connectionFactory" id="rabbitTemplate" channel-transacted="true"/>
    <rabbit:queue name="userMesssageQueue" />

    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener ref="lis" queue-names="userMesssageQueue"/>
    </rabbit:listener-container>

    <bean id="transactionManager" class="org.springframework.amqp.rabbit.transaction.RabbitTransactionManager">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>
    <bean id="lis" class="foo.bar.MessageReceiver"/>
<?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"
       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">
    <context:annotation-config />
    <context:component-scan base-package="foo.bar"/>


    <import resource="spring-amqp.xml"/>
</beans>
但我想看到:

Was sent
RECEIVE ...

有什么不对???

您的问题其实是关于AMQP的知识水平较低。您应该了解什么是
交换
路由键
绑定

您需要配置:

<rabbit:direct-exchange name="myExchange">
    <rabbit:bindings>
         <rabbit:binding queue="userMesssageQueue" key="userMesssage" />
    </rabbit:bindings>
</rabbit:direct-exchange>
只有在这种情况下,您的邮件才会被放入
usermessagequeue

默认情况下,
rabbitmplate
将空字符串用于
exchange
(默认值),将空字符串用于
routingKey
。由于您的
usermessagequeue
没有使用该路由密钥绑定到该交换,因此您的侦听器不会收到它

发送工作没有错误,因为消息被放置到交换机上,对于生产者(发送者)来说就足够了

最后,您的消息被丢弃在RabbitMQ代理上,因为没有绑定到
“”
路由密钥的队列


请阅读RabbitMQ站点和Spring AMQP上的更多文档。

您这里的问题实际上与AMQP的知识水平低下有关。您应该了解什么是
交换
路由键
绑定

您需要配置:

<rabbit:direct-exchange name="myExchange">
    <rabbit:bindings>
         <rabbit:binding queue="userMesssageQueue" key="userMesssage" />
    </rabbit:bindings>
</rabbit:direct-exchange>
只有在这种情况下,您的邮件才会被放入
usermessagequeue

默认情况下,
rabbitmplate
将空字符串用于
exchange
(默认值),将空字符串用于
routingKey
。由于您的
usermessagequeue
没有使用该路由密钥绑定到该交换,因此您的侦听器不会收到它

发送工作没有错误,因为消息被放置到交换机上,对于生产者(发送者)来说就足够了

最后,您的消息被丢弃在RabbitMQ代理上,因为没有绑定到
“”
路由密钥的队列


请阅读RabbitMQ站点和Spring AMQP上的更多文档。

谢谢,我几个小时前开始学习RabbitMQ和Spring AMQP。我认为学习东西最好的方法就是通过例子和自己的错误来学习。谢谢,我几个小时前就开始学习RabbitMQ和Spring AMQP了。我认为学习东西最好的方法就是以身作则,自己犯错。
template.send("myExchange", "userMesssage", message);