From 3d78c0bcb5cc5dc43ab0df758704df7e65188546 Mon Sep 17 00:00:00 2001 From: dsyer Date: Sat, 5 Jul 2008 07:32:44 +0000 Subject: [PATCH] Dust off JMX demo --- .../batch/core/step/AbstractStep.java | 7 ++-- .../item/file/mapping/DefaultFieldSet.java | 7 ++++ .../batch/item/file/mapping/FieldSet.java | 7 ++++ .../batch/item/file/FieldSetTests.java | 2 + .../integration/job/JobExecutionRequest.java | 8 ++++ .../integration/job/MessageOrientedStep.java | 35 +++++++++++++---- ...tionRequest.java => JobLaunchRequest.java} | 4 +- .../launch/JobLaunchingMessageHandler.java | 2 +- ...unchingMessageHandlerIntegrationTests.java | 4 +- .../JobLaunchingMessageHandlerTests.java | 2 +- .../launch/JobRequestConverter.java | 4 +- .../JobExecutionNotificationPublisher.java | 2 +- ... StepExecutionApplicationEventAdvice.java} | 24 ++++++------ .../sample/tasklet/InfiniteLoopTasklet.java | 14 ++++++- .../resources/adhoc-job-launcher-context.xml | 2 +- .../src/main/resources/batch-hsql.properties | 4 +- .../src/main/resources/jobs/adhocLoopJob.xml | 39 +++++++------------ .../src/main/resources/jobs/footballJob.xml | 8 ++-- .../main/resources/jobs/multiResourceJob.xml | 14 +++---- .../resources/simple-job-launcher-context.xml | 15 +------ 20 files changed, 120 insertions(+), 84 deletions(-) rename spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/{JobExecutionRequest.java => JobLaunchRequest.java} (92%) rename spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/{MethodExecutionApplicationEventAdvice.java => StepExecutionApplicationEventAdvice.java} (67%) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java index 8344c3a1c..ebbbaf5c8 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java @@ -58,7 +58,7 @@ public abstract class AbstractStep implements Step, InitializingBean, BeanNameAw private int startLimit = Integer.MAX_VALUE; - private boolean allowStartIfComplete; + private boolean allowStartIfComplete = false; private CompositeStepExecutionListener listener = new CompositeStepExecutionListener(); @@ -122,9 +122,10 @@ public abstract class AbstractStep implements Step, InitializingBean, BeanNameAw } /** - * Public setter for the shouldAllowStartIfComplete. + * Public setter for flag that determines whether the step should start + * again if it is already complete. Defaults to false. * - * @param allowStartIfComplete the shouldAllowStartIfComplete to set + * @param allowStartIfComplete the value of the flag to set */ public void setAllowStartIfComplete(boolean allowStartIfComplete) { this.allowStartIfComplete = allowStartIfComplete; diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/DefaultFieldSet.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/DefaultFieldSet.java index 2108689bc..a69b8098c 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/DefaultFieldSet.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/DefaultFieldSet.java @@ -71,6 +71,13 @@ public class DefaultFieldSet implements FieldSet { } return (String[]) names.toArray(); } + + /* (non-Javadoc) + * @see org.springframework.batch.item.file.mapping.FieldSet#hasNames() + */ + public boolean hasNames() { + return names!=null; + } /* * (non-Javadoc) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/FieldSet.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/FieldSet.java index ad56268cf..ae8d5a3e0 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/FieldSet.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/FieldSet.java @@ -40,6 +40,13 @@ public interface FieldSet { */ String[] getNames(); + /** + * Check if there are names defined for the fields. + * + * @return true if there are names for the fields + */ + boolean hasNames(); + /** * @return fields wrapped by this 'FieldSet' instance as * String values. diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FieldSetTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FieldSetTests.java index a58cddade..0b3e8a4d6 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FieldSetTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FieldSetTests.java @@ -45,11 +45,13 @@ public class FieldSetTests extends TestCase { } public void testNames() throws Exception { + assertTrue(fieldSet.hasNames()); assertEquals(fieldSet.getFieldCount(), fieldSet.getNames().length); } public void testNamesNotKnown() throws Exception { fieldSet = new DefaultFieldSet(new String[] { "foo" }); + assertFalse(fieldSet.hasNames()); try { fieldSet.getNames(); fail("Expected IllegalStateException"); diff --git a/spring-batch-integration/src/main/java/org/springframework/batch/integration/job/JobExecutionRequest.java b/spring-batch-integration/src/main/java/org/springframework/batch/integration/job/JobExecutionRequest.java index f7d4c2199..3663d2ae5 100644 --- a/spring-batch-integration/src/main/java/org/springframework/batch/integration/job/JobExecutionRequest.java +++ b/spring-batch-integration/src/main/java/org/springframework/batch/integration/job/JobExecutionRequest.java @@ -103,5 +103,13 @@ public class JobExecutionRequest extends AttributeAccessorSupport { public JobExecution getJobExecution() { return jobExecution; } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return getClass().getSimpleName()+": "+jobExecution; + } } diff --git a/spring-batch-integration/src/main/java/org/springframework/batch/integration/job/MessageOrientedStep.java b/spring-batch-integration/src/main/java/org/springframework/batch/integration/job/MessageOrientedStep.java index d8f184d45..32861a2af 100644 --- a/spring-batch-integration/src/main/java/org/springframework/batch/integration/job/MessageOrientedStep.java +++ b/spring-batch-integration/src/main/java/org/springframework/batch/integration/job/MessageOrientedStep.java @@ -43,6 +43,27 @@ public class MessageOrientedStep extends AbstractStep { private MessageChannel replyChannel; + private int executionTimeoutMinutes = 30; + + private long pollingInterval = 5; + + /** + * Public setter for the execution timeout in minutes. Defaults to 30. + * @param executionTimeoutMinutes the timeout to set + */ + public void setExecutionTimeoutMinutes(int executionTimeoutMinutes) { + this.executionTimeoutMinutes = executionTimeoutMinutes; + } + + /** + * Public setter for the polling interval in milliseconds while waiting for + * replies signalling the end of the step. Defaults to 5. + * @param pollingInterval the polling interval to set + */ + public void setPollingInterval(long pollingInterval) { + this.pollingInterval = pollingInterval; + } + /** * Public setter for the requestChannel. * @param requestChannel the requestChannel to set @@ -88,7 +109,7 @@ public class MessageOrientedStep extends AbstractStep { return ExitStatus.FINISHED; } - + /** * Do nothing. * @@ -97,7 +118,7 @@ public class MessageOrientedStep extends AbstractStep { @Override protected void open(ExecutionContext ctx) throws Exception { } - + /** * Do nothing. * @@ -111,12 +132,12 @@ public class MessageOrientedStep extends AbstractStep { * @param expectedJobId */ private void waitForReply(Long expectedJobId) { - // TODO: promote timeout to field and calculate count - long timeout = 5; - int count = 0; + long timeout = pollingInterval; + long maxCount = executionTimeoutMinutes * 1000 * 60 / timeout; + long count = 0; // TODO: use a ReponseCorrelator?, or just a SynchronousChannel - while (count++ < 100) { + while (count++ < maxCount) { Message message = replyChannel.receive(timeout); @@ -144,7 +165,7 @@ public class MessageOrientedStep extends AbstractStep { } } - if (count >= 100) { + if (count >= maxCount) { throw new StepExecutionTimeoutException("Timed out waiting for steps to execute."); } } diff --git a/spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/JobExecutionRequest.java b/spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/JobLaunchRequest.java similarity index 92% rename from spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/JobExecutionRequest.java rename to spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/JobLaunchRequest.java index 1f1b58751..4154b70ec 100644 --- a/spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/JobExecutionRequest.java +++ b/spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/JobLaunchRequest.java @@ -25,7 +25,7 @@ import org.springframework.batch.core.JobParameters; * @author Dave Syer * */ -public class JobExecutionRequest { +public class JobLaunchRequest { private final Job job; private final JobParameters jobParameters; @@ -34,7 +34,7 @@ public class JobExecutionRequest { * @param job * @param jobParameters */ - public JobExecutionRequest(Job job, JobParameters jobParameters) { + public JobLaunchRequest(Job job, JobParameters jobParameters) { super(); this.job = job; this.jobParameters = jobParameters; diff --git a/spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandler.java b/spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandler.java index 7c238e2c4..4ae22495e 100644 --- a/spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandler.java +++ b/spring-batch-integration/src/main/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandler.java @@ -28,7 +28,7 @@ public class JobLaunchingMessageHandler { } @Handler - public JobExecution launch(JobExecutionRequest request) { + public JobExecution launch(JobLaunchRequest request) { Job job = request.getJob(); JobParameters jobParameters = request.getJobParameters(); diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandlerIntegrationTests.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandlerIntegrationTests.java index 8a405f594..eb76835e3 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandlerIntegrationTests.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandlerIntegrationTests.java @@ -43,7 +43,7 @@ public class JobLaunchingMessageHandlerIntegrationTests { @DirtiesContext @SuppressWarnings("unchecked") public void testNoReply() { - GenericMessage trigger = new GenericMessage(new JobExecutionRequest(job, new JobParameters())); + GenericMessage trigger = new GenericMessage(new JobLaunchRequest(job, new JobParameters())); requestChannel.send(trigger); Message executionMessage = (Message) responseChannel.receive(1000); @@ -56,7 +56,7 @@ public class JobLaunchingMessageHandlerIntegrationTests { public void testReply() { JobParametersBuilder builder = new JobParametersBuilder(); builder.addString("dontclash", "12"); - GenericMessage trigger = new GenericMessage(new JobExecutionRequest(job, builder.toJobParameters())); + GenericMessage trigger = new GenericMessage(new JobLaunchRequest(job, builder.toJobParameters())); trigger.getHeader().setReturnAddress("response"); requestChannel.send(trigger); Message executionMessage = (Message) responseChannel.receive(1000); diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandlerTests.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandlerTests.java index d10b7a8d2..99fd3793a 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandlerTests.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobLaunchingMessageHandlerTests.java @@ -43,7 +43,7 @@ public class JobLaunchingMessageHandlerTests extends AbstractJUnit4SpringContext @Test public void testSimpleDelivery() throws Exception{ - messageHandler.launch(new JobExecutionRequest(new JobSupport("testjob"), null)); + messageHandler.launch(new JobLaunchRequest(new JobSupport("testjob"), null)); assertEquals("Wrong job count", 1, jobLauncher.jobs.size()); assertEquals("Wrong job name", jobLauncher.jobs.get(0).getName(), "testjob"); diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobRequestConverter.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobRequestConverter.java index c29d8bdda..0b297bcf0 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobRequestConverter.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/launch/JobRequestConverter.java @@ -28,10 +28,10 @@ import org.springframework.integration.annotation.Handler; public class JobRequestConverter { @Handler - public JobExecutionRequest convert(String jobName) { + public JobLaunchRequest convert(String jobName) { // TODO: get these from message header Properties properties = new Properties(); - return new JobExecutionRequest(new JobSupport(jobName), new DefaultJobParametersConverter().getJobParameters(properties)); + return new JobLaunchRequest(new JobSupport(jobName), new DefaultJobParametersConverter().getJobParameters(properties)); } } diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/JobExecutionNotificationPublisher.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/JobExecutionNotificationPublisher.java index 5d82741b7..b1741e3ce 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/JobExecutionNotificationPublisher.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/JobExecutionNotificationPublisher.java @@ -29,7 +29,7 @@ import org.springframework.jmx.export.notification.NotificationPublisherAware; * JMX notification broadcaster * * @author Dave Syer - * @since 2.1 + * @since 1.0 */ public class JobExecutionNotificationPublisher implements ApplicationListener, NotificationPublisherAware { diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/MethodExecutionApplicationEventAdvice.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/StepExecutionApplicationEventAdvice.java similarity index 67% rename from spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/MethodExecutionApplicationEventAdvice.java rename to spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/StepExecutionApplicationEventAdvice.java index 6e189d85f..492d004b2 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/MethodExecutionApplicationEventAdvice.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/StepExecutionApplicationEventAdvice.java @@ -17,16 +17,18 @@ package org.springframework.batch.sample.advice; import org.aspectj.lang.JoinPoint; +import org.springframework.batch.core.StepExecution; +import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; /** - * Wraps calls for 'Processing' methods which output a single Object to write - * the string representation of the object to the log. + * Wraps calls for methods taking {@link StepExecution} as an argument and + * publishes notifications in the form of {@link ApplicationEvent}. * - * @author Lucas Ward + * @author Dave Syer */ -public class MethodExecutionApplicationEventAdvice implements ApplicationEventPublisherAware { +public class StepExecutionApplicationEventAdvice implements ApplicationEventPublisherAware { private ApplicationEventPublisher applicationEventPublisher; @@ -38,21 +40,21 @@ public class MethodExecutionApplicationEventAdvice implements ApplicationEventPu this.applicationEventPublisher = applicationEventPublisher; } - public void before(JoinPoint jp) { - String msg = "Before: "+jp.toShortString(); + public void before(JoinPoint jp, StepExecution stepExecution) { + String msg = "Before: " + jp.toShortString() + " with: " + stepExecution; publish(jp.getTarget(), msg); } - public void after(JoinPoint jp) { - String msg = "After: "+jp.toShortString(); + public void after(JoinPoint jp, StepExecution stepExecution) { + String msg = "After: " + jp.toShortString() + " with: " + stepExecution; publish(jp.getTarget(), msg); } - public void onError(JoinPoint jp, Throwable t) { - String msg = "Error in: "+jp.toShortString()+"("+t.getClass()+":"+t.getMessage()+")"; + public void onError(JoinPoint jp, StepExecution stepExecution, Throwable t) { + String msg = "Error in: " + jp.toShortString() + " with: " + stepExecution + " (" + t.getClass() + ":" + t.getMessage() + ")"; publish(jp.getTarget(), msg); } - + /** * Publish a {@link RepeatOperationsApplicationEvent} with the given * parameters. diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/tasklet/InfiniteLoopTasklet.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/tasklet/InfiniteLoopTasklet.java index bdcf67654..792285cb8 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/tasklet/InfiniteLoopTasklet.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/tasklet/InfiniteLoopTasklet.java @@ -18,6 +18,7 @@ package org.springframework.batch.sample.tasklet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.listener.StepExecutionListenerSupport; import org.springframework.batch.core.step.tasklet.Tasklet; @@ -52,9 +53,10 @@ public class InfiniteLoopTasklet extends StepExecutionListenerSupport implements Thread.currentThread().interrupt(); throw new RuntimeException("Job interrupted."); } - count++; + stepExecution.setItemCount(++count); logger.info("Executing infinite loop, at count="+count); } + stepExecution.setStatus(BatchStatus.STOPPING); return ExitStatus.FAILED; } @@ -64,5 +66,15 @@ public class InfiniteLoopTasklet extends StepExecutionListenerSupport implements public void beforeStep(StepExecution stepExecution) { this.stepExecution = stepExecution; } + + /* (non-Javadoc) + * @see org.springframework.batch.core.listener.StepExecutionListenerSupport#afterStep(org.springframework.batch.core.StepExecution) + */ + public ExitStatus afterStep(StepExecution stepExecution) { + if (stepExecution.isTerminateOnly()) { + stepExecution.setStatus(BatchStatus.STOPPED); + } + return stepExecution.getExitStatus(); + } } diff --git a/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml b/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml index ca1fff431..af6d233ab 100644 --- a/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml +++ b/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml @@ -7,7 +7,7 @@ http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> - + diff --git a/spring-batch-samples/src/main/resources/batch-hsql.properties b/spring-batch-samples/src/main/resources/batch-hsql.properties index 7f28d4f4a..fae546bb8 100644 --- a/spring-batch-samples/src/main/resources/batch-hsql.properties +++ b/spring-batch-samples/src/main/resources/batch-hsql.properties @@ -1,10 +1,10 @@ # Placeholders batch.* # for HSQLDB: batch.jdbc.driver=org.hsqldb.jdbcDriver -batch.jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true +# batch.jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true # use this one for a separate server process so you can inspect the results # (or add it to system properties with -D to override at run time). -# batch.jdbc.url=jdbc:hsqldb:hsql://localhost:9005/samples +batch.jdbc.url=jdbc:hsqldb:hsql://localhost:9005/samples batch.jdbc.user=sa batch.jdbc.password= batch.schema= diff --git a/spring-batch-samples/src/main/resources/jobs/adhocLoopJob.xml b/spring-batch-samples/src/main/resources/jobs/adhocLoopJob.xml index 3ac446f51..c83683fdd 100644 --- a/spring-batch-samples/src/main/resources/jobs/adhocLoopJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/adhocLoopJob.xml @@ -21,29 +21,18 @@ - - - - - - - - - - - - - - - + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/jobs/footballJob.xml b/spring-batch-samples/src/main/resources/jobs/footballJob.xml index a55006c3e..8e82d7c60 100644 --- a/spring-batch-samples/src/main/resources/jobs/footballJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/footballJob.xml @@ -119,18 +119,18 @@ - diff --git a/spring-batch-samples/src/main/resources/jobs/multiResourceJob.xml b/spring-batch-samples/src/main/resources/jobs/multiResourceJob.xml index 8cc581b19..878791555 100644 --- a/spring-batch-samples/src/main/resources/jobs/multiResourceJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/multiResourceJob.xml @@ -34,13 +34,13 @@ - - - - - + + + + + diff --git a/spring-batch-samples/src/main/resources/simple-job-launcher-context.xml b/spring-batch-samples/src/main/resources/simple-job-launcher-context.xml index 69a8d0b1a..f7f077cfc 100644 --- a/spring-batch-samples/src/main/resources/simple-job-launcher-context.xml +++ b/spring-batch-samples/src/main/resources/simple-job-launcher-context.xml @@ -71,19 +71,6 @@ - - - - - - - - - - - - +