RESOLVED - issue BATCH-312: Fix JMX demo now that JobIdentifier has been replaced with JobParameters

http://jira.springframework.org/browse/BATCH-312

Implemented ExportedJobLauncher and exposed in JMX
This commit is contained in:
dsyer
2008-01-24 12:49:36 +00:00
parent 58efcd47ef
commit d8da44cc92
6 changed files with 494 additions and 150 deletions

View File

@@ -39,24 +39,27 @@ public interface ExportedJobLauncher {
* @param name the name of the job to launch
* @return a representation of the {@link JobExecution} returned by a
* {@link JobLauncher}.
*
* @see #run()
*/
String run(String name);
/**
* Stop all running jobs.
* Launch a job with the given name and parameters.
*
* @see JobLauncher#stop()
* @param name the name of the job to launch
* @return a representation of the {@link JobExecution} returned by a
* {@link JobLauncher}.
*/
String run(String name, String params);
/**
* Stop all running jobs.
*/
void stop();
/**
* Enquire if any jobs are still running.
* Enquire if any jobs launched here are still running.
*
* @return true if any jobs are running.
*
* @see JobLauncher#isRunning()
*/
boolean isRunning();

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2006-2007 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.execution.bootstrap.support;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobLocator;
import org.springframework.batch.core.domain.NoSuchJobException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* A {@link PropertyEditor} that delegates to a {@link JobLocator}.
* @author Dave Syer
*
*/
public class JobPropertyEditor extends PropertyEditorSupport implements InitializingBean {
private JobLocator jobLocator;
/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(jobLocator, "JobLocator is required");
}
/**
* Accept job name and convert to {@link Job} through the injected {@link JobLocator}.
*
* @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
*/
public void setAsText(String text) throws IllegalArgumentException {
try {
setValue(jobLocator.getJob(text));
}
catch (NoSuchJobException e) {
throw new IllegalArgumentException(e);
}
}
/**
* Extract the name from the {@link JobIdentifier}.
*
* @see java.beans.PropertyEditorSupport#getAsText()
*/
public String getAsText() {
Job job = (Job) getValue();
if (job == null) {
return null;
}
return job.getName();
}
/**
* Public setter for the {@link JobLocator}.
* @param jobLocator the jobLocator to set
*/
public void setJobLocator(JobLocator jobLocator) {
this.jobLocator = jobLocator;
}
}

View File

