Nih*_*rma 0 rabbitmq spring-amqp
我已经实现了 RabbitMQ,但卡在一个地方,似乎我的发送者无法向队列发送任何数据。
我的制作人课程:
@Service
public class MessageSender {
@Autowired
private AmqpTemplate template;
public void send(String text) {
template.convertAndSend(text);
}
}
Run Code Online (Sandbox Code Playgroud)
我的 spring 配置文件如下所示:
<rabbit:connection-factory id="connectionFactory"
addresses="${connectionFactory.addresses}" channel-cache-size="${connectionFactory.channel-cache-size}"
virtual-host="${connectionFactory.vhost}" username="${connectionFactory.user}"
password="${connectionFactory.password}" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" queue="myQueue" exchange="myExchange" routing-key="dummyKey"/>
<rabbit:queue name="myQueue" />
<rabbit:direct-exchange name="myExchange">
<rabbit:bindings>
<rabbit:binding queue="myQueue" />
</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="messageHandler" method="onMessage" queue-names="myQueue" />
</rabbit:listener-container>
<rabbit:admin connection-factory="connectionFactory" />
<bean id="messageHandler" class="com.tm.email.sender.spring.MessageHandler" />
Run Code Online (Sandbox Code Playgroud)
我无法找出问题所在。
下面提到的事情完美地运作。我可以轻松地将消息推送到队列,然后我的发送者类工作得很好。
public class Send {
private final static String QUEUE_NAME = "myQueue";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
int i = 0;
while(i < 10000){
HashMap message = new HashMap();
message.put("message number", i);
channel.basicPublish("", QUEUE_NAME, null, SerializationUtils.serialize(message));
i++;
}
channel.close();
connection.close();
}
}
Run Code Online (Sandbox Code Playgroud)
您对定义有默认值 。这意味着您将通过路由密钥向 发送消息。routing-key="dummyKey"
<rabbit:template>
template.convertAndSend(text);
myExchange
dummyKey
由于您没有通过您提供binding
的消息刚刚丢失。myQueue
dummyKey
从原始 AMQP 代码中,您只需使用每个队列的""
默认值(其名称)将消息发送到默认 Exchange ( ) 。routing-key
对于侦听器来说,消息从何处以及如何出现在队列中并不重要。这就是为什么第二个“发送”变体效果很好的原因。
因此,要使用 修复您的用例AmqpTemplate
,您必须添加以下内容:
<rabbit:binding queue="myQueue" key="dummyKey"/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2305 次 |
最近记录: |