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:
@@ -0,0 +1,2 @@
|
||||
/target/
|
||||
/.settings/
|
||||
@@ -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>
|
||||
+14
@@ -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);
|
||||
}
|
||||
}
|
||||
+105
@@ -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();
|
||||
}
|
||||
}
|
||||
+56
@@ -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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
@@ -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");
|
||||
}
|
||||
}
|
||||
+26
@@ -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
|
||||
Reference in New Issue
Block a user