为了账号安全,请及时绑定邮箱和手机立即绑定

Spring boot standalone CommandLineRunner 不会返回

Spring boot standalone CommandLineRunner 不会返回

呼唤远方 2023-03-23 15:20:34
我正在构建一个经典的制作人 -> rabbitmq -> 消费者流程。所有 3 个节点都在单独的 jvm 甚至单独的主机上运行Producer 是一个 spring boot 命令行运行器应用程序,预计在完成生产后停止。消费者应用程序是一个 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@Slf4jpublic 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);        });
查看完整描述

2 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

AMQP 客户端有一些后台线程。

您应该更改main()方法以在跑步者返回后关闭应用程序上下文...

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

它会干净利落地关闭一切,因为它不像System.exit().


查看完整回答
反对 回复 2023-03-23
?
大话西游666

TA贡献1817条经验 获得超14个赞

PlatformBrokerClientConfiguration 绑定队列。但我看不到任何地方可以关闭队列。所以这可能是暂停您的实例的原因。

请试试这个。

  public static void main(String[] args) {
    System.exit(SpringApplication.exit(SpringApplication.run(EmployeeDataProduceApp.class, args)));
  }


查看完整回答
反对 回复 2023-03-23
  • 2 回答
  • 0 关注
  • 122 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号