spring boot rabbitmq集成,只能监听到两条消息后,然后没有了
目前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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到相同的问题了,楼主解决了吗