下面是一个小型Spring程序,预期它会将一条消息插入到rabbitmq队列中:
public class Main {
public static void main(String [] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(QueueConfiguration.class);
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("asdflk ...");
context.destroy();
}
}ApplicationContext如下:
@Configuration
public class QueueConfiguration {
@Bean
public ConnectionFactory connectionFactory() {
return new CachingConnectionFactory("192.168.1.39");
}
@Bean
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(connectionFactory());
}
}当我检查服务器上队列的内容时,不会插入任何内容。我还试图在RabbitTemplate上设置交换的名称或队列的名称,但是服务器上仍然没有显示任何内容。
应用程序的日志不显示任何错误,但记录如下:
17:28:02.441 [main] DEBUG o.s.amqp.rabbit.core.RabbitTemplate - Executing callback on RabbitMQ Channel: Cached Rabbit Channel: AMQChannel(amqp://guest@192.168.1.39:5672/,1)
17:28:02.441 [main] DEBUG o.s.amqp.rabbit.core.RabbitTemplate - Publishing message on exchange [], routingKey = []有什么好主意吗?
发布于 2015-03-16 16:43:58
在调用convertAndSend()时,我必须将队列作为参数:
template.convertAndSend("hello2", "asdflk ...");仍然想知道为什么spring不会抛出一个异常。当没有队列时,有人知道消息在哪里传递吗?
发布于 2015-03-16 19:49:53
我想我会将路由密钥和队列名保留在bean rabbitTemplate()中,就像spring-amqp示例一样。由于我目前正在处理多个队列,所以对于每个队列,我都有不同的类,其中我有rabbitTemplate,如下所示:
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
//The routing key = name of the queue in default exchange.
template.setRoutingKey("MyQueue");
// Queue name
template.setQueue("MyQueue");
return template;
}你是用tomcat来部署这个吗?如果是,那么它们可以在启动时加载,这也将初始化所有连接/通道/队列等。
https://stackoverflow.com/questions/29082044
复制相似问题