1

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://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'}

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?

sohamangoes
  • 129
  • 2
  • 7

2 Answers2

1

Not sure if this is a problem still but we ran into this after taking a spring upgrade to one of our tasks. The root cause of your / our issue seems to stem from this change. I guess the related issue for this is 462.

With that in mind you were close to a solution except for the property needed to be like this

spring.cloud.task.closecontextEnabled=true

The class that you can refer to is TaskProperties

naav
  • 121
  • 8
  • Tried this, didn't seem to work. I'll add what I did as a workaround to the answers list – sohamangoes Jun 25 '19 at 11:48
  • I'm having a similar problem, and the `closecontextEnabled` property also doesn't seem to do the trick for me. @SohamBasu what was your workaround? – Zetten Mar 11 '20 at 14:28
  • @Zetten The immediate workaround we did was to explicitly destroy our RabbitTemplate bean as soon as the Task ended execution. This worked well, but we needed a Spring Data Cassandra repository bean as well, so it would mean any dependency configured by the cloud profile in the buildpack would have to be explicitly destroyed. I have posted the actual fix in the answer, you may want to check that out. If you prefer the workaround, just refer to this [example](https://gist.github.com/sohamangoes/ce017b64e6b365837ac1bcf426569314) – sohamangoes Apr 07 '20 at 03:09
0

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.

sohamangoes
  • 129
  • 2
  • 7