提问者:小点点

无法使用 SpringApplication.exit() 终止 Spring 启动作业应用程序,因为 Tomcat 错误“套接字接受失败”


我有一个作业应用程序(在EKS中作为外部cron运行,而不是这个Spring Boot应用程序本身中的cron)需要终止。我在Eclipse中以Application.java运行它-

但是当我在运行()结束时手动执行此操作时:

@SpringBootApplication
public class MyApplication implements CommandLineRunner {

    @Autowired
    private ApplicationContext appContext;

    @Override
    public void run(String... args) throws Exception {

            // ... Run job, and at the end terminate manually

            SpringApplication.exit(appContext, new ExitCodeGenerator() {

                @Override
                public int getExitCode() {
                    return 0;
                }
                
            });

我收到此错误:

org.apache.tomcat.util.net.Acceptor.log(175)-套接字接受失败

023-03-24 11:27:29.364 [main] INFO  o.a.coyote.http11.Http11NioProtocol.log(173) - Pausing ProtocolHandler ["http-nio-8081"]
2023-03-24 11:27:29.364 [main] WARN  o.apache.tomcat.util.net.NioEndpoint.log(173) - Failed to unlock acceptor for [http-nio-8081] because the local address was not available
2023-03-24 11:27:29.364 [main] INFO  o.a.catalina.core.StandardService.log(173) - Stopping service [Tomcat]
2023-03-24 11:27:29.364 [http-nio-8081-Acceptor] ERROR org.apache.tomcat.util.net.Acceptor.log(175) - Socket accept failed
java.nio.channels.AsynchronousCloseException: null
    at java.base/java.nio.channels.spi.AbstractInterruptibleChannel.end(AbstractInterruptibleChannel.java:202)
    at java.base/sun.nio.ch.ServerSocketChannelImpl.end(ServerSocketChannelImpl.java:376)
    at java.base/sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:399)
    at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:520)
    at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:79)
    at org.apache.tomcat.util.net.Acceptor.run(Acceptor.java:128)
    at java.base/java.lang.Thread.run(Thread.java:833)

有没有办法从这个 Spring 启动作业应用程序中删除 Tomcat 层并手动终止它?


共1个答案

匿名用户

看起来您在Spring应用程序中使用了嵌入式Tomcat服务器。为了避免这种情况并将其作为独立应用程序运行,需要实现Command dLineRunner接口,以便您可以将作业作为独立应用程序运行。这样的实现是:

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext applicationContext) {
        return args -> {
            // Job code
            System.exit(0);
        };
    }