13. Spring Cloud Task Events

Spring Cloud Task provides the ability to emit events via Spring Cloud Stream channel when the task is executed via a Spring Cloud Stream channel. A task listener is used to publish the TaskExecution on a message channel named task-events. This feature is autowired into any task that has spring-cloud-stream on its classpath in addition to the spring-cloud-stream and a task defined.

[Note]Note

To disable the event emitting listener, set the property spring.cloud.task.events.enabled to false.

With the appropriate classpath defined, a simple task like this:

@SpringBootApplication
@EnableTask
public class TaskEventsApplication {

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

	@Configuration
	public static class TaskConfiguration {

		@Bean
		public CommandLineRunner commandLineRunner() {
			return new CommandLineRunner() {
				@Override
				public void run(String... args) throws Exception {
					System.out.println("The CommandLineRunner was executed");
				}
			};
		}
	}
}

will emit the TaskExecution as an event on the task-events channel (both at the start and end of the task).

[Note]Note

Configuration of the content type may be required via --spring.cloud.stream.bindings.task-events.contentType=<CONTENT_TYPE> if the processor or sink downstream does not have the spring-cloud-task-core jar on its classpath.

[Note]Note

A binder implementation is also required to be on the classpath.