Spring Cloud Task modules are added. (#3675)

* Spring Cloud Task modules are added.

* Unnecessary files are removed.

* All Sysout are replaced by Logger from Util.

* class name is fixed in Logger.

* Spring cloud task batch module POM updated with dependencies from Maven
Central.

* Links are removed and unnecessary comments are removed from POM.
This commit is contained in:
abirkhan04 2018-02-20 11:35:52 +06:00 committed by maibin
parent 327d8b5d97
commit 6826ad1853
13 changed files with 534 additions and 0 deletions

View File

@ -0,0 +1,2 @@
/target/
/.settings/

View File

@ -0,0 +1,81 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung.cloud</groupId>
<artifactId>springcloudtask</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<start-class>com.baeldung.TaskDemo</start-class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud-task.version>1.2.2.RELEASE</spring-cloud-task.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-task</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-batch</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-dependencies</artifactId>
<version>${spring-cloud-task.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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")
.<String, String> chunk(3)
.reader(
new ListItemReader<>(Arrays.asList("7",
"2", "3", "10", "5", "6")))
.processor(
new ItemProcessor<String, String>() {
@Override
public String process(String item)
throws Exception {
LOGGER.info("Processing of chunks");
return String.valueOf(Integer
.parseInt(item) * -1);
}
})
.writer(new ItemWriter<String>() {
@Override
public void write(
List<? extends String> 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();
}
}

View File

@ -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!");
}
}
}

View File

@ -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");
}
}

View File

@ -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

View File

@ -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/

View File

@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>SpringCloudTaskSink</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringCloudTaskSink</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud-task.version>1.2.2.RELEASE</spring-cloud-task.version>
<spring-cloud.version>Edgware.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-task</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-deployer-local</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-dependencies</artifactId>
<version>${spring-cloud-task.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1 @@
maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot

View File

@ -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<String, String> prop = new HashMap<String, String>();
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<TaskLaunchRequest> message = new GenericMessage<TaskLaunchRequest>(
request);
this.sink.input().send(message);
ArgumentCaptor<AppDeploymentRequest> 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"));
}
}