@@ -0,0 +1,184 @@
/*
* Copyright 2006-2007 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.execution.bootstrap.support;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobLocator;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.domain.NoSuchJobException;
import org.springframework.batch.core.domain.StepExecution;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.runtime.JobParametersFactory;
import org.springframework.batch.execution.launch.JobLauncher;
import org.springframework.batch.support.PropertiesConverter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* @author Dave Syer
*
*/
public class SimpleExportedJobLauncher implements ExportedJobLauncher, InitializingBean {
private JobLauncher launcher;
private JobLocator jobLocator;
private Map registry = new HashMap();
private JobParametersFactory jobParametersFactory = new DefaultJobParametersFactory();
/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(launcher, "JobLauncher must be provided.");
Assert.notNull(jobLocator, "JobLocator must be provided.");
}
/**
* Public setter for the {@link JobLauncher}.
* @param launcher the launcher to set
*/
public void setLauncher(JobLauncher launcher) {
this.launcher = launcher;
}
/**
* Public setter for the JobLocator.
* @param jobLocator the jobLocator to set
*/
public void setJobLocator(JobLocator jobLocator) {
this.jobLocator = jobLocator;
}
/**
* Public setter for the JobParametersFactory.
* @param jobParametersFactory the jobParametersFactory to set
*/
public void setJobParametersFactory(JobParametersFactory jobParametersFactory) {
this.jobParametersFactory = jobParametersFactory;
}
/*
* (non-Javadoc)
* @see org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher#getStatistics()
*/
public Properties getStatistics() {
Properties result = new Properties();
int i = 0;
for (Iterator iterator = registry.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
JobExecution execution = (JobExecution) registry.get(key);
addStatistics(result, execution, "job" + i + ".");
i++;
}
return result;
}
/**
* @param result
* @param execution
*/
private void addStatistics(Properties result, JobExecution execution, String prefix) {
int i = 0;
for (Iterator iterator = execution.getStepExecutions().iterator(); iterator.hasNext();) {
StepExecution stepExecution = (StepExecution) iterator.next();
Properties statistics = stepExecution.getStatistics();
for (Iterator iter = statistics.keySet().iterator(); iter.hasNext();) {
String key = (String) iter.next();
result.setProperty(prefix + "step" + i + "." + key, statistics.getProperty(key));
}
}
}
/*
* (non-Javadoc)
* @see org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher#isRunning()
*/
public boolean isRunning() {
for (Iterator iterator = registry.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
JobExecution execution = (JobExecution) registry.get(key);
if (execution.isRunning()) {
return true;
}
}
return false;
}
/*
* (non-Javadoc)
* @see org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher#run(java.lang.String)
*/
public String run(String name) {
return run(name, null);
}
/*
* (non-Javadoc)
* @see org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher#run(java.lang.String,
* java.lang.String)
*/
public String run(String name, String params) {
Job job;
try {
job = jobLocator.getJob(name);
}
catch (NoSuchJobException e) {
return e.getClass().getName() + ": " + e.getMessage();
}
JobParameters jobParameters = new JobParameters();
if (params != null) {
jobParameters = jobParametersFactory.getJobParameters(PropertiesConverter.stringToProperties(params));
}
JobExecution execution;
try {
execution = launcher.run(job, jobParameters);
}
catch (JobExecutionAlreadyRunningException e) {
return e.getClass().getName() + ": " + e.getMessage();
}
registry.put(name + params, execution);
return execution.toString();
}
/*
* (non-Javadoc)
* @see org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher#stop()
*/
public void stop() {
for (Iterator iterator = registry.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
JobExecution execution = (JobExecution) registry.get(key);
execution.stop();
}
registry.clear();
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2006-2007 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.execution.bootstrap.support;
import junit.framework.TestCase;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobLocator;
import org.springframework.batch.core.domain.NoSuchJobException;
/**
* @author Dave Syer
*
*/
public class JobPropertyEditorTests extends TestCase {
private JobPropertyEditor editor = new JobPropertyEditor();
private Job job = new Job();
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
editor.setJobLocator(new JobLocator() {
public Job getJob(String name) throws NoSuchJobException {
job.setName(name);
return job;
}
});
}
public void testMandatoryProperties() throws Exception {
editor = new JobPropertyEditor();
try {
editor.afterPropertiesSet();
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
// expected
}
}
/**
* Test method for
* {@link org.springframework.batch.execution.bootstrap.support.JobParametersPropertyEditor#setAsText(java.lang.String)}.
*/
public void testSetAsTextString() {
editor.setAsText("foo");
Job job = (Job) editor.getValue();
assertEquals(job, job);
}
/**
* Test method for
* {@link org.springframework.batch.execution.bootstrap.support.JobParametersPropertyEditor#getAsText()}.
*/
public void testGetAsText() {
editor.setAsText("foo");
assertEquals("foo", editor.getAsText());
}
}

View File

@@ -8,6 +8,8 @@
<listEntry value="1"/>
</listAttribute>
<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
<stringAttribute key="org.eclipse.debug.core.source_locator_id" value="org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector"/>
<stringAttribute key="org.eclipse.debug.core.source_locator_memento" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;sourceLookupDirector&gt;&#13;&#10;&lt;sourceContainers duplicates=&quot;false&quot;&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;javaProject name=&amp;quot;spring-batch-core&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;javaProject name=&amp;quot;spring-batch-execution&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;javaProject name=&amp;quot;spring-batch-infrastructure&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;javaProject name=&amp;quot;spring-batch-integration&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;javaProject name=&amp;quot;spring-batch-samples&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;default/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.debug.core.containerType.default&quot;/&gt;&#13;&#10;&lt;/sourceContainers&gt;&#13;&#10;&lt;/sourceLookupDirector&gt;&#13;&#10;"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.springframework.batch.sample.TaskExecutorLauncher"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="spring-batch-samples"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dcom.sun.management.jmxremote"/>

