Updated to files to fit the Standard.

This commit is contained in:
Glenn Renfro
2022-07-25 11:41:43 -04:00
parent 39908bb499
commit 76a5d12136
200 changed files with 2839 additions and 4125 deletions

View File

@@ -60,43 +60,39 @@ public class BatchEventsApplication {
@Bean
public Step step1() {
return this.stepBuilderFactory.get("step1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Tasklet has run");
return RepeatStatus.FINISHED;
}
}).build();
return this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Tasklet has run");
return RepeatStatus.FINISHED;
}
}).build();
}
@Bean
public Step step2() {
return this.stepBuilderFactory.get("step2")
.<String, String>chunk(DEFAULT_CHUNK_COUNT)
.reader(new ListItemReader<>(Arrays.asList("1", "2", "3", "4", "5", "6")))
.processor(new ItemProcessor<String, String>() {
@Override
public String process(String item) throws Exception {
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) {
System.out.println(">> " + item);
return this.stepBuilderFactory.get("step2").<String, String>chunk(DEFAULT_CHUNK_COUNT)
.reader(new ListItemReader<>(Arrays.asList("1", "2", "3", "4", "5", "6")))
.processor(new ItemProcessor<String, String>() {
@Override
public String process(String item) throws Exception {
return String.valueOf(Integer.parseInt(item) * -1);
}
}
}).build();
}).writer(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
for (String item : items) {
System.out.println(">> " + item);
}
}
}).build();
}
@Bean
public Job job() {
return this.jobBuilderFactory.get("job")
.start(step1())
.next(step2())
.build();
return this.jobBuilderFactory.get("job").start(step1()).next(step2()).build();
}
}
}

View File

@@ -42,6 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@Tag("DockerRequired")
public class BatchEventsApplicationTests {
private static final String TASK_NAME = "taskEventTest";
private ConfigurableApplicationContext applicationContext;
@@ -64,33 +65,25 @@ public class BatchEventsApplicationTests {
@Test
public void testExecution() throws Exception {
List<Message<byte[]>> result = testListener(
taskEventProperties.getJobExecutionEventBindingName(), 1);
List<Message<byte[]>> result = testListener(taskEventProperties.getJobExecutionEventBindingName(), 1);
JobExecutionEvent jobExecutionEvent = this.objectMapper.readValue(result.get(0).getPayload(),
JobExecutionEvent.class);
assertThat(jobExecutionEvent.getJobInstance().getJobName())
.isEqualTo("job").as("Job name should be job");
JobExecutionEvent.class);
assertThat(jobExecutionEvent.getJobInstance().getJobName()).isEqualTo("job").as("Job name should be job");
}
private String[] getCommandLineParams(boolean enableFailJobConfig) {
String jobConfig = enableFailJobConfig ?
"--spring.cloud.task.test.enable-job-configuration=true" :
"--spring.cloud.task.test.enable-fail-job-configuration=true";
return new String[]{"--spring.cloud.task.closecontext_enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false",
"--spring.cloud.stream.defaultBinder=rabbit",
"--spring.cloud.stream.bindings.task-events.destination=test",
jobConfig,
"foo=" + UUID.randomUUID()};
String jobConfig = enableFailJobConfig ? "--spring.cloud.task.test.enable-job-configuration=true"
: "--spring.cloud.task.test.enable-fail-job-configuration=true";
return new String[] { "--spring.cloud.task.closecontext_enable=false", "--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false", "--spring.cloud.stream.defaultBinder=rabbit",
"--spring.cloud.stream.bindings.task-events.destination=test", jobConfig, "foo=" + UUID.randomUUID() };
}
private List<Message<byte[]>> testListener(String bindingName, int numberToRead) {
List<Message<byte[]>> results = new ArrayList<>();
this.applicationContext = new SpringApplicationBuilder()
.sources(TestChannelBinderConfiguration
.getCompleteConfiguration(BatchEventsTestApplication.class)).web(WebApplicationType.NONE).build()
.run(getCommandLineParams(true));
.sources(TestChannelBinderConfiguration.getCompleteConfiguration(BatchEventsTestApplication.class))
.web(WebApplicationType.NONE).build().run(getCommandLineParams(true));
OutputDestination target = this.applicationContext.getBean(OutputDestination.class);
for (int i = 0; i < numberToRead; i++) {
results.add(target.receive(10000, bindingName));
@@ -99,8 +92,9 @@ public class BatchEventsApplicationTests {
}
@SpringBootApplication
@Import({BatchEventsApplication.class})
@Import({ BatchEventsApplication.class })
public static class BatchEventsTestApplication {
}
}

View File

@@ -29,4 +29,5 @@ public class BatchJobApplication {
public static void main(String[] args) {
SpringApplication.run(BatchJobApplication.class, args);
}
}

View File

@@ -46,16 +46,13 @@ public class JobConfiguration {
@Bean
public Job job1() {
return this.jobBuilderFactory.get("job1")
.start(this.stepBuilderFactory.get("job1step1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
logger.info("Job1 was run");
return RepeatStatus.FINISHED;
}
})
.build())
.build();
return this.jobBuilderFactory.get("job1").start(this.stepBuilderFactory.get("job1step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
logger.info("Job1 was run");
return RepeatStatus.FINISHED;
}
}).build()).build();
}
}

View File

