Spring 部署在 PCF 上的 Cloud Task 在 TaskExecution 结束时无法退出

Spring Cloud Task deployed on PCF fails to exit on TaskExecution end

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

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

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 升级后进入了这个问题。您/我们问题的根本原因似乎源于此change. I guess the related issue for this is 462

考虑到这一点,除了 属性 需要像这样

之外,您已接近解决方案
spring.cloud.task.closecontextEnabled=true

你可以参考的class是TaskProperties

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

在 manifest.yml

上添加此环境变量
env:
    JBP_CONFIG_SPRING_AUTO_RECONFIGURATION: '{enabled: false}'

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