gpt4 book ai didi

java - spring-amqp- 较新的调用监听器

转载 作者:行者123 更新时间:2023-11-29 03:21:47 27 4
gpt4 key购买 nike

抱歉,我是 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"/>

spring-config.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: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

但我想看看:

Was sent
RECEIVE ...

怎么了??

最佳答案

您在这里的问题实际上是关于 AMQP 的知识不足。您应该了解什么是 ExchangeroutingKeyBinding

你需要配置:

<rabbit:direct-exchange name="myExchange">
<rabbit:bindings>
<rabbit:binding queue="userMesssageQueue" key="userMesssage" />
</rabbit:bindings>
</rabbit:direct-exchange>

从另一端,您应该使用具体的 routingKey 将消息发送到具体的 exchange。在你的情况下:

template.send("myExchange", "userMesssage", message);

只有在这种情况下,您的消息才会被放入 userMesssageQueue

默认情况下,RabbitTemplate 使用空字符串作为 exchange(默认值),为 routingKey 使用空字符串。由于您的 userMesssageQueue 未绑定(bind)到与该路由 key 的交换,因此您的监听器不会收到它。

发送工作没有错误,因为消息被放置在交换器上,这对生产者(发送者)来说已经足够了。

最后,您的消息只是被丢弃到 RabbitMQ 代理上,因为没有队列绑定(bind)到 "" 路由键。

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

关于java - spring-amqp- 较新的调用监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23173108/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com