Skip to content

Commit

Permalink
Fixed manual wiring of JsrJobOperator
Browse files Browse the repository at this point in the history
While the JSR-352 provides only one way to access the JobOperator
(BatchRuntime.getJobOperator()), it is useful for testing and embedding
to be able to wire your own JsrJobOperator instance.  This commit
addresses previous issues with using the constructor that provided that
functionality.

Note: This is a breaking change in that there is a new parameter on the
non-default constructor (adding a PlatformTransactionManager reference).
Users using the JsrJobOperator through the BatchRuntime as previously
mentioned should not be impacted.  Since this is the *only* method
perscribed by the JSR to consume that class, it is not expected to have
a large impact.

This fix partially addresses BATCH-2290.
  • Loading branch information
mminella committed Dec 22, 2014
1 parent df543c2 commit d3410c9
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 34 deletions.
Expand Up @@ -81,6 +81,7 @@
import org.springframework.core.io.Resource;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -144,7 +145,8 @@ public class JsrJobOperator implements JobOperator, InitializingBean {
private JobRepository jobRepository;
private TaskExecutor taskExecutor;
private JobParametersConverter jobParametersConverter;
private static ApplicationContext baseContext;
private ApplicationContext baseContext;
private PlatformTransactionManager transactionManager;
private static ExecutingJobRegistry jobRegistry = new ExecutingJobRegistry();

/**
Expand Down Expand Up @@ -174,14 +176,16 @@ public JsrJobOperator() {
* @param jobRepository an instance of Spring Batch's {@link JobOperator}
* @param jobParametersConverter an instance of Spring Batch's {@link JobParametersConverter}
*/
public JsrJobOperator(JobExplorer jobExplorer, JobRepository jobRepository, JobParametersConverter jobParametersConverter) {
public JsrJobOperator(JobExplorer jobExplorer, JobRepository jobRepository, JobParametersConverter jobParametersConverter, PlatformTransactionManager transactionManager) {
Assert.notNull(jobExplorer, "A JobExplorer is required");
Assert.notNull(jobRepository, "A JobRepository is required");
Assert.notNull(jobParametersConverter, "A ParametersConverter is required");
Assert.notNull(transactionManager, "A PlatformTransactionManager is required");

this.jobExplorer = jobExplorer;
this.jobRepository = jobRepository;
this.jobParametersConverter = jobParametersConverter;
this.transactionManager = transactionManager;
}

public void setJobExplorer(JobExplorer jobExplorer) {
Expand Down Expand Up @@ -607,7 +611,14 @@ public long start(String jobName, Properties params) throws JobStartException,
beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
batchContext.registerBeanDefinition(JSR_JOB_CONTEXT_BEAN_NAME, beanDefinition);

batchContext.setParent(baseContext);
if(baseContext != null) {
batchContext.setParent(baseContext);
} else {
batchContext.getBeanFactory().registerSingleton("jobExplorer", jobExplorer);
batchContext.getBeanFactory().registerSingleton("jobRepository", jobRepository);
batchContext.getBeanFactory().registerSingleton("jobParametersConverter", jobParametersConverter);
batchContext.getBeanFactory().registerSingleton("transactionManager", transactionManager);
}

try {
batchContext.refresh();
Expand Down
@@ -0,0 +1,61 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.jsr.configuration.xml;

import java.util.Date;
import java.util.Properties;
import java.util.concurrent.TimeoutException;

import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchStatus;
import javax.batch.runtime.JobExecution;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* @author Michael Minella
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class CustomWiredJsrJobOperatorTests {

@Autowired
JobOperator jobOperator;

@Test
public void testRunningJobWithManuallyWiredJsrJobOperator() throws Exception {
Date startTime = new Date();
long jobExecutionId = jobOperator.start("jsrJobOperatorTestJob", new Properties());

JobExecution jobExecution = jobOperator.getJobExecution(jobExecutionId);

long timeout = startTime.getTime() + 10000;

while(!jobExecution.getBatchStatus().equals(BatchStatus.COMPLETED)) {
Thread.sleep(500);
jobExecution = jobOperator.getJobExecution(jobExecutionId);

if(new Date().getTime() > timeout) {
throw new TimeoutException("Job didn't finish within 10 seconds");
}
}
}
}
Expand Up @@ -15,11 +15,39 @@
*/
package org.springframework.batch.core.jsr.launch;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import javax.batch.api.AbstractBatchlet;
import javax.batch.api.Batchlet;
import javax.batch.operations.JobExecutionIsRunningException;
import javax.batch.operations.JobOperator;
import javax.batch.operations.JobRestartException;
import javax.batch.operations.JobStartException;
import javax.batch.operations.NoSuchJobException;
import javax.batch.operations.NoSuchJobExecutionException;
import javax.batch.operations.NoSuchJobInstanceException;
import javax.batch.runtime.BatchRuntime;
import javax.batch.runtime.BatchStatus;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParametersBuilder;
Expand All @@ -32,36 +60,11 @@
import org.springframework.batch.core.jsr.JsrJobParametersConverter;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.JobRepositorySupport;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.task.SyncTaskExecutor;

import javax.batch.api.AbstractBatchlet;
import javax.batch.api.Batchlet;
import javax.batch.operations.JobExecutionIsRunningException;
import javax.batch.operations.JobOperator;
import javax.batch.operations.JobRestartException;
import javax.batch.operations.JobStartException;
import javax.batch.operations.NoSuchJobException;
import javax.batch.operations.NoSuchJobExecutionException;
import javax.batch.operations.NoSuchJobInstanceException;
import javax.batch.runtime.BatchRuntime;
import javax.batch.runtime.BatchStatus;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class JsrJobOperatorTests extends AbstractJsrTestCase {

private JobOperator jsrJobOperator;
Expand All @@ -76,7 +79,7 @@ public class JsrJobOperatorTests extends AbstractJsrTestCase {
public void setup() {
MockitoAnnotations.initMocks(this);
parameterConverter = new JobParametersConverterSupport();
jsrJobOperator = new JsrJobOperator(jobExplorer, jobRepository, parameterConverter);
jsrJobOperator = new JsrJobOperator(jobExplorer, jobRepository, parameterConverter, new ResourcelessTransactionManager());
}

@Test
Expand All @@ -88,24 +91,30 @@ public void testLoadingWithBatchRuntime() {
@Test
public void testNullsInConstructor() {
try {
new JsrJobOperator(null, new JobRepositorySupport(), parameterConverter);
new JsrJobOperator(null, new JobRepositorySupport(), parameterConverter, null);
fail("JobExplorer should be required");
} catch (IllegalArgumentException correct) {
}

try {
new JsrJobOperator(new SimpleJobExplorer(null, null, null, null), null, parameterConverter);
new JsrJobOperator(new SimpleJobExplorer(null, null, null, null), null, parameterConverter, null);
fail("JobRepository should be required");
} catch (IllegalArgumentException correct) {
}

try {
new JsrJobOperator(new SimpleJobExplorer(null, null, null, null), new JobRepositorySupport(), null);
new JsrJobOperator(new SimpleJobExplorer(null, null, null, null), new JobRepositorySupport(), null, null);
fail("ParameterConverter should be required");
} catch (IllegalArgumentException correct) {
}

new JsrJobOperator(new SimpleJobExplorer(null, null, null, null), new JobRepositorySupport(), parameterConverter);
try {
new JsrJobOperator(new SimpleJobExplorer(null, null, null, null), new JobRepositorySupport(), parameterConverter, null);
}
catch (IllegalArgumentException correct) {
}

new JsrJobOperator(new SimpleJobExplorer(null, null, null, null), new JobRepositorySupport(), parameterConverter, new ResourcelessTransactionManager());
}

@Test
Expand Down
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
</bean>

<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean">
<property name="repositoryFactory" ref="&amp;jobRepository"/>
</bean>

<bean id="jobParametersConverter" class="org.springframework.batch.core.converter.DefaultJobParametersConverter"/>

<bean id="jsrJobOperator" class="org.springframework.batch.core.jsr.launch.JsrJobOperator">
<constructor-arg ref="jobExplorer"/>
<constructor-arg ref="jobRepository"/>
<constructor-arg ref="jobParametersConverter"/>
<constructor-arg ref="transactionManager"/>
</bean>

</beans>

0 comments on commit d3410c9

Please sign in to comment.