diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/.gitignore b/spring-cloud/spring-cloud-task/springcloudtaskbatch/.gitignore new file mode 100644 index 0000000000..f675857856 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/.gitignore @@ -0,0 +1,2 @@ +/target/ +/.settings/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml new file mode 100644 index 0000000000..4805f5296c --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml @@ -0,0 +1,81 @@ + + 4.0.0 + org.baeldung.cloud + springcloudtask + 0.0.1-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + + + + + com.baeldung.TaskDemo + UTF-8 + UTF-8 + 1.8 + 1.2.2.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + org.springframework.cloud + spring-cloud-starter-task + + + + org.springframework.cloud + spring-cloud-task-core + + + + org.springframework.boot + spring-boot-starter-batch + + + + org.springframework.cloud + spring-cloud-task-batch + + + + + + + org.springframework.cloud + spring-cloud-task-dependencies + ${spring-cloud-task.version} + pom + import + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/HelloWorldTaskConfigurer.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/HelloWorldTaskConfigurer.java new file mode 100644 index 0000000000..dc2ba53138 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/HelloWorldTaskConfigurer.java @@ -0,0 +1,14 @@ +package com.baeldung.task; + +import javax.sql.DataSource; + +import org.springframework.cloud.task.configuration.DefaultTaskConfigurer; + +public class HelloWorldTaskConfigurer + extends + DefaultTaskConfigurer { + + public HelloWorldTaskConfigurer(DataSource dataSource) { + super(dataSource); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/JobConfiguration.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/JobConfiguration.java new file mode 100644 index 0000000000..6c215ad53d --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/JobConfiguration.java @@ -0,0 +1,105 @@ +package com.baeldung.task; + +import java.util.Arrays; +import java.util.List; +import java.util.logging.Logger; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.item.support.ListItemReader; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class JobConfiguration { + + private final static Logger LOGGER = Logger + .getLogger(JobConfiguration.class.getName()); + + @Autowired + private JobBuilderFactory jobBuilderFactory; + + @Autowired + private StepBuilderFactory stepBuilderFactory; + + @Bean + public Step step1() { + return this.stepBuilderFactory.get("job1step1") + .tasklet(new Tasklet() { + @Override + public RepeatStatus execute( + StepContribution contribution, + ChunkContext chunkContext) + throws Exception { + LOGGER.info("Tasklet has run"); + return RepeatStatus.FINISHED; + } + }).build(); + } + + @Bean + public Step step2() { + return this.stepBuilderFactory + .get("job1step2") + . chunk(3) + .reader( + new ListItemReader<>(Arrays.asList("7", + "2", "3", "10", "5", "6"))) + .processor( + new ItemProcessor() { + @Override + public String process(String item) + throws Exception { + LOGGER.info("Processing of chunks"); + return String.valueOf(Integer + .parseInt(item) * -1); + } + }) + .writer(new ItemWriter() { + @Override + public void write( + List items) + throws Exception { + for (String item : items) { + LOGGER.info(">> " + item); + } + } + }).build(); + } + + @Bean + public Job job1() { + return this.jobBuilderFactory.get("job1") + .start(step1()) + .next(step2()) + .build(); + } + + @Bean + public Job job2() { + return jobBuilderFactory.get("job2") + .start(stepBuilderFactory.get("job2step1") + .tasklet(new Tasklet() { + @Override + public RepeatStatus execute( + StepContribution contribution, + ChunkContext chunkContext) + throws Exception { + LOGGER + .info("This job is from Baeldung"); + return RepeatStatus.FINISHED; + } + }) + .build()) + .build(); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java new file mode 100644 index 0000000000..be2a173589 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java @@ -0,0 +1,56 @@ +package com.baeldung.task; + +import java.util.logging.Logger; + +import javax.sql.DataSource; + +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.task.configuration.EnableTask; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +@SpringBootApplication +@EnableTask +@EnableBatchProcessing +public class TaskDemo { + + private final static Logger LOGGER = Logger + .getLogger(TaskDemo.class.getName()); + + @Autowired + private DataSource dataSource; + + @Bean + public HelloWorldTaskConfigurer getTaskConfigurer() + { + return new HelloWorldTaskConfigurer(dataSource); + } + + @Bean + public TaskListener taskListener() { + return new TaskListener(); + } + + public static void main(String[] args) { + SpringApplication.run(TaskDemo.class, args); + } + + @Component + public static class HelloWorldApplicationRunner + implements + ApplicationRunner { + + @Override + public void run(ApplicationArguments arg0) + throws Exception { + // TODO Auto-generated method stub + LOGGER + .info("Hello World from Spring Cloud Task!"); + } + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskListener.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskListener.java new file mode 100644 index 0000000000..8250153b66 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskListener.java @@ -0,0 +1,31 @@ +package com.baeldung.task; + +import java.util.logging.Logger; + +import org.springframework.cloud.task.listener.TaskExecutionListener; +import org.springframework.cloud.task.repository.TaskExecution; + +public class TaskListener implements TaskExecutionListener { + + private final static Logger LOGGER = Logger + .getLogger(TaskListener.class.getName()); + + @Override + public void onTaskEnd(TaskExecution arg0) { + // TODO Auto-generated method stub + LOGGER.info("End of Task"); + } + + @Override + public void onTaskFailed(TaskExecution arg0, + Throwable arg1) { + // TODO Auto-generated method stub + + } + + @Override + public void onTaskStartup(TaskExecution arg0) { + // TODO Auto-generated method stub + LOGGER.info("Task Startup"); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/resources/application.yml b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/resources/application.yml new file mode 100644 index 0000000000..1187c12fe7 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/resources/application.yml @@ -0,0 +1,26 @@ +logging: + level: + org: + springframework: + cloud: + task=DEBUG + +spring: + application: + name=helloWorld + datasource: + url: jdbc:mysql://localhost:3306/springcloud?useSSL=false + username: root + password: + jpa: + hibernate: + ddl-auto: create-drop + properties: + hibernate: + dialect: org.hibernate.dialect.MySQL5Dialect + batch: + initialize-schema: always +maven: + remoteRepositories: + springRepo: + url: https://repo.spring.io/libs-snapshot \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/.gitignore b/spring-cloud/spring-cloud-task/springcloudtasksink/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml b/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml new file mode 100644 index 0000000000..b717fffc7c --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + com.baeldung + SpringCloudTaskSink + 0.0.1-SNAPSHOT + jar + + SpringCloudTaskSink + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + 1.2.2.RELEASE + Edgware.SR2 + + + + + org.springframework.cloud + spring-cloud-starter-stream-rabbit + + + org.springframework.cloud + spring-cloud-starter-task + + + org.springframework.cloud + spring-cloud-stream-test-support + test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.cloud + spring-cloud-deployer-local + 1.3.0.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-task-dependencies + ${spring-cloud-task.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplication.java b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplication.java new file mode 100644 index 0000000000..62085e5fa5 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.SpringCloudTaskFinal; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.task.launcher.annotation.EnableTaskLauncher; + +@SpringBootApplication +@EnableTaskLauncher +public class SpringCloudTaskSinkApplication { + + public static void main(String[] args) { + SpringApplication.run( + SpringCloudTaskSinkApplication.class, args); + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java new file mode 100644 index 0000000000..638de58ecb --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java @@ -0,0 +1,18 @@ +package com.baeldung.SpringCloudTaskFinal; + +import static org.mockito.Mockito.mock; + +import org.springframework.cloud.deployer.spi.task.TaskLauncher; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +@Configuration +public class TaskSinkConfiguration { + + @Bean + public TaskLauncher taskLauncher() { + return mock(TaskLauncher.class); + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/resources/application.properties b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/resources/application.properties new file mode 100644 index 0000000000..1660dc8516 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/resources/application.properties @@ -0,0 +1 @@ +maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot \ No newline at end of file diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplicationTests.java b/spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplicationTests.java new file mode 100644 index 0000000000..1f47eada14 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/SpringCloudTaskSinkApplicationTests.java @@ -0,0 +1,72 @@ +package com.baeldung.SpringCloudTaskFinal; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest; +import org.springframework.cloud.deployer.spi.task.TaskLauncher; +import org.springframework.cloud.stream.messaging.Sink; +import org.springframework.cloud.task.launcher.TaskLaunchRequest; +import org.springframework.context.ApplicationContext; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.verify; + +@RunWith(SpringRunner.class) +@SpringBootTest( + classes = SpringCloudTaskSinkApplication.class) +public class SpringCloudTaskSinkApplicationTests { + + @Autowired + ApplicationContext context; + + @Autowired + private Sink sink; + + @Test + public void testTaskLaunch() throws IOException { + + TaskLauncher taskLauncher = + context.getBean(TaskLauncher.class); + + Map prop = new HashMap(); + prop.put("server.port", "0"); + TaskLaunchRequest request = new TaskLaunchRequest( + "maven://org.springframework.cloud.task.app:" + + "timestamp-task:jar:1.0.1.RELEASE", null, + prop, + null, null); + GenericMessage message = new GenericMessage( + request); + this.sink.input().send(message); + + ArgumentCaptor deploymentRequest = ArgumentCaptor + .forClass(AppDeploymentRequest.class); + + verify(taskLauncher).launch( + deploymentRequest.capture()); + + AppDeploymentRequest actualRequest = deploymentRequest + .getValue(); + + // Verifying the co-ordinate of launched Task here. + assertTrue(actualRequest.getCommandlineArguments() + .isEmpty()); + assertEquals("0", actualRequest.getDefinition() + .getProperties().get("server.port")); + assertTrue(actualRequest + .getResource() + .toString() + .contains( + "org.springframework.cloud.task.app:timestamp-task:jar:1.0.1.RELEASE")); + } +} \ No newline at end of file