是否可以从任务接收器中接连执行另一个Cloud Task,从而触发Spring Cloud Task?

时间:2018-10-26 04:59:37

标签: spring-cloud-task

我们正在使用任务接收器方法来触发多个Spring Cloud Tasks。云任务之一是对另一任务的依赖。我试图从另一个的afterTask方法触发一个Cloud Task。但这是行不通的。让我知道这种方法是否可行,或者我应该做些其他事情。

@SpringBootApplication
@EnableBinding(Sink.class)
@RestController
@EnableScheduling
@EnableTaskLauncher
@Slf4j
public class FileTaskSink {
    @Autowired
    private Sink sink;

    @Value("${spring.task.artifactory.url}")
    private String uri;

    @Value("${spring.task.name:file_task_launcher}")
    private String taskName;

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

    @GetMapping("/triggerTask")
    public String publishTask(){
       log.info("Publishing task with task launcher request...");

       Map<String, String> prop = new HashMap<>();
       prop.put("server.port", "0");
           TaskLaunchRequest request = new TaskLaunchRequest(
              uri, null,
              prop,
              null, taskName);
           GenericMessage<TaskLaunchRequest> message = new 
              GenericMessage<TaskLaunchRequest>(
              request);
           this.sink.input().send(message);
           return "SUCCESS";
    }
}

我已在以下任务中配置了任务侦听器,因此,完成此任务后,我想触发一个Spring Cloud Task。

  @SpringBootApplication
  @EnableTask
  @EnableTaskLauncher
  @Slf4j
  public class FulfillmentPrintFileGenerationTaskApplication {
      @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(FulfillmentPrintFileGenerationTaskApplication.class, args);
      }

      @Component
      public static class FileGeneratorTaskRunner implements ApplicationRunner, TaskExecutionListener {
         @Autowired
         private JobLauncher launcher;
         @Autowired
         private Job job;
         @Autowired
         private ThreadPoolTaskExecutor executor;
         @Value("${fulfillment.client.id}")
         private String clientId;
         @Autowired
         private Sink sink;


            @Override
            public void run(ApplicationArguments args) {

                log.info("Launching  file generation task .. ");

            }

            @Override
            public void onTaskStartup(TaskExecution taskExecution) {
                log.info(“File generation task is starting.");
            }

            @Override
            public void onTaskEnd(TaskExecution taskExecution) {
                if(taskExecution.getExitCode()==0){
                    log.info(“Triggering another task..”);

                    TaskLaunchRequest request = new TaskLaunchRequest(
                       labelfileurl, null,
                       config,
                       null, taskName);
                    GenericMessage<TaskLaunchRequest> message = new 
                    GenericMessage<TaskLaunchRequest>(
                    request);
                    this.sink.input().send(message);
                }
                log.info(“File generation task is completed, shutting 
                down the executor beans..");
                executor.shutdown();
            }

            @Override
            public void onTaskFailed(TaskExecution taskExecution, 
                Throwable throwable) {
                log.warn(“File generation task is failed, shutting 
                down the executor beans..");
                executor.shutdown();
            }
        }
    }

请注意:我没有使用Spring Cloud Data Flow,因为我正在使用Task Sink应用程序。如果可以的话,请告诉我。我无法成功运行任务。

0 个答案:

没有答案