Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring amqp不会使用AmqpTemplate.convertAndSend()插入消息_Java_Spring_Spring Amqp - Fatal编程技术网

Java spring amqp不会使用AmqpTemplate.convertAndSend()插入消息

Java spring amqp不会使用AmqpTemplate.convertAndSend()插入消息,java,spring,spring-amqp,Java,Spring,Spring Amqp,下面是一个小型Spring程序,它将向rabbitmq队列中插入消息: public class Main { public static void main(String [] args) throws IOException { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(QueueConfiguration.class); AmqpTemplate

下面是一个小型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上设置exchange的名称或队列的名称,但服务器上仍然没有显示任何内容

应用程序的日志不会显示任何错误,但会记录以下内容:

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 = []

有什么问题吗?

我必须在调用convertAndSend()时将队列作为参数:


仍然想知道为什么spring amqp不会抛出异常。有人知道在没有给出队列的情况下消息传递到哪里吗?

我想我会按照要求将路由密钥和队列名称保存在bean
rabbitmplate()
中。由于我目前正在处理多个队列,因此对于每个队列,我都有不同的类,其中的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部署此功能?如果是,则可以在启动时加载这些消息,这也将初始化所有连接/通道/队列等。

这正是amqp/rabbitmq的工作方式-默认情况下,不可中断的消息会自动删除。请注意,按队列名称进行路由非常方便,因为所有队列都绑定到默认exchange,路由密钥等于队列名称。通常建议通过使用路由密钥将队列绑定到交换机,从而将生产者与队列解耦。
@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;
}