JAVA-11240 Moved spring-cloud to spring-cloud-modules
This commit is contained in:
2
spring-cloud-modules/spring-cloud-task/springcloudtaskbatch/.gitignore
vendored
Normal file
2
spring-cloud-modules/spring-cloud-task/springcloudtaskbatch/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/target/
|
||||
/.settings/
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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.cloud</groupId>
|
||||
<artifactId>springcloudtaskbatch</artifactId>
|
||||
<name>springcloudtaskbatch</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-task</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<start-class>com.baeldung.TaskDemo</start-class>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* This Application requires:
|
||||
* * a MySql instance running, that allows a root user with no password, and with a database named
|
||||
*
|
||||
* (e.g. with the following command `docker run -p 3306:3306 --name bael-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true -e MYSQL_DATABASE=springcloud mysql:latest`)
|
||||
*
|
||||
*/
|
||||
@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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.task.JobConfiguration;
|
||||
import com.baeldung.task.TaskDemo;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootApplication
|
||||
@ContextConfiguration(classes = { JobConfiguration.class, TaskDemo.class }, initializers = { ConfigDataApplicationContextInitializer.class })
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:springcloud;DB_CLOSE_ON_EXIT=FALSE
|
||||
username: sa
|
||||
password:
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: create-drop
|
||||
properties:
|
||||
hibernate:
|
||||
dialect: org.hibernate.dialect.H2Dialect
|
||||
Reference in New Issue
Block a user