如何通过@JmsListener仅更改一种方法的默认并发性?
我已经向@JmsListener添加了并发性(如下所示),但是在“ JBoss管理控制台运行时JMS队列指标”中没有任何更改。
我有JBoss 6.4,Spring JMS版本4.3.1.RELEASE。
我的听众:
@JmsListener(containerFactory = "myJmsQueueListenerContainerFactory", destination = "some.destination", concurrency = "3-10")
@SendTo("some.place")
public TransactionBookingStatus bookTransaction(Message message) throws JMSException {
//my implementation
}
我的默认配置:
@Bean
public JndiObjectFactoryBean jmsConnectionFactoryFactoryBean() {
JndiObjectFactoryBean factory = new JndiObjectFactoryBean();
factory.setJndiName("java:/ConnectionFactory");
return factory;
}
public ConnectionFactory jmsConnectionFactory() {
return (ConnectionFactory) jmsConnectionFactoryFactoryBean().getObject();
}
@Bean
public JmsTemplate testMessageTemplate() {
JmsTemplate tmpl = new JmsTemplate();
tmpl.setConnectionFactory(jmsConnectionFactory());
return tmpl;
}
@Bean
public DefaultJmsListenerContainerFactory myJmsQueueListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(jmsConnectionFactory());
factory.setConcurrency("3-40");
factory.setErrorHandler(new TrnJmsErrorHandler());
return factory;
}
最佳答案
您应该执行的操作-打开org.springframework.jms
的DEBUG日志记录,您将看到有关使用者管理的消息-确保在日志配置中包括该线程,并且应该看到3个使用者线程。
编辑
@SpringBootApplication
public class So40908517Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(So40908517Application.class, args);
JmsTemplate template = context.getBean(JmsTemplate.class);
for (int i = 0; i < 3; i++) {
template.convertAndSend("foo", "bar");
}
context.getBean(CountDownLatch.class).await();
context.close();
}
@Bean
public CountDownLatch latch() {
return new CountDownLatch(3);
}
@JmsListener(destination = "foo", concurrency = "3-40")
public void listen(String in) {
System.out.println(in + " received on " + Thread.currentThread().getName());
latch().countDown();
}
}
结果(如预期的那样有3个线程):
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.2.RELEASE)
bar received on DefaultMessageListenerContainer-3
bar received on DefaultMessageListenerContainer-2
bar received on DefaultMessageListenerContainer-1