Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何防止Spring启动守护程序/服务器应用程序立即关闭/关闭?_Java_Spring_Spring Boot_Daemon_Server - Fatal编程技术网

Java 如何防止Spring启动守护程序/服务器应用程序立即关闭/关闭?

Java 如何防止Spring启动守护程序/服务器应用程序立即关闭/关闭?,java,spring,spring-boot,daemon,server,Java,Spring,Spring Boot,Daemon,Server,我的Spring Boot应用程序不是一个web服务器,而是一个使用自定义协议的服务器(在本例中使用Camel) 但启动后,Spring Boot立即停止(优雅地)。我如何防止这种情况 如果按Ctrl+C或编程方式,我希望应用程序停止 @CompileStatic @Configuration class CamelConfig { @Bean CamelContextFactoryBean camelContext() { final camelContext

我的Spring Boot应用程序不是一个web服务器,而是一个使用自定义协议的服务器(在本例中使用Camel)

但启动后,Spring Boot立即停止(优雅地)。我如何防止这种情况

如果按Ctrl+C或编程方式,我希望应用程序停止

@CompileStatic
@Configuration
class CamelConfig {

    @Bean
    CamelContextFactoryBean camelContext() {
        final camelContextFactory = new CamelContextFactoryBean()
        camelContextFactory.id = 'camelContext'
        camelContextFactory
    }

}

springboot应用程序要持续运行,必须在容器中运行,否则就像任何java应用程序一样,所有线程都完成了, 你可以加上

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

org.springframework.boot
SpringBootStarterWeb

它将变成webapp,如果你不负责在你的实现中保持它的活力

我使用
org.springframework.boot.CommandLineRunner
+
Thread.currentThread().join()
找到了解决方案,例如: (注意:下面的代码是Groovy,而不是Java)


Spring Boot将运行应用程序的任务留给应用程序围绕其实现的协议。例如,请参见:

还需要一些内务对象,如
倒计时闩锁
,以使主线程保持活动状态


例如,运行Camel服务的方法是从主Spring Boot应用程序类中将Camel作为一个应用程序运行。

一个使用CountDownLatch的示例实现:

@Bean
public CountDownLatch closeLatch() {
    return new CountDownLatch(1);
}

public static void main(String... args) throws InterruptedException {
    ApplicationContext ctx = SpringApplication.run(MyApp.class, args);  

    final CountDownLatch closeLatch = ctx.getBean(CountDownLatch.class);
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            closeLatch.countDown();
        }
    });
    closeLatch.await();
}
现在要停止应用程序,您可以查找进程ID并从控制台发出kill命令:

kill <PID>
杀死
从Apache Camel 2.17开始,有一个更清晰的答案。引述:

要使主线程保持阻塞状态,以便Camel保持运行,请包括spring boot starter web依赖项,或者将Camel.springboot.main run controller=true添加到application.properties或application.yml文件中

您还需要以下依赖项:


org.apache.camel
驼形弹簧靴起动器
2.17.0


清楚地替换
2.17.0
或使用camel BOM导入依赖项管理信息以保持一致性。

要在不部署web应用程序时保持java进程的活动状态,请将webEnvironment属性设置为false,如下所示:

 SpringApplication sa = new SpringApplication();
 sa.setWebEnvironment(false); //important
 ApplicationContext ctx = sa.run(ApplicationMain.class, args);

我的项目是非WEB Spirng Boot。 我的优雅解决方案是通过CommandLineRunner创建守护进程线程。 然后,应用程序不会立即关闭

 @Bean
    public CommandLineRunner deQueue() {
        return args -> {
            Thread daemonThread;
            consumer.connect(3);
            daemonThread = new Thread(() -> {
                try {
                    consumer.work();
                } catch (InterruptedException e) {
                    logger.info("daemon thread is interrupted", e);
                }
            });
            daemonThread.setDaemon(true);
            daemonThread.start();
        };
    }

所有线程完成后,程序将自动关闭。
因此,使用
@Scheduled
注册一个空任务将创建一个循环线程,以防止关机。

这现在变得更加简单


只需将
camel.springboot.main run controller=true
添加到你的应用程序中。属性

你如何公开你的端点,你能分享一些代码吗?@Haim就像我上面说的,它不是一个web服务器。所以我的问题是。。。我如何模拟Tomcat的行为?我是否要暂停主线程或类似的东西?什么是“官方”的方法?你的主要方法是什么?这不会编译。@static void main(String[]args)可以,但请查看main()。看来你少了一些父母。它不编译。“SpringApplication.run LumenSocialApplication,args”@隐身拉比我要说的是我的代码是Groovy,非Java此操作将破坏使用
SpringBootTest
注释的集成测试。“此指南”链接已被破坏。Spring似乎对其quides进行了修改,原始文章不再可用。这可能是答案,而不是评论。请阅读规则。
 @Bean
    public CommandLineRunner deQueue() {
        return args -> {
            Thread daemonThread;
            consumer.connect(3);
            daemonThread = new Thread(() -> {
                try {
                    consumer.work();
                } catch (InterruptedException e) {
                    logger.info("daemon thread is interrupted", e);
                }
            });
            daemonThread.setDaemon(true);
            daemonThread.start();
        };
    }