Приложение Spring Cloud Task App выдает ошибку «Задача уже запущена», хотя предыдущая задача была завершена до ее запуска - PullRequest
0 голосов
/ 25 июня 2019

Я включил конфигурацию с одним экземпляром, чтобы ограничить одновременное выполнение облачных задач с тем же именем, что и в приведенной ниже документации.https://spring.io/blog/2018/05/07/spring-cloud-task-2-0-0-release-is-now-available

Я получаю следующую трассировку стека:

org.springframework.context.ApplicationContextException: Failed to start bean 'taskLifecycleListener'; nested exception is org.springframework.cloud.task.listener.TaskExecutionException: Failed to process @BeforeTask or @AfterTask annotation because: Task with name "fulfillment_label_file_generation_task" is already running.
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:184) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:52) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:157) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:121) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:885) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]

Задача Spring Cloud:

    @SpringBootApplication
    @EnableTask
    @EnableBatchProcessing
    @EnableBinding(Source.class)
    @Slf4j
    @Configuration
    @PropertySource("file:/app.properties")
    public class FileGenerationTaskApplication {
        @Autowired
        private DataSource dataSource;

        public class FileGeneratorTaskConfigurer extends DefaultTaskConfigurer {
            public FileGeneratorTaskConfigurer(DataSource dataSource) {
                super(dataSource);
            }
        }

        @Bean
        public FileGeneratorTaskConfigurer getTaskConfigurer() {
            return new FileGeneratorTaskConfigurer(dataSource);
        }

        public static void main(String[] args) {
            SpringApplication.run(FileGenerationTaskApplication.class, args);
        }

        @Component
        public static class FileGeneratorTaskRunner implements ApplicationRunner, TaskExecutionListener {


            @Autowired
            private Job job;

            @Autowired
            private JobLauncher launcher;


            @Override
            @Retryable(value = FileJobFailedException.class, maxAttemptsExpression = "#{${app.retry.attempts}}", backoff = @Backoff(delayExpression = "#{${app.retry.delay}}"))
            public void run(ApplicationArguments args) throws Exception {

                log.info("Launching file generation task ");
//some code here

               }

            @Recover
            public void recover(FileJobFailedException exception) throws  Exception{
                log.error("Label file task  failed after maximum retry due to exception ");
            }

            @Override
            public void onTaskStartup(TaskExecution taskExecution) {
                startTime = System.currentTimeMillis();
                log.debug("File generation task is starting..");
            }

            @Override
            public void onTaskEnd(TaskExecution taskExecution) {

                    if (taskExecution.getExitCode() == 0) {
                        taskExecution.setExitMessage("File generation task is successfull");
                        log.info("File generation task is successfull ");
                    }
                    else {
                        taskExecution.setExitMessage("Failed ");
                    }
                } 

            }

            @Override
            public void onTaskFailed(TaskExecution taskExecution, Throwable throwable) {
                log.error("File generation task failed");

            }
        }
    }

application.properties:

spring.batch.initialize-schema=never
spring.jpa.hibernate.ddl-auto=none
spring.batch.job.enabled=false
spring.cloud.task.initialize.enable=false
spring.cloud.task.batch.fail-on-job-failure=true
spring.cloud.task.singleInstanceEnabled=true
spring.cloud.task.closecontextEnabled=true
spring.cloud.task.name=fulfillment_label_file_generation_task
spring.cloud.stream.kafka.binder.brokers=localhost:9092
spring.cloud.stream.bindings.output.destination=gcr-trigger-task

spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=

зависимость build.gradle:

ext {
    springCloudTaskVersion = '2.0.0.RELEASE'
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter') {
        exclude module: 'spring-boot-starter-logging' exclude module: 'logback-classic'
    }
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-batch')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-task', version:'2.0.0.RELEASE'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-stream', version:'2.0.1.RELEASE'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-stream-kafka', version:'2.0.1.RELEASE'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-deployer-local', version:'1.3.7.RELEASE'
    compile('org.springframework.integration:spring-integration-core')
    compile('org.springframework.integration:spring-integration-jdbc')
    compile('com.oracle:ojdbc8:12.2.0.1')
    compile('org.projectlombok:lombok:1.16.20')
    compile("com.h2database:h2:1.4.195")
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.batch:spring-batch-test')
    testCompile('junit:junit:4.12')
    compile group: 'org.apache.commons', name: 'commons-compress', version: '1.8'
    compile('org.apache.pdfbox:pdfbox:2.0.13')
    compile('com.github.dhorions:boxable:1.5')
    testCompile('io.findify:s3mock_2.12:0.2.5')
    compile group : 'com.amazonaws', name: 'aws-java-sdk-core', version: '1.11.312'
    compile group : 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.11.312'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-task-dependencies:${springCloudTaskVersion}"
    }
}

Может кто-нибудь, пожалуйста, дайте мне знать, если я что-то упустил или мне нужно сделать что-то по-другому.

...