【问题标题】:Spring Cloud Task deployed on PCF fails to exit on TaskExecution end部署在 PCF 上的 Spring Cloud Task 无法在 TaskExecution 端退出
【发布时间】:2019-07-04 22:05:18
【问题描述】:

我正在尝试在 PCF 上托管 Spring 云任务应用程序,并使用 PCF 调度程序的 CRON 作业每小时运行一次任务。但是,作为任务的一部分,我必须将消息发布到 RabbitMQ 交换。 RabbitMQ 实例是绑定到任务应用程序的 PCF 服务上的 RabbitMQ。当我运行任务时,任务执行结束但应用程序没有关闭,从而导致任务实例永久处于运行状态。

这是我的应用程序类的代码。

package com.example.demo;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.task.configuration.EnableTask;

@EnableTask
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    final String topicExchangeName = "demo-exchange";

    @Autowired
    RabbitTemplate rabbitTemplate;

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args).close();
    }

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

        System.out.println("Hello Task Demo!");
            rabbitTemplate.convertAndSend(topicExchangeName, "HLT", "Hello Task!");
        }

    }

这是 application.properties 文件

logging.level.org.springframework.cloud.task=DEBUG
spring.application.name=helloTaskApp
spring.cloud.task.closecontext_enable=true
spring.cloud.task.events.enabled=false

以下是任务运行的日志

2019-02-11T17:20:47.468+05:30 [APP/TASK/sample-task/0] [OUT] :: Spring Boot :: (v2.1.2.RELEASE)
2019-02-11T17:20:47.468+05:30 [APP/TASK/sample-task/0] [OUT] =========|_|==============|___/=/_/_/_/
2019-02-11T17:20:47.468+05:30 [APP/TASK/sample-task/0] [OUT] ' |____| .__|_| |_|_| |_\__, | / / / /
2019-02-11T17:20:47.468+05:30 [APP/TASK/sample-task/0] [OUT] \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2019-02-11T17:20:47.468+05:30 [APP/TASK/sample-task/0] [OUT] ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2019-02-11T17:20:47.468+05:30 [APP/TASK/sample-task/0] [OUT] /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2019-02-11T17:20:47.468+05:30 [APP/TASK/sample-task/0] [OUT] . ____ _ __ _ _
2019-02-11T17:20:47.652+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:47.649 INFO 10 --- [ main] pertySourceApplicationContextInitializer : 'cloud' property source added
2019-02-11T17:20:47.653+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:47.653 INFO 10 --- [ main] nfigurationApplicationContextInitializer : Reconfiguration enabled
2019-02-11T17:20:47.661+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:47.661 INFO 10 --- [ main] com.example.demo.DemoApplication : The following profiles are active: cloud
2019-02-11T17:20:47.661+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:47.660 INFO 10 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on 91defac2-7964-41f1-8b39-096c9e32ca32 with PID 10 (/home/vcap/app/BOOT-INF/classes started by vcap in /home/vcap/app)
2019-02-11T17:20:48.304+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:48.303 INFO 10 --- [ main] o.c.reconfiguration.CloudServiceUtils : 'rabbitConnectionFactory' bean of type with 'org.springframework.amqp.rabbit.connection.ConnectionFactory' reconfigured with 'rabbit-1' bean
2019-02-11T17:20:48.327+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:48.327 INFO 10 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$ec22c458] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-02-11T17:20:48.505+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:48.505 DEBUG 10 --- [ main] o.s.c.t.c.DefaultTaskConfigurer : No DataSource was found, using ResourcelessTransactionManager
2019-02-11T17:20:48.505+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:48.505 DEBUG 10 --- [ main] o.s.c.t.c.SimpleTaskAutoConfiguration : Using org.springframework.cloud.task.configuration.DefaultTaskConfigurer TaskConfigurer
2019-02-11T17:20:48.864+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:48.863 DEBUG 10 --- [ main] o.s.c.t.r.support.SimpleTaskRepository : Creating: TaskExecution{executionId=0, parentExecutionId=null, exitCode=null, taskName='helloTaskApp', startTime=Mon Feb 11 11:50:48 UTC 2019, endTime=null, exitMessage='null', externalExecutionId='null', errorMessage='null', arguments=[]}
2019-02-11T17:20:48.872+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:48.872 INFO 10 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.742 seconds (JVM running for 2.43)
2019-02-11T17:20:48.873+05:30 [APP/TASK/sample-task/0] [OUT] Hello Task Demo!
2019-02-11T17:20:48.876+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:48.876 INFO 10 --- [ main] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [*****]
2019-02-11T17:20:49.017+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:49.016 INFO 10 --- [ main] o.s.a.r.c.CachingConnectionFactory : Created new connection: SpringAMQP#fdefd3f:0/SimpleConnection@527e5409 [delegate=amqp://8de6f119-e720-4baf-b2f7-cf8d7704985e@10.32.27.77:5672/ac4af8cd-c6ff-4da2-a645-b14f27eea150, localPort= 55422]
2019-02-11T17:20:49.051+05:30 [APP/TASK/sample-task/0] [OUT] 2019-02-11 11:50:49.051 DEBUG 10 --- [ main] o.s.c.t.r.support.SimpleTaskRepository : Updating: TaskExecution with executionId=0 with the following {exitCode=0, endTime=Mon Feb 11 11:50:49 UTC 2019, exitMessage='null', errorMessage='null'}

我正在使用命令“.java-buildpack/open_jdk_jre/bin/java org.springframework.boot.loader.JarLauncher”来运行任务。

它在本地运行良好,但添加 RabbitTemplate 似乎无法以某种方式关闭应用程序上下文。谁能帮我理解为什么会这样?

【问题讨论】:

    标签: spring-boot rabbitmq spring-cloud-task


    【解决方案1】:

    不确定这是否仍然是一个问题,但我们在对我们的一项任务进行春季升级后遇到了这个问题。您/我们的问题的根本原因似乎源于此change。我想与此相关的问题是462

    考虑到这一点,您已经接近解决方案,除了需要像这样的属性

    spring.cloud.task.closecontextEnabled=true
    

    可以参考的类是TaskProperties

    【讨论】:

    • 试过了,好像没用。我会将我所做的作为解决方法添加到答案列表中
    • 我遇到了类似的问题,closecontextEnabled 属性似乎也没有为我解决问题。 @SohamBasu 你的解决方法是什么?
    • @Zetten 我们所做的直接解决方法是在任务结束执行后立即显式销毁我们的 RabbitTemplate bean。这很好用,但我们还需要一个 Spring Data Cassandra 存储库 bean,因此这意味着 buildpack 中的云配置文件配置的任何依赖项都必须显式销毁。我已经在答案中发布了实际修复,您可能需要检查一下。如果您更喜欢解决方法,请参考此example
    【解决方案2】:

    这源于 PCF 上的 buildpack 添加的“云”配置文件。您可以在我的日志中以及问题中看到相同的内容。

    在 manifest.yml 中添加这个环境变量

    env:
        JBP_CONFIG_SPRING_AUTO_RECONFIGURATION: '{enabled: false}'
    

    这阻止了 buildpack 加载云自动配置 jar 并且应用程序在任务运行后停止。事后看来,更多的是 cloudfoundry buildpack 问题,而不是 spring 云任务问题。

    【讨论】: