there are currently thousands of messages stored in mq. Integrated into the project, after each startup, only 2 pieces of consume can be dropped, and it will not be consumed automatically. But to bring out the consumer alone and become a pure to the project, alone, you can constantly listen to the messages in the mq and continue to consume. The reason is unknown.
@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){
//SimpleRabbitListenerContainerFactorycontent_typetextstring
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...");
}
phenomenon:
output something like this, and then there is no
---!!!!!!!!!!-------[B@7ea7b053
40ab
ticket consumer start running...
---!!!!!!!!!!-------[B@44fccb7a
40ac
and in rabbitmq"s administrative interface, queue, consumer doesn"t have any consumer registrations, it"s empty.