diff --git a/spring-batch-samples/pom.xml b/spring-batch-samples/pom.xml index afb3bf838..b0875e309 100644 --- a/spring-batch-samples/pom.xml +++ b/spring-batch-samples/pom.xml @@ -230,7 +230,13 @@ mx4j mx4j - 3.0.1 + 3.0.2 + true + + + mx4j + mx4j-remote + 3.0.2 true 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 0422cd898..407dee9ee 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 @@ -9,7 +9,15 @@ - + + + + + + + + + diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/launch/RemoteLauncherTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/launch/RemoteLauncherTests.java index 00fafb83b..05fff0a11 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/launch/RemoteLauncherTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/launch/RemoteLauncherTests.java @@ -18,53 +18,59 @@ package org.springframework.batch.sample.launch; import java.util.ArrayList; import java.util.List; +import javax.management.MBeanServerConnection; + import junit.framework.TestCase; import org.springframework.batch.core.launch.support.ExportedJobLauncher; +import org.springframework.jmx.MBeanServerNotFoundException; import org.springframework.jmx.access.InvalidInvocationException; import org.springframework.jmx.access.MBeanProxyFactoryBean; +import org.springframework.jmx.support.MBeanServerConnectionFactoryBean; /** * @author Dave Syer - * + * */ public class RemoteLauncherTests extends TestCase { - + private static List errors = new ArrayList(); + private static Thread thread; + private static ExportedJobLauncher launcher; + private static MBeanProxyFactoryBean factory; + 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); + 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); + assertTrue("Should contain 'JobExecution': " + result, result.indexOf("JobExecution: id=") >= 0); Thread.sleep(500); assertTrue(launcher.isRunning()); launcher.stop(); assertFalse(launcher.isRunning()); } - /** - * @throws Exception + /* + * (non-Javadoc) + * @see junit.framework.TestCase#setUp() */ - private static void doLaunch() throws Exception { - if (launcher!=null) { + protected void setUp() throws Exception { + if (launcher != null) { return; } System.setProperty("com.sun.management.jmxremote", ""); @@ -79,13 +85,15 @@ public class RemoteLauncherTests extends TestCase { } }); thread.start(); - MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean(); + MBeanServerConnectionFactoryBean connectionFactory = new MBeanServerConnectionFactoryBean(); + connectionFactory.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/batch-samples"); + factory = new MBeanProxyFactoryBean(); factory.setObjectName("spring:service=batch,bean=jobLauncher"); factory.setProxyInterface(ExportedJobLauncher.class); - factory.afterPropertiesSet(); - launcher = (ExportedJobLauncher) factory.getObject(); + factory.setServer((MBeanServerConnection) connectionFactory.getObject()); + // factory.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/batch-samples"); int count = 0; - while (!isConnected() && count ++<10) { + while (!isConnected() && count++ < 10) { Thread.sleep(1000); } } @@ -98,10 +106,21 @@ public class RemoteLauncherTests extends TestCase { if (!TaskExecutorLauncher.getErrors().isEmpty()) { throw (RuntimeException) TaskExecutorLauncher.getErrors().get(0); } + if (launcher == null) { + try { + factory.afterPropertiesSet(); + launcher = (ExportedJobLauncher) factory.getObject(); + } + catch (MBeanServerNotFoundException e) { + // ignore + return false; + } + } try { launcher.isRunning(); connected = true; - } catch (InvalidInvocationException e) { + } + catch (InvalidInvocationException e) { // ignore } return connected;