使用Spring Integration JAR时,Spring Batch不会退出

问题描述 投票:1回答:1

当我使用Spring集成JAR时,我的spring应用程序没有退出。 MBeanExporter不会取消注册bean并关闭。

这是日志:

没有JAR

2018-09-03 11:03:45 INFO  [main] o.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=resetRegressionDataJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
2018-09-03 11:03:45 INFO  [main] c.p.c.r.ResetRegressionDataBatchJob - ResetRegressionDataBatchJob exiting with status of exitCode=COMPLETED;exitDescription=
2018-09-03 11:03:45 INFO  [Thread-118] o.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@c6f9085f: startup date [Mon Sep 03 11:02:56 BST 2018]; root of context hierarchy
2018-09-03 11:03:45 INFO  [Thread-118] o.s.jmx.export.MBeanExporter - Unregistering JMX-exposed beans on shutdown
2018-09-03 11:03:45 INFO  [Thread-118] o.s.jmx.export.MBeanExporter - Unregistering JMX-exposed beans
2018-09-03 11:03:45 INFO  [Thread-118] o.s.s.c.ThreadPoolTaskExecutor - Shutting down ExecutorService 'taskExecutor'
2018-09-03 11:03:46 INFO  [Thread-118] o.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'

有了JAR

2018-09-03 11:06:55 INFO  [main] c.p.c.r.ResetRegressionDataBatchJob - Started ResetRegressionDataBatchJob in 50.735 seconds (JVM running for 51.906)
2018-09-03 11:06:55 INFO  [main] c.p.c.i.batch.BaseRunnableBatchJob - **************** jobShouldBeRun: True (Default - No Override)
2018-09-03 11:06:55 INFO  [main] o.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=resetRegressionDataJob]] launched with the following parameters: [{}]
2018-09-03 11:06:55 INFO  [main] o.s.batch.core.job.SimpleStepHandler - Executing step: [step1]
2018-09-03 11:06:55 INFO  [main] c.p.c.r.ResetRegressionDataBatchJob - Running the Delete and Update Script for the Regression Reset Batch Job....
2018-09-03 11:06:55 INFO  [main] o.s.jdbc.datasource.init.ScriptUtils - Executing SQL script from class path resource [regression/data/regression.reset.deleteupdatedata.sql]
2018-09-03 11:06:55 INFO  [main] o.s.jdbc.datasource.init.ScriptUtils - Executed SQL script from class path resource [regression/data/regression.reset.deleteupdatedata.sql] in 193 ms.
2018-09-03 11:06:55 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Start Time is 2018-09-03 11:06:55:282
2018-09-03 11:06:55 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - End Time is 2018-09-03 11:06:55:528
2018-09-03 11:06:55 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Duration 0:0:0.246
2018-09-03 11:06:55 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Batch Status is COMPLETED
2018-09-03 11:06:55 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Exit Status is exitCode=COMPLETED;exitDescription=
2018-09-03 11:06:55 INFO  [main] o.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=resetRegressionDataJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
2018-09-03 11:06:55 INFO  [main] c.p.c.r.ResetRegressionDataBatchJob - ResetRegressionDataBatchJob exiting with status of exitCode=COMPLETED;exitDescription=

使用System.exit(0)