View File

@@ -1,153 +1,155 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- The tasklet used in this job will run in an infinite loop. This is useful for testing graceful shutdown from
multiple environments. -->
<bean parent="stepScope" />
<bean parent="jobConfigurationRegistryBeanPostProcessor" />
<bean id="loopJob" parent="simpleJob">
<property name="steps">
<bean id="step1" parent="simpleStep">
<property name="tasklet">
<bean id="module"
class="org.springframework.batch.sample.tasklet.InfiniteLoopTasklet"
scope="step">
<aop:scoped-proxy />
</bean>
</property>
<property name="commitInterval" value="2" />
</bean>
</property>
</bean>
<bean id="notifyingStepExecutor" parent="stepExecutor"
scope="prototype">
<property name="stepOperations">
<bean
class="org.springframework.batch.repeat.support.RepeatTemplate">
<property name="interceptor">
<bean
class="org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatInterceptor" />
</property>
</bean>
</property>
</bean>
<bean class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="spring:service=batch,bean=jobLauncher">
<bean
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="jobLauncher" />
<property name="interfaces">
<list>
<value>
org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher
</value>
</list>
</property>
<property name="interceptorNames"
value="convertingMethodInterceptor" />
</bean>
</entry>
<entry
key="spring:service=batch,bean=notificationPublisher"
value-ref="notificationPublisher" />
<entry
key="spring:service=batch,bean=configurationLoader">
<bean
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="loader" />
<property name="interfaces">
<list>
<value>
org.springframework.batch.sample.ExportedJobLoader
</value>
</list>
</property>
<property name="interceptorNames"
value="convertingMethodInterceptor" />
</bean>
</entry>
</map>
</property>
<property name="assembler">
<bean
class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="interfaceMappings">
<map>
<entry
key="spring:service=batch,bean=jobLauncher"
value="org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher" />
<entry
key="spring:service=batch,bean=jobLoader"
value="org.springframework.batch.sample.JobLoader" />
</map>
</property>
</bean>
</property>
</bean>
<bean id="notificationPublisher"
class="org.springframework.batch.execution.bootstrap.JobExecutionNotificationPublisher" />
<bean id="convertingMethodInterceptor"
class="org.springframework.batch.execution.bootstrap.support.TypeConverterMethodInterceptor">
<!-- The tasklet used in this job will run in an infinite loop. This is useful for testing graceful shutdown from
multiple environments. -->
<bean parent="stepScope" />
<bean parent="jobConfigurationRegistryBeanPostProcessor" />
<bean id="loopJob" parent="simpleJob">
<property name="steps">
<bean id="step1" parent="simpleStep">
<property name="tasklet">
<bean id="module"
class="org.springframework.batch.sample.tasklet.InfiniteLoopTasklet"
scope="step">
<aop:scoped-proxy />
</bean>
</property>
<property name="commitInterval" value="2" />
</bean>
</property>
</bean>
<bean id="notifyingStepExecutor" parent="stepExecutor"
scope="prototype">
<property name="stepOperations">
<bean
class="org.springframework.batch.repeat.support.RepeatTemplate">
<property name="interceptor">
<bean
class="org.springframework.batch.repeat.interceptor.ApplicationEventPublisherRepeatInterceptor" />
</property>
</bean>
</property>
</bean>
<bean class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="spring:service=batch,bean=jobLauncher">
<bean
class="org.springframework.batch.execution.bootstrap.support.SimpleExportedJobLauncher">
<property name="launcher" ref="jobLauncher" />
<property name="jobLocator"
ref="jobConfigurationRegistry" />
</bean>
</entry>
<entry
key="spring:service=batch,bean=notificationPublisher"
value-ref="notificationPublisher" />
<entry
key="spring:service=batch,bean=configurationLoader">
<bean
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="loader" />
<property name="interfaces">
<list>
<value>
org.springframework.batch.sample.ExportedJobLoader
</value>
</list>
</property>
<property name="interceptorNames"
value="convertingMethodInterceptor" />
</bean>
</entry>
</map>
</property>
<property name="assembler">
<bean
class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="interfaceMappings">
<map>
<entry
key="spring:service=batch,bean=jobLauncher"
value="org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher" />
<entry key="spring:service=batch,bean=jobLoader"
value="org.springframework.batch.sample.JobLoader" />
</map>
</property>
</bean>
</property>
</bean>
<bean id="notificationPublisher"
class="org.springframework.batch.execution.bootstrap.JobExecutionNotificationPublisher" />
<bean id="convertingMethodInterceptor"
class="org.springframework.batch.execution.bootstrap.support.TypeConverterMethodInterceptor">
<property name="convertException" value="true" />
<property name="customEditors">
<map>
<entry key="org.springframework.batch.core.domain.JobIdentifier">
<bean class="org.springframework.batch.execution.bootstrap.support.JobParametersPropertyEditor"/>
<entry
key="org.springframework.batch.core.domain.JobParameters">
<bean
class="org.springframework.batch.execution.bootstrap.support.JobParametersPropertyEditor" />
</entry>
<entry
key="org.springframework.batch.core.domain.Job">
<bean
class="org.springframework.batch.execution.bootstrap.support.JobPropertyEditor">
<property name="jobLocator"
ref="jobConfigurationRegistry" />
</bean>
</entry>
</map>
</property>
</bean>
<bean id="logAdvice"
class="org.springframework.batch.sample.advice.MethodExecutionLogAdvice" />
<aop:config>
<aop:aspect ref="logAdvice">
<aop:after
pointcut="execution( * org.springframework.batch.sample..InfiniteLoopTasklet+.execute(..))"
method="doBasicLogging" />
</aop:aspect>
</aop:config>
<bean id="jobLauncher"
class="org.springframework.batch.execution.launch.SimpleJobLauncher">
<property name="jobRepository" ref="simpleJobRepository" />
<property name="jobExecutor">
<bean parent="jobExecutor">
<property name="stepExecutorFactory">
<bean
class="org.springframework.batch.execution.step.PrototypeBeanStepExecutorFactory">
<property name="stepExecutorName"
value="notifyingStepExecutor" />
</bean>
</property>
</bean>
</property>
<property name="taskExecutor">
<bean
class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
</property>
</bean>
<bean id="loader"
class="org.springframework.batch.sample.DefaultJobLoader">
<property name="registry" ref="jobConfigurationRegistry" />
</bean>
</beans>
</property>
</bean>
<bean id="logAdvice"
class="org.springframework.batch.sample.advice.MethodExecutionLogAdvice" />
<aop:config>
<aop:aspect ref="logAdvice">
<aop:after
pointcut="execution( * org.springframework.batch.sample..InfiniteLoopTasklet+.execute(..))"
method="doBasicLogging" />
</aop:aspect>
</aop:config>
<bean id="jobLauncher"
class="org.springframework.batch.execution.launch.SimpleJobLauncher">
<property name="jobRepository" ref="simpleJobRepository" />
<property name="jobExecutor">
<bean parent="jobExecutor">
<property name="stepExecutorFactory">
<bean
class="org.springframework.batch.execution.step.PrototypeBeanStepExecutorFactory">
<property name="stepExecutorName"
value="notifyingStepExecutor" />
</bean>
</property>
</bean>
</property>
<property name="taskExecutor">
<bean
class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
</property>
</bean>
<bean id="loader"
class="org.springframework.batch.sample.DefaultJobLoader">
<property name="registry" ref="jobConfigurationRegistry" />
</bean>
</beans>