使用 Spring Integration JAR 时 Spring Batch 未退出
Posted
技术标签:
【中文标题】使用 Spring Integration JAR 时 Spring Batch 未退出【英文标题】:Spring Batch is not exiting when using Spring Integration JAR 【发布时间】:2019-02-08 09:42:53 【问题描述】:当我使用 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()
时线程仍在运行
【问题讨论】:
这可能会有所帮助:***.com/a/23295587/5019386 @Mahmoud Ben Hassine 你能解释一下我如何在我的 Launcher 类中添加生命周期停止,因为我没有 ApplicationContext。更新我上面的代码。app.run(args)
返回一个ConfigurableApplicationContext
,您可以在适当的时候调用stop
。
我试过了,它仍然没有退出。
您需要使用context.close()
。这样所有资源和线程都将被正确清理
【参考方案1】:
当您使用 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()
后面不允许重新开始。
【讨论】:
以上是关于使用 Spring Integration JAR 时 Spring Batch 未退出的主要内容,如果未能解决你的问题,请参考以下文章
将消息发送到套接字端口并使用Spring Integration接收响应
将war部署到weblogic版本(weblogic 12.2.1.3)时,Spring Integration应用程序部署失败
使用 Spring Integration HttpInbound 示例