我正在建立一个经典的制作人-> Rabbitmq->消费者流。 所有3个节点都在单独的jvm甚至单独的主机上运行
Producer是Spring Boot命令行运行器应用程序,有望在完成生产后停止。
Consumer应用程序是一个Spring Boot Web应用程序,它侦听3个Rabbitmq队列(2个持久队列绑定到一个直接交换,1个非持久队列绑定到一个扇出交换)
我的启动顺序如下: -启动rabbitmq -开始消费 -开始制作
生产者和消费者amqp依赖项mvn dependency:tree
[INFO] | +- org.springframework.boot:spring-boot-starter-amqp:jar:2.1.6.RELEASE:compile
[INFO] | | +- org.springframework:spring-messaging:jar:5.1.8.RELEASE:compile
[INFO] | | \- org.springframework.amqp:spring-rabbit:jar:2.1.7.RELEASE:compile
[INFO] | | +- org.springframework.amqp:spring-amqp:jar:2.1.7.RELEASE:compile
[INFO] | | | \- org.springframework.retry:spring-retry:jar:1.2.4.RELEASE:compile
[INFO] | | +- com.rabbitmq:amqp-client:jar:5.4.3:compile
[INFO] | | \- org.springframework:spring-tx:jar:5.1.8.RELEASE:compile
生产者代码
/**
* @author louis.gueye@gmail.com
*/
@RequiredArgsConstructor
@Slf4j
public class PlatformBrokerExampleProducerJob implements CommandLineRunner {
private final AmqpTemplate template;
@Override
public void run(String... args) {
final Instant now = Instant.now();
final Instant anHourAgo = now.minus(Duration.ofHours(1));
final String directExchangeName = "careassist_queues";
final String fanoutExchangeName = "careassist_schedules_topics";
IntStream.range(0, 60).boxed().forEach(i -> {
final SensorEventDto event = SensorEventDto.builder() //
.id(UUID.randomUUID().toString()) //
.businessId("sens-q7ikjxk1ftik") //
.timestamp(anHourAgo.plus(Duration.ofMinutes(i))) //
.state(SensorState.on) //
.build();
final String routingKey = "care.events";
template.convertAndSend(directExchangeName, routingKey, event);
log.info(">>>>>>>>>>> Sent {} to exchange {} with routing key {}", event.getId(), directExchangeName, routingKey);
});
IntStream.range(0, 60).boxed().forEach(i -> {
final SensorEventDto event = SensorEventDto.builder() //
.id(UUID.randomUUID().toString()) //
.businessId("sens-q7ikjxk1ftik") //
.timestamp(anHourAgo.plus(Duration.ofMinutes(i))) //
.state(SensorState.off) //
.build();
final String routingKey = "maintenance.events";
template.convertAndSend(directExchangeName, routingKey, event);
log.info(">>>>>>>>>>> Sent {} to exchange {} with routing key {}", event.getId(), directExchangeName, routingKey);
});
IntStream.range(0, 60).boxed().forEach(i -> {
final SensorEventDto event = SensorEventDto.builder() //
.id(UUID.randomUUID().toString()) //
.businessId("sens-q7ikjxk1ftik") //
.timestamp(anHourAgo.plus(Duration.ofMinutes(i))) //
.state(SensorState.off) //
.build();
final ScheduleDto schedule = ScheduleDto.builder().id(UUID.randomUUID().toString()) //
.destination("any.routing.queue") //
.message(event) //
.timestamp(anHourAgo.plus(Duration.ofMinutes(i))) //
.build();
final String routingKey = "#";
template.convertAndSend(fanoutExchangeName, routingKey, schedule);
log.info(">>>>>>>>>>> Sent {} to exchange {} with routing key {}", event.getId(), fanoutExchangeName, routingKey);
});
}
}
消费者代码(1个侦听器)
@Component
@RabbitListener(queues = {PlatformBrokerExampleCareEventsQueueConsumer.QUEUE_NAME})
@Slf4j
public class PlatformBrokerExampleCareEventsQueueConsumer {
public static final String QUEUE_NAME = "care_events";
@RabbitHandler
public void onMessage(SensorEventDto event) {
log.info("<<<<<<<<<<<< Received event [" + event + "] from {}...", PlatformBrokerExampleCareEventsQueueConsumer.QUEUE_NAME);
}
}
我希望生产者先生产然后关机,但是Java进程会无限期挂起
任何有关生产者为何在产生其消息后不停止的解释将不胜感激。我怀疑它与spring-started-amqp
有关,但不确定。我当然不需要装满的罐子,只需要装满AmqpTemplate
注意:消费者收到了所有消息
github project
谢谢您的帮助。
答案 0 :(得分:2)
PlatformBrokerClientConfiguration绑定队列。但是我看不到任何地方可以关闭队列。这样也许就是挂起实例的原因。
请尝试这个。
public static void main(String[] args) {
System.exit(SpringApplication.exit(SpringApplication.run(EmployeeDataProduceApp.class, args)));
}
答案 1 :(得分:2)
AMQP客户端具有一些后台线程。
您应该更改main()
方法,以便在运行程序返回后关闭应用程序上下文。
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args).close();
}
它将彻底关闭所有内容,因为它比System.exit()
残酷得多。