RESOLVED - issue BATCH-183: Unit test for JMX notifications / control
This commit is contained in:
@@ -1,96 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.configuration.ListableJobRegistry;
|
||||
import org.springframework.batch.core.repository.NoSuchJobException;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.PropertyAccessorUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class DefaultJobLoader implements JobLoader,
|
||||
ApplicationContextAware {
|
||||
|
||||
private ListableJobRegistry registry;
|
||||
private ApplicationContext applicationContext;
|
||||
private Map configurations = new HashMap();
|
||||
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public void setRegistry(ListableJobRegistry registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public Map getConfigurations() {
|
||||
Map result = new HashMap(configurations);
|
||||
for (Iterator iterator = registry.getJobNames().iterator(); iterator
|
||||
.hasNext();) {
|
||||
try {
|
||||
Job configuration = (Job) registry.getJob((String) iterator.next());
|
||||
String name = configuration.getName();
|
||||
if (!configurations.containsKey(name)) {
|
||||
result.put(name, "<unknown path>: " + configuration);
|
||||
}
|
||||
}
|
||||
catch (NoSuchJobException e) {
|
||||
throw new IllegalStateException("Registry could not locate its own job (NoSuchJobException).");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void loadResource(String path) {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
new String[] { path }, applicationContext);
|
||||
String[] names = context.getBeanNamesForType(Job.class);
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
String name = names[i];
|
||||
configurations.put(name, path);
|
||||
}
|
||||
}
|
||||
|
||||
public Object getJobConfiguration(String name) {
|
||||
try {
|
||||
return registry.getJob(name);
|
||||
} catch (NoSuchJobException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Object getProperty(String path) {
|
||||
int index = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(path);
|
||||
BeanWrapperImpl wrapper = createBeanWrapper(path, index);
|
||||
String key = path.substring(index+1);
|
||||
return wrapper.getPropertyValue(key);
|
||||
}
|
||||
|
||||
public void setProperty(String path, String value) {
|
||||
int index = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(path);
|
||||
BeanWrapperImpl wrapper = createBeanWrapper(path, index);
|
||||
String key = path.substring(index+1);
|
||||
wrapper.setPropertyValue(key, value);
|
||||
}
|
||||
|
||||
private BeanWrapperImpl createBeanWrapper(String path, int index) {
|
||||
Assert.state(index>0, "Path must be nested, e.g. bean.value");
|
||||
String name = path.substring(0,index);
|
||||
Object bean = getJobConfiguration(name);
|
||||
Assert.notNull(bean, "No JobConfiguration exists with name="+name);
|
||||
BeanWrapperImpl wrapper = new BeanWrapperImpl(bean);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* 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 java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface ExportedJobLoader {
|
||||
|
||||
void loadResource(String path);
|
||||
|
||||
Map getConfigurations();
|
||||
|
||||
String getJob(String path);
|
||||
|
||||
String getProperty(String path);
|
||||
|
||||
void setProperty(String path, String value);
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* 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 java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface JobLoader {
|
||||
|
||||
void loadResource(String path);
|
||||
|
||||
Map getConfigurations();
|
||||
|
||||
Object getJobConfiguration(String path);
|
||||
|
||||
Object getProperty(String path);
|
||||
|
||||
void setProperty(String path, String value);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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.launch;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.core.launch.support.ExportedJobLauncher;
|
||||
import org.springframework.jmx.access.InvalidInvocationException;
|
||||
import org.springframework.jmx.access.MBeanProxyFactoryBean;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class RemoteLauncherTests extends TestCase {
|
||||
|
||||
private static List errors = new ArrayList();
|
||||
private static Thread thread;
|
||||
private static ExportedJobLauncher launcher;
|
||||
|
||||
public void testConnect() throws Exception {
|
||||
doLaunch();
|
||||
assertEquals(0, errors.size());
|
||||
assertTrue(isConnected());
|
||||
}
|
||||
|
||||
public void testLaunchBadJob() throws Exception {
|
||||
doLaunch();
|
||||
assertEquals(0, errors.size());
|
||||
assertTrue(isConnected());
|
||||
String result = launcher.run("foo");
|
||||
assertTrue("Should contain 'NoSuchJobException': "+result, result.indexOf("NoSuchJobException")>=0);
|
||||
}
|
||||
|
||||
public void testLaunchAndStopRealJob() throws Exception {
|
||||
doLaunch();
|
||||
assertEquals(0, errors.size());
|
||||
assertTrue(isConnected());
|
||||
String result = launcher.run("loopJob");
|
||||
assertTrue("Should contain 'JobExecution': "+result, result.indexOf("JobExecution: id=")>=0);
|
||||
Thread.sleep(500);
|
||||
assertTrue(launcher.isRunning());
|
||||
launcher.stop();
|
||||
assertFalse(launcher.isRunning());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private static void doLaunch() throws Exception {
|
||||
if (launcher!=null) {
|
||||
return;
|
||||
}
|
||||
System.setProperty("com.sun.management.jmxremote", "");
|
||||
thread = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
TaskExecutorLauncher.main(new String[0]);
|
||||
}
|
||||
catch (Exception e) {
|
||||
errors.add(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean();
|
||||
factory.setObjectName("spring:service=batch,bean=jobLauncher");
|
||||
factory.setProxyInterface(ExportedJobLauncher.class);
|
||||
factory.afterPropertiesSet();
|
||||
launcher = (ExportedJobLauncher) factory.getObject();
|
||||
int count = 0;
|
||||
while (!isConnected() && count ++<10) {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static boolean isConnected() {
|
||||
boolean connected = false;
|
||||
if (!TaskExecutorLauncher.getErrors().isEmpty()) {
|
||||
throw (RuntimeException) TaskExecutorLauncher.getErrors().get(0);
|
||||
}
|
||||
try {
|
||||
launcher.isRunning();
|
||||
connected = true;
|
||||
} catch (InvalidInvocationException e) {
|
||||
// ignore
|
||||
}
|
||||
return connected;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user