2018-09-03 11:49:50 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Start Time is 2018-09-03 11:49:50:052
2018-09-03 11:49:50 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - End Time is 2018-09-03 11:49:50:342
2018-09-03 11:49:50 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Duration 0:0:0.290
2018-09-03 11:49:50 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Batch Status is COMPLETED
2018-09-03 11:49:50 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Exit Status is exitCode=COMPLETED;exitDescription=
2018-09-03 11:49:50 INFO  [main] o.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=resetRegressionDataJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
2018-09-03 11:49:50 INFO  [main] c.p.c.r.ResetRegressionDataBatchJob - ResetRegressionDataBatchJob exiting with status of exitCode=COMPLETED;exitDescription=
2018-09-03 11:49:50 INFO  [Thread-118] o.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@fb5a33e4: startup date [Mon Sep 03 11:48:56 BST 2018]; root of context hierarchy
2018-09-03 11:49:50 INFO  [Thread-118] c.p.c.f.batch.ContextClosedHandler - Inside onApplicationEvent Shutting down ThreadPoolTaskExecutor
2018-09-03 11:49:50 INFO  [Thread-118] o.s.s.c.ThreadPoolTaskExecutor - Shutting down ExecutorService 'taskExecutor'
2018-09-03 11:49:50 INFO  [Thread-118] o.s.c.s.DefaultLifecycleProcessor - Stopping beans in phase 0
2018-09-03 11:49:50 INFO  [Thread-118] o.s.i.endpoint.EventDrivenConsumer - Removing {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2018-09-03 11:49:50 INFO  [Thread-118] o.s.i.c.PublishSubscribeChannel - Channel 'application.errorChannel' has 0 subscriber(s).
2018-09-03 11:49:50 INFO  [Thread-118] o.s.i.endpoint.EventDrivenConsumer - stopped _org.springframework.integration.errorLogger
2018-09-03 11:49:50 INFO  [Thread-118] o.s.c.s.DefaultLifecycleProcessor - Stopping beans in phase -2147483648
2018-09-03 11:49:50 INFO  [Thread-118] o.s.jmx.export.MBeanExporter - Unregistering JMX-exposed beans on shutdown
2018-09-03 11:49:50 INFO  [Thread-118] o.s.jmx.export.MBeanExporter - Unregistering JMX-exposed beans
2018-09-03 11:49:50 INFO  [Thread-118] o.s.s.c.ThreadPoolTaskExecutor - Shutting down ExecutorService 'taskExecutor'
2018-09-03 11:49:51 INFO  [Thread-118] o.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'
2018-09-03 11:49:51 INFO  [Thread-118] o.s.s.c.ThreadPoolTaskScheduler - Shutting down ExecutorService 'taskScheduler'

使用context.stop()

2018-09-03 14:49:39 INFO  [main] c.p.c.r.ResetRegressionDataBatchJob - Started ResetRegressionDataBatchJob in 49.884 seconds (JVM running for 50.91)
2018-09-03 14:49:39 INFO  [main] o.s.c.s.DefaultLifecycleProcessor - Starting beans in phase -2147483648
2018-09-03 14:49:39 INFO  [main] o.s.c.s.DefaultLifecycleProcessor - Starting beans in phase 0
2018-09-03 14:49:39 INFO  [main] c.p.c.i.batch.BaseRunnableBatchJob - **************** jobShouldBeRun: True (Default - No Override)
2018-09-03 14:49:39 INFO  [main] o.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=resetRegressionDataJob]] launched with the following parameters: [{}]
2018-09-03 14:49:39 INFO  [main] o.s.batch.core.job.SimpleStepHandler - Executing step: [step1]
2018-09-03 14:49:39 INFO  [main] c.p.c.r.ResetRegressionDataBatchJob - Running the Delete and Update Script for the Regression Reset Batch Job....
2018-09-03 14:49:39 INFO  [main] o.s.jdbc.datasource.init.ScriptUtils - Executing SQL script from class path resource [regression/data/regression.reset.deleteupdatedata.sql]
2018-09-03 14:49:39 INFO  [main] o.s.jdbc.datasource.init.ScriptUtils - Executed SQL script from class path resource [regression/data/regression.reset.deleteupdatedata.sql] in 181 ms.
2018-09-03 14:49:39 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Start Time is 2018-09-03 14:49:39:363
2018-09-03 14:49:39 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - End Time is 2018-09-03 14:49:39:594
2018-09-03 14:49:39 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Duration 0:0:0.231
2018-09-03 14:49:39 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Batch Status is COMPLETED
2018-09-03 14:49:39 INFO  [main] c.p.c.i.b.l.BaseBatchListenerSupport - Exit Status is exitCode=COMPLETED;exitDescription=
2018-09-03 14:49:39 INFO  [main] o.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=resetRegressionDataJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
2018-09-03 14:49:39 INFO  [main] c.p.c.r.ResetRegressionDataBatchJob - ResetRegressionDataBatchJob exiting with status of exitCode=COMPLETED;exitDescription=
2018-09-03 14:49:39 INFO  [main] o.s.c.s.DefaultLifecycleProcessor - Stopping beans in phase 0
2018-09-03 14:49:39 INFO  [main] o.s.i.endpoint.EventDrivenConsumer - Removing {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2018-09-03 14:49:39 INFO  [main] o.s.i.c.PublishSubscribeChannel - Channel 'application.errorChannel' has 0 subscriber(s).
2018-09-03 14:49:39 INFO  [main] o.s.i.endpoint.EventDrivenConsumer - stopped _org.springframework.integration.errorLogger
2018-09-03 14:49:39 INFO  [main] o.s.c.s.DefaultLifecycleProcessor - Stopping beans in phase -2147483648

Spring Launcher类

public static void main(String [] args)  throws Exception{
        logger.info("Starting Regression Data Reset Batch job......");
        SpringApplication app = new SpringApplication(ResetRegressionDataBatchJob.class);
        app.setWebEnvironment(false);
        app.setBannerMode(Banner.Mode.OFF);
        ExitStatus exitStatus = app.run(args).getBean("resetRegressionDataBatchJob", ResetRegressionDataBatchJob.class).run(args);
        logger.info("ResetRegressionDataBatchJob exiting with status of "+exitStatus);
        if (!ExitStatus.COMPLETED.equals(exitStatus) && !ExitStatus.NOOP.equals(exitStatus))
            System.exit(-1);

    }

我添加了System.exit(0)ctx.stop()后更新了日志。 context.stop()没有像System.exit(0)那样关闭所有组件。当我使用ctx.stop()时,线程仍在运行

java spring spring-boot spring-integration spring-batch
1个回答
2
投票

当您使用Spring Integration时,它会创建几个活动组件作为基础结构bean,以支持应用程序上下文中的其他bean。其中之一是ThreadPoolTaskExecutor

要使用活动组件正确关闭应用程序上下文,您需要使用ConfigurableApplicationContext.close()

/**
 * Close this application context, releasing all resources and locks that the
 * implementation might hold. This includes destroying all cached singleton beans.
 * <p>Note: Does <i>not</i> invoke {@code close} on a parent context;
 * parent contexts have their own, independent lifecycle.
 * <p>This method can be called multiple times without side effects: Subsequent
 * {@code close} calls on an already closed context will be ignored.
 */
@Override
void close();

stop()不会停用活动组件,但只停止当前正在进行的任务,并且在start()返回应用程序上下文或该活动组件之前不允许启动新组件。

© www.soinside.com 2019 - 2024. All rights reserved.