@@ -26,7 +26,6 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -37,7 +36,6 @@ import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(OutputCaptureExtension.class)
public class BatchJobApplicationTests {
@Test
public void testBatchJobApp(CapturedOutput capturedOutput) throws Exception {
final String JOB_RUN_MESSAGE = " was run";
@@ -58,7 +56,6 @@ public class BatchJobApplicationTests {
assertThat(i).isGreaterThan(0);
String taskTitle = "Demo Batch Job Task";
Pattern pattern = Pattern.compile(taskTitle);
Matcher matcher = pattern.matcher(output);

View File

@@ -47,4 +47,5 @@ public class TaskRunComponent {
this.taskRunRepository.save(new TaskRunOutput("Executed at " + execDate));
logger.info("Executed at : " + execDate);
}
}

View File

@@ -31,6 +31,7 @@ import jakarta.persistence.Table;
@Entity
@Table(name = "TASK_RUN_OUTPUT")
public class TaskRunOutput {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@@ -64,4 +65,5 @@ public class TaskRunOutput {
public String toString() {
return "TaskRunOutput{" + "id=" + this.id + ", output='" + this.output + '\'' + '}';
}
}

View File

@@ -23,4 +23,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @author Glenn Renfro
*/
public interface TaskRunRepository extends JpaRepository<TaskRunOutput, Long> {
}

View File

@@ -27,7 +27,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
@@ -59,11 +58,13 @@ public class JpaApplicationTests {
static {
randomPort = TestSocketUtils.findAvailableTcpPort();
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort + "/mem:dataflow;DB_CLOSE_DELAY=-1;"
+ "DB_CLOSE_ON_EXIT=FALSE";
+ "DB_CLOSE_ON_EXIT=FALSE";
}
private ConfigurableApplicationContext context;
private DataSource dataSource;
private Server server;
@BeforeEach
@@ -76,9 +77,8 @@ public class JpaApplicationTests {
this.dataSource = dataSource;
try {
this.server = Server
.createTcpServer("-tcp", "-ifNotExists", "-tcpAllowOthers", "-tcpPort", String
.valueOf(randomPort))
.start();
.createTcpServer("-tcp", "-ifNotExists", "-tcpAllowOthers", "-tcpPort", String.valueOf(randomPort))
.start();
}
catch (SQLException e) {
throw new IllegalStateException(e);
@@ -96,18 +96,14 @@ public class JpaApplicationTests {
@Test
public void testBatchJobApp(CapturedOutput capturedOutput) {
final String INSERT_MESSAGE = "Hibernate: insert into task_run_output (";
this.context = SpringApplication
.run(JpaApplication.class, "--spring.datasource.url=" + DATASOURCE_URL,
this.context = SpringApplication.run(JpaApplication.class, "--spring.datasource.url=" + DATASOURCE_URL,
"--spring.datasource.username=" + DATASOURCE_USER_NAME,
"--spring.datasource.driverClassName=" + DATASOURCE_DRIVER_CLASS_NAME,
"--spring.jpa.database-platform=org.hibernate.dialect.H2Dialect");
String output = capturedOutput.toString();
assertThat(output
.contains(INSERT_MESSAGE)).as("Unable to find the insert message: " + output)
.isTrue();
assertThat(output.contains(INSERT_MESSAGE)).as("Unable to find the insert message: " + output).isTrue();
JdbcTemplate template = new JdbcTemplate(this.dataSource);
Map<String, Object> result = template
.queryForMap("Select * from TASK_RUN_OUTPUT");
Map<String, Object> result = template.queryForMap("Select * from TASK_RUN_OUTPUT");
assertThat(result.get("ID")).isEqualTo(1L);
assertThat(((String) result.get("OUTPUT"))).contains("Executed at");
}

View File

@@ -20,7 +20,6 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.task.configuration.EnableTask;
/**
* @author Michael Minella
*/

View File

@@ -33,4 +33,5 @@ public class CustomTaskConfigurer extends DefaultTaskConfigurer {
public CustomTaskConfigurer(@Qualifier("secondDataSource") DataSource dataSource) {
super(dataSource);
}
}

View File

@@ -36,15 +36,12 @@ public class EmbeddedDataSourceConfiguration {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.build();
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).build();
}
@Bean
public DataSource secondDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
}
}

View File

@@ -31,6 +31,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
/**
* Creates two data sources that use external databases.
*
* @author Glenn Renfro
*/
@Configuration(proxyBeanMethods = false)
@@ -52,20 +53,19 @@ public class ExternalDataSourceConfiguration {
@Bean(name = "springDataSource")
@Primary
public DataSource dataSource(@Qualifier("springDataSourceProperties")DataSourceProperties springDataSourceProperties) {
return DataSourceBuilder.create().driverClassName(springDataSourceProperties.getDriverClassName()).
url(springDataSourceProperties.getUrl()).
password(springDataSourceProperties.getPassword()).
username(springDataSourceProperties.getUsername()).
build();
public DataSource dataSource(
@Qualifier("springDataSourceProperties") DataSourceProperties springDataSourceProperties) {
return DataSourceBuilder.create().driverClassName(springDataSourceProperties.getDriverClassName())
.url(springDataSourceProperties.getUrl()).password(springDataSourceProperties.getPassword())
.username(springDataSourceProperties.getUsername()).build();
}
@Bean
public DataSource secondDataSource(@Qualifier("secondDataSourceProperties") DataSourceProperties secondDataSourceProperties) {
return DataSourceBuilder.create().driverClassName(secondDataSourceProperties.getDriverClassName()).
url(secondDataSourceProperties.getUrl()).
password(secondDataSourceProperties.getPassword()).
username(secondDataSourceProperties.getUsername()).
build();
public DataSource secondDataSource(
@Qualifier("secondDataSourceProperties") DataSourceProperties secondDataSourceProperties) {
return DataSourceBuilder.create().driverClassName(secondDataSourceProperties.getDriverClassName())
.url(secondDataSourceProperties.getUrl()).password(secondDataSourceProperties.getPassword())
.username(secondDataSourceProperties.getUsername()).build();
}
}

View File

@@ -39,7 +39,7 @@ public class SampleCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("There are " + this.dataSources.size() +
" DataSources within this application");
System.out.println("There are " + this.dataSources.size() + " DataSources within this application");
}
}

View File

@@ -16,7 +16,6 @@
package io.spring;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -40,10 +39,11 @@ public class MultiDataSourcesApplicationTests {
String output = capturedOutput.toString();
assertThat(output.contains("There are 2 DataSources within this application"))
.as("Unable to find CommandLineRunner output: " + output).isTrue();
assertThat(output.contains("Creating: TaskExecution{"))
.as("Unable to find start task message: " + output).isTrue();
assertThat(output.contains("Updating: TaskExecution"))
.as("Unable to find update task message: " + output).isTrue();
.as("Unable to find CommandLineRunner output: " + output).isTrue();
assertThat(output.contains("Creating: TaskExecution{")).as("Unable to find start task message: " + output)
.isTrue();
assertThat(output.contains("Updating: TaskExecution")).as("Unable to find update task message: " + output)
.isTrue();
}
}

View File

@@ -16,7 +16,6 @@
package io.spring;
import java.sql.SQLException;
import org.h2.tools.Server;
@@ -37,9 +36,10 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Glenn Renfro
*/
@ExtendWith({OutputCaptureExtension.class, SpringExtension.class})
@ExtendWith({ OutputCaptureExtension.class, SpringExtension.class })
@SpringBootTest(classes = { MultiDataSourcesExternalApplicationTests.TaskLauncherConfiguration.class })
public class MultiDataSourcesExternalApplicationTests {
private final static String DATASOURCE_URL;
private final static String SECOND_DATASOURCE_URL;
@@ -56,35 +56,33 @@ public class MultiDataSourcesExternalApplicationTests {
static {
randomPort = TestSocketUtils.findAvailableTcpPort();
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort
+ "/mem:dataflow;DB_CLOSE_DELAY=-1;" + "DB_CLOSE_ON_EXIT=FALSE";
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort + "/mem:dataflow;DB_CLOSE_DELAY=-1;"
+ "DB_CLOSE_ON_EXIT=FALSE";
secondRandomPort = TestSocketUtils.findAvailableTcpPort();
SECOND_DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort
+ "/mem:dataflow;DB_CLOSE_DELAY=-1;" + "DB_CLOSE_ON_EXIT=FALSE";
SECOND_DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort + "/mem:dataflow;DB_CLOSE_DELAY=-1;"
+ "DB_CLOSE_ON_EXIT=FALSE";
}
@Test
public void testTimeStampApp(CapturedOutput capturedOutput) throws Exception {
SpringApplication.run(MultipleDataSourcesApplication.class, "--spring.profiles.active=external",
"--spring.datasource.url=" + DATASOURCE_URL,
"--spring.datasource.username=" + DATASOURCE_USER_NAME,
"--spring.datasource.password=" + DATASOURCE_USER_PASSWORD,
"--spring.datasource.driverClassName=" + DATASOURCE_DRIVER_CLASS_NAME,
"--second.datasource.url=" + SECOND_DATASOURCE_URL,
"--second.datasource.username=" + DATASOURCE_USER_NAME,
"--second.datasource.password=" + DATASOURCE_USER_PASSWORD,
"--second.datasource.driverClassName=" + DATASOURCE_DRIVER_CLASS_NAME);
"--spring.datasource.url=" + DATASOURCE_URL, "--spring.datasource.username=" + DATASOURCE_USER_NAME,
"--spring.datasource.password=" + DATASOURCE_USER_PASSWORD,
"--spring.datasource.driverClassName=" + DATASOURCE_DRIVER_CLASS_NAME,
"--second.datasource.url=" + SECOND_DATASOURCE_URL,
"--second.datasource.username=" + DATASOURCE_USER_NAME,
"--second.datasource.password=" + DATASOURCE_USER_PASSWORD,
"--second.datasource.driverClassName=" + DATASOURCE_DRIVER_CLASS_NAME);
String output = capturedOutput.toString();
assertThat(output.contains("There are 2 DataSources within this application"))
.as("Unable to find CommandLineRunner output: " + output).isTrue();
assertThat(output.contains("Creating: TaskExecution{"))
.as("Unable to find start task message: " + output).isTrue();
assertThat(output.contains("Updating: TaskExecution"))
.as("Unable to find update task message: " + output).isTrue();
.as("Unable to find CommandLineRunner output: " + output).isTrue();
assertThat(output.contains("Creating: TaskExecution{")).as("Unable to find start task message: " + output)
.isTrue();
assertThat(output.contains("Updating: TaskExecution")).as("Unable to find update task message: " + output)
.isTrue();
}
@Configuration(proxyBeanMethods = false)
@@ -99,9 +97,8 @@ public class MultiDataSourcesExternalApplicationTests {
Server server = null;
try {
if (defaultServer == null) {
server = Server.createTcpServer("-ifNotExists", "-tcp",
"-tcpAllowOthers", "-tcpPort", String.valueOf(randomPort))
.start();
server = Server.createTcpServer("-ifNotExists", "-tcp", "-tcpAllowOthers", "-tcpPort",
String.valueOf(randomPort)).start();
defaultServer = server;
}
}
@@ -116,9 +113,8 @@ public class MultiDataSourcesExternalApplicationTests {
Server server = null;
try {
if (secondServer == null) {
server = Server.createTcpServer("-ifNotExists", "-tcp",
"-tcpAllowOthers", "-tcpPort", String.valueOf(secondRandomPort))
.start();
server = Server.createTcpServer("-ifNotExists", "-tcp", "-tcpAllowOthers", "-tcpPort",
String.valueOf(secondRandomPort)).start();
secondServer = server;
}
}
@@ -127,5 +123,7 @@ public class MultiDataSourcesExternalApplicationTests {
}
return secondServer;
}
}
}

View File

@@ -63,50 +63,53 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
public class JobConfiguration {
private static final int GRID_SIZE = 4;
// @checkstyle:off
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public DataSource dataSource;
@Autowired
public JobRepository jobRepository;
// @checkstyle:on
@Autowired
private ConfigurableApplicationContext context;
@Autowired
private DelegatingResourceLoader resourceLoader;
@Autowired
private Environment environment;
@Bean
public PartitionHandler partitionHandler(TaskLauncher taskLauncher, JobExplorer jobExplorer,
TaskRepository taskRepository, @Autowired(required = false) ThreadPoolTaskExecutor executor) throws Exception {
TaskRepository taskRepository, @Autowired(required = false) ThreadPoolTaskExecutor executor)
throws Exception {
Resource resource = this.resourceLoader
.getResource("maven://io.spring.cloud:partitioned-batch-job:3.0.0-SNAPSHOT");
.getResource("maven://io.spring.cloud:partitioned-batch-job:3.0.0-SNAPSHOT");
DeployerPartitionHandler partitionHandler =
new DeployerPartitionHandler(taskLauncher, jobExplorer, resource,
DeployerPartitionHandler partitionHandler = new DeployerPartitionHandler(taskLauncher, jobExplorer, resource,
"workerStep", taskRepository, executor);
List<String> commandLineArgs = new ArrayList<>(3);
commandLineArgs.add("--spring.profiles.active=worker");
commandLineArgs.add("--spring.cloud.task.initialize-enabled=false");
commandLineArgs.add("--spring.batch.initializer.enabled=false");
partitionHandler
.setCommandLineArgsProvider(new PassThroughCommandLineArgsProvider(commandLineArgs));
partitionHandler
.setEnvironmentVariablesProvider(new SimpleEnvironmentVariablesProvider(this.environment));
partitionHandler.setCommandLineArgsProvider(new PassThroughCommandLineArgsProvider(commandLineArgs));
partitionHandler.setEnvironmentVariablesProvider(new SimpleEnvironmentVariablesProvider(this.environment));
partitionHandler.setMaxWorkers(2);
partitionHandler.setApplicationName("PartitionedBatchJobTask");
return partitionHandler;
}
@ConditionalOnProperty( value="io.spring.asynchronous",
havingValue = "true",
matchIfMissing = false)
@ConditionalOnProperty(value = "io.spring.asynchronous", havingValue = "true", matchIfMissing = false)
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
@@ -145,8 +148,7 @@ public class JobConfiguration {
@Bean
@StepScope
public Tasklet workerTasklet(
final @Value("#{stepExecutionContext['partitionNumber']}") Integer partitionNumber) {
public Tasklet workerTasklet(final @Value("#{stepExecutionContext['partitionNumber']}") Integer partitionNumber) {
return new Tasklet() {
@Override
@@ -160,26 +162,20 @@ public class JobConfiguration {
@Bean
public Step step1(PartitionHandler partitionHandler) throws Exception {
return this.stepBuilderFactory.get("step1")
.partitioner(workerStep().getName(), partitioner())
.step(workerStep())
.partitionHandler(partitionHandler)
.build();
return this.stepBuilderFactory.get("step1").partitioner(workerStep().getName(), partitioner())
.step(workerStep()).partitionHandler(partitionHandler).build();
}
@Bean
public Step workerStep() {
return this.stepBuilderFactory.get("workerStep")
.tasklet(workerTasklet(null))
.build();
return this.stepBuilderFactory.get("workerStep").tasklet(workerTasklet(null)).build();
}
@Bean
@Profile("!worker")
public Job partitionedJob(PartitionHandler partitionHandler) throws Exception {
Random random = new Random();
return this.jobBuilderFactory.get("partitionedJob" + random.nextInt())
.start(step1(partitionHandler))
.build();
return this.jobBuilderFactory.get("partitionedJob" + random.nextInt()).start(step1(partitionHandler)).build();
}
}

View File

@@ -29,4 +29,5 @@ public class PartitionedBatchJobApplication {
public static void main(String[] args) {
SpringApplication.run(PartitionedBatchJobApplication.class, args);
}
}

View File

@@ -46,22 +46,27 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {TaskPartitionerTests.TaskLauncherConfiguration.class})
@SpringBootTest(classes = { TaskPartitionerTests.TaskLauncherConfiguration.class })
public class TaskPartitionerTests {
private final static String DATASOURCE_USER_NAME = "SA";
private final static String DATASOURCE_USER_PASSWORD = "";
private final static String DATASOURCE_DRIVER_CLASS_NAME = "org.h2.Driver";
private static String DATASOURCE_URL;
private static int randomPort;
static {
randomPort = TestSocketUtils.findAvailableTcpPort();
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort + "/mem:dataflow;DB_CLOSE_DELAY=-1;"
+ "DB_CLOSE_ON_EXIT=FALSE";
+ "DB_CLOSE_ON_EXIT=FALSE";
}
private TaskExplorer taskExplorer;
@Autowired
private DataSource dataSource;
@@ -100,16 +105,12 @@ public class TaskPartitionerTests {
app.setDefaultProperties(properties);
app.run();
Page<TaskExecution> taskExecutions = this.taskExplorer
.findAll(PageRequest.of(0, 10));
assertThat(taskExecutions.getTotalElements()).as("Five rows are expected")
.isEqualTo(5);
assertThat(this.taskExplorer
.getTaskExecutionCountByTaskName("PartitionedBatchJobTask"))
.as("Only One master is expected").isEqualTo(1);
Page<TaskExecution> taskExecutions = this.taskExplorer.findAll(PageRequest.of(0, 10));
assertThat(taskExecutions.getTotalElements()).as("Five rows are expected").isEqualTo(5);
assertThat(this.taskExplorer.getTaskExecutionCountByTaskName("PartitionedBatchJobTask"))
.as("Only One master is expected").isEqualTo(1);
for (TaskExecution taskExecution : taskExecutions) {
assertThat(taskExecution.getExitCode()
.intValue()).as("return code should be 0").isEqualTo(0);
assertThat(taskExecution.getExitCode().intValue()).as("return code should be 0").isEqualTo(0);
}
}
@@ -120,10 +121,8 @@ public class TaskPartitionerTests {
public org.h2.tools.Server initH2TCPServer() {
Server server;
try {
server = Server
.createTcpServer("-tcp", "-ifNotExists", "-tcpAllowOthers", "-tcpPort", String
.valueOf(randomPort))
.start();
server = Server.createTcpServer("-tcp", "-ifNotExists", "-tcpAllowOthers", "-tcpPort",
String.valueOf(randomPort)).start();
}
catch (SQLException e) {
throw new IllegalStateException(e);
@@ -140,6 +139,7 @@ public class TaskPartitionerTests {
dataSource.setPassword(DATASOURCE_USER_PASSWORD);
return dataSource;
}
}
}

View File

@@ -29,4 +29,5 @@ public class SingleStepBatchJobApplication {
public static void main(String[] args) {
SpringApplication.run(SingleStepBatchJobApplication.class, args);
}
}

View File

@@ -64,8 +64,8 @@ public class BatchJobApplicationTests {
static {
randomPort = TestSocketUtils.findAvailableTcpPort();
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort
+ "/mem:dataflow;DB_CLOSE_DELAY=-1;" + "DB_CLOSE_ON_EXIT=FALSE";
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort + "/mem:dataflow;DB_CLOSE_DELAY=-1;"
+ "DB_CLOSE_ON_EXIT=FALSE";
}
private File outputFile;
@@ -90,45 +90,36 @@ public class BatchJobApplicationTests {
@Test
public void testFileReaderJdbcWriter() throws Exception {
getSpringApplication().run(SingleStepBatchJobApplication.class,
"--spring.profiles.active=ffreader,jdbcwriter",
"--spring.datasource.username=" + DATASOURCE_USER_NAME,
"--spring.datasource.url=" + DATASOURCE_URL,
"--spring.datasource.driver-class-name=" + DATASOURCE_DRIVER_CLASS_NAME,
"--spring.datasource.password=" + DATASOURCE_USER_PASSWORD,
"foo=testFileReaderJdbcWriter");
getSpringApplication().run(SingleStepBatchJobApplication.class, "--spring.profiles.active=ffreader,jdbcwriter",
"--spring.datasource.username=" + DATASOURCE_USER_NAME, "--spring.datasource.url=" + DATASOURCE_URL,
"--spring.datasource.driver-class-name=" + DATASOURCE_DRIVER_CLASS_NAME,
"--spring.datasource.password=" + DATASOURCE_USER_PASSWORD, "foo=testFileReaderJdbcWriter");
validateDBResult();
}
@Test
public void testJdbcReaderJdbcWriter() throws Exception {
getSpringApplication().run(SingleStepBatchJobApplication.class,
"--spring.profiles.active=jdbcreader,jdbcwriter",
"--spring.datasource.username=" + DATASOURCE_USER_NAME,
"--spring.datasource.url=" + DATASOURCE_URL,
"--spring.datasource.driver-class-name=" + DATASOURCE_DRIVER_CLASS_NAME,
"--spring.datasource.password=" + DATASOURCE_USER_PASSWORD,
"foo=testJdbcReaderJdbcWriter");
"--spring.profiles.active=jdbcreader,jdbcwriter",
"--spring.datasource.username=" + DATASOURCE_USER_NAME, "--spring.datasource.url=" + DATASOURCE_URL,
"--spring.datasource.driver-class-name=" + DATASOURCE_DRIVER_CLASS_NAME,
"--spring.datasource.password=" + DATASOURCE_USER_PASSWORD, "foo=testJdbcReaderJdbcWriter");
validateDBResult();
}
@Test
public void testJdbcReaderFlatfileWriter() throws Exception {
getSpringApplication().run(SingleStepBatchJobApplication.class,
"--spring.profiles.active=jdbcreader,ffwriter",
"--spring.datasource.username=" + DATASOURCE_USER_NAME,
"--spring.datasource.url=" + DATASOURCE_URL,
"--spring.datasource.driver-class-name=" + DATASOURCE_DRIVER_CLASS_NAME,
"--spring.datasource.password=" + DATASOURCE_USER_PASSWORD,
"foo=testJdbcReaderFlatfileWriter");
getSpringApplication().run(SingleStepBatchJobApplication.class, "--spring.profiles.active=jdbcreader,ffwriter",
"--spring.datasource.username=" + DATASOURCE_USER_NAME, "--spring.datasource.url=" + DATASOURCE_URL,
"--spring.datasource.driver-class-name=" + DATASOURCE_DRIVER_CLASS_NAME,
"--spring.datasource.password=" + DATASOURCE_USER_PASSWORD, "foo=testJdbcReaderFlatfileWriter");
validateFileResult();
}
@Test
public void testFileReaderFileWriter() throws Exception {
getSpringApplication().run(SingleStepBatchJobApplication.class,
"--spring.profiles.active=ffreader,ffwriter",
"foo=testFileReaderFileWriter");
getSpringApplication().run(SingleStepBatchJobApplication.class, "--spring.profiles.active=ffreader,ffwriter",
"foo=testFileReaderFileWriter");
validateFileResult();
}
@@ -136,36 +127,33 @@ public class BatchJobApplicationTests {
Server server;
if (defaultServer == null) {
server = Server.createTcpServer("-ifNotExists", "-tcp",
"-tcpAllowOthers", "-tcpPort", String.valueOf(randomPort))
.start();
server = Server
.createTcpServer("-ifNotExists", "-tcp", "-tcpAllowOthers", "-tcpPort", String.valueOf(randomPort))
.start();
defaultServer = server;
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DATASOURCE_DRIVER_CLASS_NAME);
dataSource.setUrl(DATASOURCE_URL);
dataSource.setUsername(DATASOURCE_USER_NAME);
dataSource.setPassword(DATASOURCE_USER_PASSWORD);
ClassPathResource setupResource = new ClassPathResource(
"schema-h2.sql");
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator(
setupResource);
ClassPathResource setupResource = new ClassPathResource("schema-h2.sql");
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator(setupResource);
resourceDatabasePopulator.execute(dataSource);
}
return defaultServer;
}
private void validateFileResult() throws Exception{
// AssertFile.assertLineCount(6, new FileSystemResource("./result.txt"));
// AssertFile.assertFileEquals(new ClassPathResource("testresult.txt"),
// new FileSystemResource(this.outputFile));
private void validateFileResult() throws Exception {
// AssertFile.assertLineCount(6, new FileSystemResource("./result.txt"));
// AssertFile.assertFileEquals(new ClassPathResource("testresult.txt"),
// new FileSystemResource(this.outputFile));
}
private void validateDBResult() {
DataSource dataSource = getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List<Map<String, Object>> result = jdbcTemplate
.queryForList("SELECT item_name FROM item ORDER BY item_name");
List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT item_name FROM item ORDER BY item_name");
assertThat(result.size()).isEqualTo(6);
assertThat(result.get(0).get("item_name")).isEqualTo("Job");
@@ -184,6 +172,7 @@ public class BatchJobApplicationTests {
dataSourceBuilder.password(DATASOURCE_USER_PASSWORD);
return dataSourceBuilder.build();
}
private SpringApplication getSpringApplication() {
SpringApplication springApplication = new SpringApplication();
Map<String, Object> properties = new HashMap<>();

View File

@@ -43,5 +43,7 @@ public class TaskEventsApplication {
}
};
}
}
}

View File

@@ -23,8 +23,10 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class ObservationConfiguration {
@Bean
public SimpleMeterRegistry meterRegistry() {
return new SimpleMeterRegistry();
}
}

View File

@@ -32,12 +32,12 @@ class TaskObservationsApplicationTests {
@Test
void contextLoads(CapturedOutput output) {
String result = output.getAll();
assertThat(result).contains("spring.cloud.task(TIMER)[application='task-observations-application-58', " +
"error='none', service='task-observations-application', " +
"spring.cloud.task.execution.id='1', spring.cloud.task.exit.code='0', " +
"spring.cloud.task.external.execution.id='unknown', spring.cloud.task.name='taskmetrics', " +
"spring.cloud.task.parent.execution.id='unknown', spring.cloud.task.status='success']; " +
"count=1.0, total_time=");
assertThat(result).contains("spring.cloud.task(TIMER)[application='task-observations-application-58', "
+ "error='none', service='task-observations-application', "
+ "spring.cloud.task.execution.id='1', spring.cloud.task.exit.code='0', "
+ "spring.cloud.task.external.execution.id='unknown', spring.cloud.task.name='taskmetrics', "
+ "spring.cloud.task.parent.execution.id='unknown', spring.cloud.task.status='success']; "
+ "count=1.0, total_time=");
}
}

View File

@@ -48,30 +48,25 @@ public class TaskProcessor {
String message = messagePayload.getPayload();
Map<String, String> properties = new HashMap<>();
if (StringUtils.hasText(this.processorProperties.getDataSourceUrl())) {
properties
.put("spring_datasource_url", this.processorProperties
.getDataSourceUrl());
properties.put("spring_datasource_url", this.processorProperties.getDataSourceUrl());
}
if (StringUtils
.hasText(this.processorProperties.getDataSourceDriverClassName())) {
properties.put("spring_datasource_driverClassName", this.processorProperties
.getDataSourceDriverClassName());
if (StringUtils.hasText(this.processorProperties.getDataSourceDriverClassName())) {
properties.put("spring_datasource_driverClassName",
this.processorProperties.getDataSourceDriverClassName());
}
if (StringUtils.hasText(this.processorProperties.getDataSourceUserName())) {
properties.put("spring_datasource_username", this.processorProperties
.getDataSourceUserName());
properties.put("spring_datasource_username", this.processorProperties.getDataSourceUserName());
}
if (StringUtils.hasText(this.processorProperties.getDataSourcePassword())) {
properties.put("spring_datasource_password", this.processorProperties
.getDataSourcePassword());
properties.put("spring_datasource_password", this.processorProperties.getDataSourcePassword());
}
properties.put("payload", message);
TaskLaunchRequest request = new TaskLaunchRequest(
this.processorProperties.getUri(), null, properties, null,
this.processorProperties.getApplicationName());
TaskLaunchRequest request = new TaskLaunchRequest(this.processorProperties.getUri(), null, properties, null,
this.processorProperties.getApplicationName());
return MessageBuilder.withPayload(request).build();
};
}
}

View File

@@ -28,4 +28,5 @@ public class TaskProcessorApplication {
public static void main(String[] args) {
SpringApplication.run(TaskProcessorApplication.class, args);
}
}

View File

@@ -25,8 +25,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
public class TaskProcessorProperties {
private static final String DEFAULT_URI = "maven://org.springframework.cloud.task.app:"
+ "timestamp-task:jar:1.0.1.RELEASE";
+ "timestamp-task:jar:1.0.1.RELEASE";
private String uri = DEFAULT_URI;
@@ -40,7 +39,6 @@ public class TaskProcessorProperties {
private String applicationName;
public String getDataSourceUrl() {
return this.dataSourceUrl;
}
@@ -88,4 +86,5 @@ public class TaskProcessorProperties {
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
}

View File

@@ -23,7 +23,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
@@ -53,6 +52,7 @@ public class TaskProcessorApplicationTests {
private static final String DEFAULT_PAYLOAD = "hello";
private final ObjectMapper objectMapper = new ObjectMapper();
private ConfigurableApplicationContext applicationContext;
@BeforeEach
@@ -72,22 +72,19 @@ public class TaskProcessorApplicationTests {
Map<String, String> properties = new HashMap();
properties.put("payload", DEFAULT_PAYLOAD);
TaskLaunchRequest expectedRequest = new TaskLaunchRequest(
"maven://org.springframework.cloud.task.app:"
+ "timestamp-task:jar:1.0.1.RELEASE", null, properties,
null, null);
"maven://org.springframework.cloud.task.app:" + "timestamp-task:jar:1.0.1.RELEASE", null, properties,
null, null);
List<Message<byte[]>> result = testListener("output", 1);
TaskLaunchRequest tlq = objectMapper.readValue(result.get(0).getPayload(), TaskLaunchRequest.class);
assertThat(tlq).isEqualTo(expectedRequest);
}
private List<Message<byte[]>> testListener(String bindingName, int numberToRead) {
List<Message<byte[]>> results = new ArrayList<>();
this.applicationContext = new SpringApplicationBuilder()
.sources(TestChannelBinderConfiguration
.getCompleteConfiguration(TaskProcessorTestApplication.class)).web(WebApplicationType.NONE)
.run();
.sources(TestChannelBinderConfiguration.getCompleteConfiguration(TaskProcessorTestApplication.class))
.web(WebApplicationType.NONE).run();
InputDestination input = this.applicationContext.getBean(InputDestination.class);
OutputDestination target = this.applicationContext.getBean(OutputDestination.class);
@@ -99,7 +96,9 @@ public class TaskProcessorApplicationTests {
}
@SpringBootApplication
@Import({TaskProcessor.class})
@Import({ TaskProcessor.class })
public static class TaskProcessorTestApplication {
}
}

View File

@@ -32,4 +32,5 @@ public class TaskSinkApplication {
public static void main(String[] args) {
SpringApplication.run(TaskSinkApplication.class, args);
}
}

View File

@@ -53,30 +53,26 @@ public class TaskSinkApplicationTests {
@Test
public void testLaunch() {
TaskLauncher testTaskLauncher =
this.context.getBean(TaskLauncher.class);
TaskLauncher testTaskLauncher = this.context.getBean(TaskLauncher.class);
Map<String, String> properties = new HashMap();
properties.put("server.port", "0");
TaskLaunchRequest request = new TaskLaunchRequest(
"maven://org.springframework.cloud.task.app:"
+ "timestamp-task:jar:1.0.1.RELEASE", null, properties,
null, null);
"maven://org.springframework.cloud.task.app:" + "timestamp-task:jar:1.0.1.RELEASE", null, properties,
null, null);
GenericMessage<TaskLaunchRequest> message = new GenericMessage<>(request);
this.streamBridge.send("taskLauncherSink-in-0", message);
ArgumentCaptor<AppDeploymentRequest> deploymentRequest = ArgumentCaptor
.forClass(AppDeploymentRequest.class);
ArgumentCaptor<AppDeploymentRequest> deploymentRequest = ArgumentCaptor.forClass(AppDeploymentRequest.class);
verify(testTaskLauncher).launch(deploymentRequest.capture());
AppDeploymentRequest actualRequest = deploymentRequest.getValue();
assertThat(actualRequest.getCommandlineArguments().isEmpty()).isTrue();
assertThat(actualRequest.getDefinition().getProperties()
.get("server.port")).isEqualTo("0");
assertThat(actualRequest.getDefinition().getProperties().get("server.port")).isEqualTo("0");
assertThat(actualRequest.getResource().toString()
.contains("org.springframework.cloud.task.app:timestamp-task:jar:1.0.1.RELEASE"))
.isTrue();
.contains("org.springframework.cloud.task.app:timestamp-task:jar:1.0.1.RELEASE")).isTrue();
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.task.timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -37,7 +36,7 @@ import org.springframework.context.annotation.Bean;
*/
@EnableTask
@SpringBootApplication
@EnableConfigurationProperties({TimestampTaskProperties.class})
@EnableConfigurationProperties({ TimestampTaskProperties.class })
public class TaskApplication {
private static final Log logger = LogFactory.getLog(TaskApplication.class);
@@ -64,5 +63,7 @@ public class TaskApplication {
DateFormat dateFormat = new SimpleDateFormat(this.config.getFormat());
logger.info(dateFormat.format(new Date()));
}
}
}

View File

@@ -38,4 +38,5 @@ public class TimestampTaskProperties {
public void setFormat(String format) {
this.format = format;
}
}

View File

@@ -36,26 +36,23 @@ import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(OutputCaptureExtension.class)
public class TaskApplicationTests {
@Test
public void testTimeStampApp(CapturedOutput capturedOutput) throws Exception {
final String TEST_DATE_DOTS = ".......";
final String CREATE_TASK_MESSAGE = "Creating: TaskExecution{executionId=";
final String UPDATE_TASK_MESSAGE = "Updating: TaskExecution with executionId=";
final String EXIT_CODE_MESSAGE = "with the following {exitCode=0";
String[] args = {"--format=yyyy" + TEST_DATE_DOTS};
String[] args = { "--format=yyyy" + TEST_DATE_DOTS };
SpringApplication.run(TaskApplication.class, args);
String output = capturedOutput.toString();
assertThat(output.contains(TEST_DATE_DOTS))
.as("Unable to find the timestamp: " + output).isTrue();
assertThat(output.contains(CREATE_TASK_MESSAGE))
.as("Test results do not show create task message: " + output).isTrue();
assertThat(output.contains(UPDATE_TASK_MESSAGE))
.as("Test results do not show success message: " + output).isTrue();
assertThat(output.contains(EXIT_CODE_MESSAGE))
.as("Test results have incorrect exit code: " + output).isTrue();
assertThat(output.contains(TEST_DATE_DOTS)).as("Unable to find the timestamp: " + output).isTrue();
assertThat(output.contains(CREATE_TASK_MESSAGE)).as("Test results do not show create task message: " + output)
.isTrue();
assertThat(output.contains(UPDATE_TASK_MESSAGE)).as("Test results do not show success message: " + output)
.isTrue();
assertThat(output.contains(EXIT_CODE_MESSAGE)).as("Test results have incorrect exit code: " + output).isTrue();
String taskTitle = "Demo Timestamp Task";
Pattern pattern = Pattern.compile(taskTitle);
@@ -64,7 +61,7 @@ public class TaskApplicationTests {
while (matcher.find()) {
count++;
}
assertThat(count).as("The number of task titles did not match expected: ")
.isEqualTo(1);
assertThat(count).as("The number of task titles did not match expected: ").isEqualTo(1);
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.task.timestamp;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -39,8 +38,7 @@ public class TimestampTaskPropertiesTests {
testPropertyValues.applyTo(context);
context.register(Conf.class);
context.refresh();
TimestampTaskProperties properties = context
.getBean(TimestampTaskProperties.class);
TimestampTaskProperties properties = context.getBean(TimestampTaskProperties.class);
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
properties.getFormat();
});
@@ -51,10 +49,9 @@ public class TimestampTaskPropertiesTests {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(Conf.class);
context.refresh();
TimestampTaskProperties properties = context
.getBean(TimestampTaskProperties.class);
TimestampTaskProperties properties = context.getBean(TimestampTaskProperties.class);
assertThat(properties.getFormat()).as("result does not match default format.")
.isEqualTo("yyyy-MM-dd HH:mm:ss.SSS");
.isEqualTo("yyyy-MM-dd HH:mm:ss.SSS");
}
@Test
@@ -63,15 +60,15 @@ public class TimestampTaskPropertiesTests {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(Conf.class);
context.refresh();
TimestampTaskProperties properties = context
.getBean(TimestampTaskProperties.class);
TimestampTaskProperties properties = context.getBean(TimestampTaskProperties.class);
properties.setFormat(FORMAT);
assertThat(properties.getFormat()).as("result does not match established format.")
.isEqualTo(FORMAT);
assertThat(properties.getFormat()).as("result does not match established format.").isEqualTo(FORMAT);
}
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(TimestampTaskProperties.class)
static class Conf {
}
}