Dust off JMX demo

This commit is contained in:
dsyer
2008-07-05 07:32:44 +00:00
parent 086fded0cf
commit 3d78c0bcb5
20 changed files with 120 additions and 84 deletions

View File

@@ -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;

View File

@@ -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)

View File

@@ -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 '<code>FieldSet</code>' instance as
* String values.

View File

@@ -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");

View File

@@ -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;
}
}

View File

@@ -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.");
}
}

View File

@@ -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;

View File

@@ -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();

View File

@@ -43,7 +43,7 @@ public class JobLaunchingMessageHandlerIntegrationTests {
@DirtiesContext
@SuppressWarnings("unchecked")
public void testNoReply() {
GenericMessage<JobExecutionRequest> trigger = new GenericMessage<JobExecutionRequest>(new JobExecutionRequest(job, new JobParameters()));
GenericMessage<JobLaunchRequest> trigger = new GenericMessage<JobLaunchRequest>(new JobLaunchRequest(job, new JobParameters()));
requestChannel.send(trigger);
Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000);
@@ -56,7 +56,7 @@ public class JobLaunchingMessageHandlerIntegrationTests {
public void testReply() {
JobParametersBuilder builder = new JobParametersBuilder();
builder.addString("dontclash", "12");
GenericMessage<JobExecutionRequest> trigger = new GenericMessage<JobExecutionRequest>(new JobExecutionRequest(job, builder.toJobParameters()));
GenericMessage<JobLaunchRequest> trigger = new GenericMessage<JobLaunchRequest>(new JobLaunchRequest(job, builder.toJobParameters()));
trigger.getHeader().setReturnAddress("response");
requestChannel.send(trigger);
Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000);

View File

@@ -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");

View File

@@ -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));
}
}

View File

@@ -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 {

View File

@@ -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.

View File

@@ -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();
}
}

View File

@@ -7,7 +7,7 @@
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<import resource="simple-job-launcher-context.xml" />
<!-- For Java 1.4 we need to ceate teh mbean server explicitly -->
<!-- For Java 1.4 we need to ceate the mbean server explicitly -->
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="true" />
</bean>

View File

@@ -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=

View File

@@ -21,29 +21,18 @@
</property>
</bean>
<bean id="logAdvice"
class="org.springframework.batch.sample.advice.MethodExecutionLogAdvice" />
<bean id="eventAdvice"
class="org.springframework.batch.sample.advice.MethodExecutionApplicationEventAdvice" />
<aop:config>
<aop:aspect ref="logAdvice">
<aop:after
pointcut="execution( * org.springframework.batch.sample..InfiniteLoopTasklet+.execute(..))"
method="doBasicLogging" />
</aop:aspect>
<aop:aspect ref="eventAdvice">
<aop:before
pointcut="execution( * org.springframework.batch..Step+.execute(..))"
method="before" />
<aop:after
pointcut="execution( * org.springframework.batch..Step+.execute(..))"
method="after" />
<aop:after-throwing throwing="t"
pointcut="execution( * org.springframework.batch..Step+.execute(..))"
method="onError" />
</aop:aspect>
</aop:config>
<aop:config>
<aop:aspect ref="eventAdvice">
<aop:before
pointcut="execution( * org.springframework.batch..Step+.execute(..)) and args(stepExecution)"
method="before" />
<aop:after
pointcut="execution( * org.springframework.batch..Step+.execute(..)) and args(stepExecution)"
method="after" />
<aop:after-throwing throwing="t"
pointcut="execution( * org.springframework.batch..Step+.execute(..)) and args(stepExecution)"
method="onError" />
</aop:aspect>
</aop:config>
</beans>

View File

@@ -119,18 +119,18 @@
</aop:aspect>
<aop:aspect ref="eventAdvice">
<aop:before
pointcut="execution( * org.springframework.batch..Step+.execute(..))"
pointcut="execution( * org.springframework.batch..Step+.execute(..)) and args(stepExecution)"
method="before" />
<aop:after
pointcut="execution( * org.springframework.batch..Step+.execute(..))"
pointcut="execution( * org.springframework.batch..Step+.execute(..)) and args(stepExecution)"
method="after" />
<aop:after-throwing throwing="t"
pointcut="execution( * org.springframework.batch..Step+.execute(..))"
pointcut="execution( * org.springframework.batch..Step+.execute(..)) and args(stepExecution)"
method="onError" />
</aop:aspect>
</aop:config>
<bean id="footbalProperties"
<bean id="footballProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<value>

View File

@@ -34,13 +34,13 @@
<!-- INFRASTRUCTURE SETUP -->
<!-- This input source is injected into the test case to verify the output - not used by the job at all -->
<bean id="testItemReader"
class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources"
value="classpath:data/multiResourceJob/input/file-*.txt" />
<property name="delegate" ref="flatFileItemReader" />
</bean>
<bean id="testItemReader"
class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources"
value="classpath:data/multiResourceJob/input/file-*.txt" />
<property name="delegate" ref="flatFileItemReader" />
</bean>
<bean id="fileItemReader" parent="testItemReader"
autowire-candidate="false" />

View File

@@ -71,19 +71,6 @@
<bean id="logAdvice" class="org.springframework.batch.sample.advice.ProcessorLogAdvice" />
<bean id="eventAdvice" class="org.springframework.batch.sample.advice.MethodExecutionApplicationEventAdvice" />
<aop:config>
<aop:aspect ref="logAdvice">
<aop:after pointcut="execution( * org.springframework.batch.sample..InfiniteLoopTasklet+.execute(..))"
method="doBasicLogging" />
</aop:aspect>
<aop:aspect ref="eventAdvice">
<aop:before pointcut="execution( * org.springframework.batch..Step+.execute(..))" method="before" />
<aop:after pointcut="execution( * org.springframework.batch..Step+.execute(..))" method="after" />
<aop:after-throwing throwing="t" pointcut="execution( * org.springframework.batch..Step+.execute(..))"
method="onError" />
</aop:aspect>
</aop:config>
<bean id="eventAdvice" class="org.springframework.batch.sample.advice.StepExecutionApplicationEventAdvice" />
</beans>