使用@queuebinding使用@rabbitlistener无效工作来声明和绑定队列?

时间:2015-12-28 08:42:39

标签: rabbitmq spring-amqp

我试图使用注释和rabbitlistener创建一个spring-amqp示例,但是在使用queuebinding自动声明队列时它没有用完。 关于这个的任何建议?很多。

这是消费者代码:

@Component
public class Consumer {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue1", durable = "true") , 
            exchange = @Exchange(value = "test"), 
            key = "hello1") )
    public void handleHello1(String text) {
        System.out.println("Received: " + text);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue2", durable = "true") , 
            exchange = @Exchange(value = "test") , 
            key = "hello2") )
    public void handleHello2(String text) {
        System.out.println("Received: " + text);
    }
}

配置代码:

@Configuration
@EnableRabbit 
public class Config {
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
        System.out.println("create rabbitListenerContainerFactory");
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setConcurrentConsumers(3);
        factory.setMaxConcurrentConsumers(10);
        return factory;
    }

    @Bean
    public AmqpAdmin amqpAdmin() {
        AmqpAdmin admin = new RabbitAdmin(connectionFactory());
        System.out.println("create amqpAdmin");
        return admin;
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        return template;
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        return connectionFactory;
    }

    @Bean
    public ScheduledProducer scheduledProducer() {
        return new ScheduledProducer();
    }

    @Bean
    public BeanPostProcessor postProcessor() {
        return new ScheduledAnnotationBeanPostProcessor();
    }


    static class ScheduledProducer {
        @Autowired
        private volatile RabbitTemplate rabbitTemplate;
        private final AtomicInteger counter = new AtomicInteger();

        @Scheduled(fixedRate = 3000)
        public void sendMessage() {
            String str = "hello" +(counter.get() % 2);
            System.out.println("routingkey:" + str);
            rabbitTemplate.convertAndSend("test", str, "Hello World " + counter.incrementAndGet());
        }
    }
}

制作代码:

public class Producer {
    public static void main(String[] args) throws Exception {
        new AnnotationConfigApplicationContext(Config.class);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在Consumer课程中添加Config ...

@Bean
public Consumer consumer() {
    return new Consumer();
}

(或在Config上启用组件扫描)。

@ComponentScan(basePackages="foo")

绑定中的路由键也是错误的 - 您要发送到hello0hello1