RESOLVED - issue BATCH-1130: Ensure Ordered is respected by generated listeners

Add JobListenerFactoryBean - same solution
This commit is contained in:
dsyer
2009-03-10 14:40:45 +00:00
parent 3719198418
commit 944b6aa4c8
2 changed files with 38 additions and 2 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.batch.support.MethodInvoker;
import org.springframework.batch.support.MethodInvokerUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
/**
@@ -107,8 +108,15 @@ public class JobListenerFactoryBean implements FactoryBean, InitializingBean {
// be called
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(delegate);
proxyFactory.setInterfaces(new Class[] { JobExecutionListener.class });
proxyFactory.addAdvisor(new DefaultPointcutAdvisor(new MethodInvokerMethodInterceptor(invokerMap)));
boolean ordered = false;
if (delegate instanceof Ordered) {
ordered = true;
proxyFactory.addInterface(Ordered.class);
}
proxyFactory.addInterface(JobExecutionListener.class);
proxyFactory.addAdvisor(new DefaultPointcutAdvisor(new MethodInvokerMethodInterceptor(invokerMap, ordered)));
return proxyFactory.getProxy();
}

View File

@@ -23,10 +23,12 @@ import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.annotation.AfterJob;
import org.springframework.batch.core.annotation.BeforeJob;
import org.springframework.core.Ordered;
/**
* @author Lucas Ward
@@ -74,6 +76,15 @@ public class JobListenerFactoryBeanTests {
assertTrue(delegate.afterJobCalled);
}
@Test
public void testVanillaInterfaceWithProxy() throws Exception {
JobListenerWithInterface delegate = new JobListenerWithInterface();
ProxyFactory factory = new ProxyFactory(delegate);
factoryBean.setDelegate(factory.getProxy());
Object listener = factoryBean.getObject();
assertTrue(listener instanceof JobExecutionListener);
}
@Test
public void testUseInHashSet() throws Exception {
JobListenerWithInterface delegate = new JobListenerWithInterface();
@@ -102,6 +113,23 @@ public class JobListenerFactoryBeanTests {
assertTrue(JobListenerFactoryBean.isListener(new JobListenerWithInterface()));
}
@Test
public void testAnnotationsWithOrdered() throws Exception {
Object delegate = new Ordered() {
@SuppressWarnings("unused")
@BeforeJob
public void foo(JobExecution execution) {
}
public int getOrder() {
return 3;
}
};
JobExecutionListener listener = JobListenerFactoryBean.getListener(delegate);
assertTrue("Listener is not of correct type", listener instanceof Ordered);
assertEquals(3, ((Ordered) listener).getOrder());
}
@Test
public void testEqualityOfProxies() throws Exception {
JobListenerWithInterface delegate = new JobListenerWithInterface();