Search code examples
spring-bootrabbitmqspring-cloud-task

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


I am trying to host a Spring cloud task app on PCF and run the task hourly using PCF scheduler's CRON jobs. However, as part of the task, I have to publish a message onto a RabbitMQ exchange. The RabbitMQ instance is a RabbitMQ on PCF service which is bound to the task app. When I run the task, the Task Execution ends but the application does not shut down, thereby causing the task instance to be permanently in the running state.

This is the code for my application 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!");
        }

    }

This is the application.properties file

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

The following is the logs for a task run

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://[email protected]: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'}

I am using the command ".java-buildpack/open_jdk_jre/bin/java org.springframework.boot.loader.JarLauncher" for the task run.

It is running fine on local, but adding the RabbitTemplate seems to fail Application Context closing somehow. Can anyone help me understand why this is happening?


Solution

  • This stemmed to the 'cloud' profile being added by the buildpack on PCF. You can see the same thing in my logs as well in the question.

    Adding this environment variable on the manifest.yml

    env:
        JBP_CONFIG_SPRING_AUTO_RECONFIGURATION: '{enabled: false}'
    

    This prevented the buildpack from loading the cloud autoconfiguration jars and application stopped after task run. In hindsight, more of a cloudfoundry buildpack issue rather than a spring cloud task one.