RESOLVED - issue BATCH-398: That old stateful / stateless thing again....

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

Fix JMX demo using an application context per job execution.
This commit is contained in:
dsyer
2008-03-01 09:59:01 +00:00
parent f2129d4ea7
commit 0eb239c9cb
9 changed files with 263 additions and 79 deletions

View File

@@ -0,0 +1,71 @@
/*
* 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.sample;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.repository.JobFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* A {@link JobFactory} that creates its own {@link ApplicationContext} from a
* path supplied, and pulls a bean out when asked to create a {@link Job}.
*
* @author Dave Syer
*
*/
public class ClassPathXmlApplicationContextJobFactory implements JobFactory {
private String beanName;
private String path;
private ApplicationContext parent;
/**
* @param beanName
* @param path
*/
public ClassPathXmlApplicationContextJobFactory(String beanName, String path, ApplicationContext parent) {
super();
this.beanName = beanName;
this.path = path;
this.parent = parent;
}
/**
* Create a {@link ClassPathXmlApplicationContext} from the path provided
* and pull out a bean with the name given during initialization.
*
* @see org.springframework.batch.core.repository.JobFactory#createJob()
*/
public Job createJob() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { path }, parent);
return (Job) context.getBean(beanName, Job.class);
}
/**
* Return the bean name of the job in the application context. N.B. this is
* usually the name of the job as well, but it needn't be. The important
* thing is that the job can be located by this name.
*
* @see org.springframework.batch.core.repository.JobFactory#getJobName()
*/
public String getJobName() {
return beanName;
}
}

View File

@@ -15,53 +15,96 @@
*/
package org.springframework.batch.sample;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.repository.DuplicateJobException;
import org.springframework.batch.core.repository.JobRegistry;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ResourceLoader;
/**
* @author Dave Syer
*
*/
public class TaskExecutorLauncher {
public class TaskExecutorLauncher implements ResourceLoaderAware {
private JobRegistry registry;
private void register(String[] paths) {
// registry.register(jobConfiguration)
private ResourceLoader resourceLoader;
private ApplicationContext parentContext = null;
/**
* Public setter for the {@link JobRegistry}.
* @param registry the registry to set
*/
public void setRegistry(JobRegistry registry) {
this.registry = registry;
}
/*
* (non-Javadoc)
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
*/
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
private void register(String[] paths) throws DuplicateJobException {
for (int i = 0; i < paths.length; i++) {
String path = paths[i];
ConfigurableListableBeanFactory beanFactory = new XmlBeanFactory(resourceLoader.getResource(path),
parentContext.getAutowireCapableBeanFactory());
String[] names = beanFactory.getBeanNamesForType(Job.class);
for (int j = 0; j < names.length; j++) {
registry.register(new ClassPathXmlApplicationContextJobFactory(names[j], path, parentContext));
}
}
}
public static void main(String[] args) throws Exception {
// Paths to individual job configurations.
final String[] paths = new String[] { "jobs/adhocLoopJob.xml",
"jobs/footballJob.xml" };
// The simple execution environment will be used as a parent
// context for each of the job contexts. The standard version of this
// from the Spring Batch samples does not have an MBean for the
// JobLauncher, nor does the JobLauncher have an asynchronous
// TaskExecutor. The adhocLoopJob has both, which is why it has to be
// included in the paths above.
final ApplicationContext parent = new ClassPathXmlApplicationContext(
"adhoc-job-launcher-context.xml");
// parent.getAutowireCapableBeanFactory().autowireBeanProperties(
// this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
final TaskExecutorLauncher launcher = new TaskExecutorLauncher();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < paths.length; i++) {
String path = paths[i];
new ClassPathXmlApplicationContext(new String[] { path },
parent);
}
launcher.run();
};
}).start();
while (launcher.parentContext == null) {
Thread.sleep(100L);
}
// Paths to individual job configurations.
final String[] paths = new String[] { "jobs/adhocLoopJob.xml", "jobs/footballJob.xml" };
launcher.register(paths);
System.out
.println("Started application. "
+ "Please connect using JMX (remember to use -Dcom.sun.management.jmxremote if you can't see anything in Jconsole).");
System.in.read();
}
private void run() {
/*
* A simple execution environment with an MBean for the JobLauncher,
* which has an asynchronous TaskExecutor. This will be used as the
* parent context for loading job configurations.
*/
final ApplicationContext parent = new ClassPathXmlApplicationContext("adhoc-job-launcher-context.xml");
parent.getAutowireCapableBeanFactory().autowireBeanProperties(this,
AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
parent.getAutowireCapableBeanFactory().initializeBean(this, "taskExecutorLauncher");
this.parentContext = parent;
}
}