spring boot rabbitmq集成,只能监听到两条消息后,然后没有了

发布于 2022-09-11 16:27:48 字数 4693 浏览 20 评论 0

目前mq中存放到消息数量有几千条。集成到项目中,每次启动之后,只能够consume掉2条之后,就自动不消耗了。。。。但是单独将consumer拎出来,成为一个纯净到单独到project,又是可以不断监听到mq中的消息,不断消费。原因真是未知。

@Configuration
@EnableRabbit
public class RabbitMQConfiguration{
    @Value("${host}")
    private String host;

    @Value("${port}")
    private int port;

    @Value("${username}")
    private String username;

    @Value("${password}")
    private String password;

    @Value("${vhost}")
    private String virtualHost;

    @Value("${routing_key}")
    private String routing_key;

    @Value("${queue_name}")
    private String queue_name;

    @Value("${exchange}")
    private String exchange;

    @Value("${queue_durable}")
    private boolean queue_durable;

    @Value("${exchange_durable}")
    private boolean exchange_durable;

    @Value("${exchange_autoDelete}")
    private boolean exchange_autoDelete;

   

    @Bean
    public ConnectionFactory connectionFactory() throws Exception {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setPort(port);
        connectionFactory.setHost(host);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.afterPropertiesSet();
        return connectionFactory;
    }


    @Bean(name = "springRabbitTemplate")
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate RabbitTemplate() throws Exception {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        return template;
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        return rabbitAdmin;
    }

    @Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        //SimpleRabbitListenerContainerFactory发现消息中有content_type有text就会默认将其转换成string类型的
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }
}
@EnableRabbit
@Slf4j
@SpringBootApplication
@ComponentScan
public class Application implements CommandLineRunner {


    @Value("${host}")
    private String host;

    @Value("${port}")
    private int port;

    @Value("${username}")
    private String username;

    @Value("${password}")
    private String password;

    @Value("${vhost}")
    private String virtualHost;

    @Value("${routing_key}")
    private String routing_key;

    @Value("${queue_name}")
    private String queue_name;

    @Value("${exchange}")
    private String exchange;

    @Value("${queue_durable}")
    private boolean queue_durable;

    @Value("${exchange_durable}")
    private boolean exchange_durable;

    @Value("${exchange_autoDelete}")
    private boolean exchange_autoDelete;


    @Bean
    public Queue queue() {
        return new Queue(queue_name, queue_durable);
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(exchange,exchange_durable,exchange_autoDelete);
    }

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


    @RabbitListener(bindings ={@QueueBinding(value = @org.springframework.amqp.rabbit.annotation.Queue(value = "${queue_name}",durable = "${queue_durable}"),
            exchange =@org.springframework.amqp.rabbit.annotation.Exchange(value = "${exchange}",durable = "${exchange_durable}", type=ExchangeTypes.DIRECT),
            key="${routing_key}")})
    @RabbitHandler
    public void receivePaymentMsg(Message message){
        System.out.println("---!!!!!!!!!!-------"+message.getBody());
        JSONObject object = JSON.parseObject(new String(message.getBody()));
        log.info(object.toJSONString());
        JSONObject payload= object.getJSONObject("payload");
        if(payload.containsKey("id")) {
            String id = payload.get("id").toString();
            System.out.println(id);
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args).close();
    }

    @Override
    public void run(String... args) {
        System.out.println("consumer start running...");
    }

现象:

输出这样的,然后没有了

---!!!!!!!!!!-------[B@7ea7b053
40ab
ticket consumer start running...
---!!!!!!!!!!-------[B@44fccb7a
40ac

并且在rabbitmq的管理界面,queue那里,consumer没有任何consumer注册上了,是空的。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情哥ㄟ 2022-09-18 16:27:48

我也遇到相同的问题了,楼主解决了吗

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文