org.springframework.amqp.AmqpIllegalStateException:未指定“队列”。检查RabbitTemplate的配置

时间:2017-12-09 18:43:42

标签: spring-boot rabbitmq amqp

我正在创建一个从rabbitmq读取消息并写入oracle数据库的应用程序。我使用spring boot batch来读取消息,但它以错误“No'queue'指定结束。检查RabbitTemplate的配置。”

RabbitConfig.java

@Component({ ... })  
export class MyClass implements AfterViewInit {
  @ViewChildren('sourceTh')
  sourceTh: QueryList<ElementRef>;

  @ViewChildren('targetTh')
  targetTh: QueryList<ElementRef>;

  constructor( private renderer: Renderer2 ) {}

  ngAfterViewInit() {
    this.resizeColumns();
  }

  resizeColumns() {
    let widths = this.sourceTh.map(th => th.nativeElement.offsetWidth);

    this.targetTh.forEach((th, index) => {
      this.renderer.setStyle(
        th.nativeElement, 
        'width', 
        `${widths[index]}px`
      );
    });
  }
}

BatchConfig.java

@Configuration
public class RabbitMQConfig {

//  @Value("${conveh.rabbitmq.queue}")
    public String queueName ="hello2";

//  @Value("${conveh.rabbitmq.exchange}")
    public String exchange="hello_exchage2";

    @Bean
    public Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(exchange);
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setQueue(queueName);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        return rabbitTemplate;
    }

    @Bean
    public ConnectionFactory connectionFactory() {

        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost("host");
        connectionFactory.setPort(123);
        connectionFactory.setVirtualHost("/xxx");
        connectionFactory.setUsername("xxx");
        connectionFactory.setPassword("xxx");
        return connectionFactory;
    }
}

我缺少正确的队列名称。

1 个答案:

答案 0 :(得分:1)

我为rabbitTemplate()方法使用了错误的返回类型。它应该是RabbitTemplate。

 @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setQueue(queueName);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        return rabbitTemplate;
    }