Add support for repeatable JmsListener
Previously, a method could only declare one Jms endpoint so if several destinations share the exact same business logic, you'd still need one separate method declaration per destination. We now make sure that JmsListener is a repeatable annotation, introducing JmsListeners for pre Java8 use cases. Issue: SPR-13147
This commit is contained in:
@@ -73,6 +73,12 @@ public abstract class AbstractJmsAnnotationDrivenTests {
|
||||
@Test
|
||||
public abstract void jmsHandlerMethodFactoryConfiguration() throws JMSException;
|
||||
|
||||
@Test
|
||||
public abstract void jmsListenerIsRepeatable();
|
||||
|
||||
@Test
|
||||
public abstract void jmsListeners();
|
||||
|
||||
/**
|
||||
* Test for {@link SampleBean} discovery. If a factory with the default name
|
||||
* is set, an endpoint will use it automatically
|
||||
@@ -234,6 +240,50 @@ public abstract class AbstractJmsAnnotationDrivenTests {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link JmsListenerRepeatableBean} and {@link JmsListenersBean} that validates that the
|
||||
* {@code @JmsListener} annotation is repeatable and generate one specific container per annotation.
|
||||
*/
|
||||
public void testJmsListenerRepeatable(ApplicationContext context) {
|
||||
JmsListenerContainerTestFactory simpleFactory =
|
||||
context.getBean("jmsListenerContainerFactory", JmsListenerContainerTestFactory.class);
|
||||
assertEquals(2, simpleFactory.getListenerContainers().size());
|
||||
|
||||
MethodJmsListenerEndpoint first = (MethodJmsListenerEndpoint)
|
||||
simpleFactory.getListenerContainer("first").getEndpoint();
|
||||
assertEquals("first", first.getId());
|
||||
assertEquals("myQueue", first.getDestination());
|
||||
assertEquals(null, first.getConcurrency());
|
||||
|
||||
MethodJmsListenerEndpoint second = (MethodJmsListenerEndpoint)
|
||||
simpleFactory.getListenerContainer("second").getEndpoint();
|
||||
assertEquals("second", second.getId());
|
||||
assertEquals("anotherQueue", second.getDestination());
|
||||
assertEquals("2-10", second.getConcurrency());
|
||||
}
|
||||
|
||||
@Component
|
||||
static class JmsListenerRepeatableBean {
|
||||
|
||||
@JmsListener(id = "first", destination = "myQueue")
|
||||
@JmsListener(id = "second", destination = "anotherQueue", concurrency = "2-10")
|
||||
public void repeatableHandle(String msg) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
static class JmsListenersBean {
|
||||
|
||||
@JmsListeners({
|
||||
@JmsListener(id = "first", destination = "myQueue"),
|
||||
@JmsListener(id = "second", destination = "anotherQueue", concurrency = "2-10")
|
||||
})
|
||||
public void repeatableHandle(String msg) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestValidator implements Validator {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -91,6 +91,20 @@ public class AnnotationDrivenNamespaceTests extends AbstractJmsAnnotationDrivenT
|
||||
testJmsHandlerMethodFactoryConfiguration(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jmsListenerIsRepeatable() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"annotation-driven-jms-listener-repeatable.xml", getClass());
|
||||
testJmsListenerRepeatable(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jmsListeners() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"annotation-driven-jms-listeners.xml", getClass());
|
||||
testJmsListenerRepeatable(context);
|
||||
}
|
||||
|
||||
static class CustomJmsListenerConfigurer implements JmsListenerConfigurer {
|
||||
|
||||
private MessageListener messageListener;
|
||||
|
||||
@@ -112,6 +112,20 @@ public class EnableJmsTests extends AbstractJmsAnnotationDrivenTests {
|
||||
testJmsHandlerMethodFactoryConfiguration(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jmsListenerIsRepeatable() {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
EnableJmsDefaultContainerFactoryConfig.class, JmsListenerRepeatableBean.class);
|
||||
testJmsListenerRepeatable(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jmsListeners() {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
EnableJmsDefaultContainerFactoryConfig.class, JmsListenersBean.class);
|
||||
testJmsListenerRepeatable(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unknownFactory() {
|
||||
thrown.expect(BeanCreationException.class);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -17,25 +17,30 @@
|
||||
package org.springframework.jms.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class JmsListenerContainerTestFactory implements JmsListenerContainerFactory<MessageListenerTestContainer> {
|
||||
|
||||
private final List<MessageListenerTestContainer> listenerContainers =
|
||||
new ArrayList<MessageListenerTestContainer>();
|
||||
private final Map<String, MessageListenerTestContainer> listenerContainers =
|
||||
new LinkedHashMap<>();
|
||||
|
||||
public List<MessageListenerTestContainer> getListenerContainers() {
|
||||
return listenerContainers;
|
||||
return new ArrayList<>(this.listenerContainers.values());
|
||||
}
|
||||
|
||||
public MessageListenerTestContainer getListenerContainer(String id) {
|
||||
return this.listenerContainers.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageListenerTestContainer createListenerContainer(JmsListenerEndpoint endpoint) {
|
||||
MessageListenerTestContainer container = new MessageListenerTestContainer(endpoint);
|
||||
this.listenerContainers.add(container);
|
||||
this.listenerContainers.put(endpoint.getId(), container);
|
||||
return container;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user