zik*_*ack 5 spring spring-jms spring-boot
我正在尝试使用以下代码段听取主题.但是它默认监听队列.在这种情况下没有xml配置.我完全依赖注释.此外,我完全依赖Spring引导提供的AutoConfiguration.我不知道如何将目标类型设置为主题,在JmsListener中.Spring JMS大师请帮忙.
@Component
public class MyTopicListener {
@JmsListener(destination = "${trans.alert.topic}")
public void receiveMessage(TransactionAlert alert) {
logger.info("AlertSubscriberEmail :: Sending Email => <" + alert + ">");
}
}
Run Code Online (Sandbox Code Playgroud)
Ram*_*A.G 12
我刚从以下网站获取了完整的Spring启动示例:https://github.com/spring-guides/gs-messaging-jms/
在此,它被创建用于从队列发送和接收消息.要将此更改为主题,您必须在Factory实例中设置Pub-Sub属性.
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType;
import javax.jms.ConnectionFactory;
@SpringBootApplication
@EnableJms
public class JmsSampleApplication {
public void registerBeans(ConfigurableApplicationContext context ){
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(JmsTemplate.class);
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
builder.addPropertyValue("connectionFactory", cachingConnectionFactory); // set property value
DefaultListableBeanFactory factory = (DefaultListableBeanFactory) context.getAutowireCapableBeanFactory();
factory.registerBeanDefinition("jmsTemplateName", builder.getBeanDefinition());
}
@Bean
public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain(true);
// This provides all boot's default to this factory, including the message converter
configurer.configure(factory, connectionFactory);
// You could still override some of Boot's default if necessary.
return factory;
}
@Bean
public JmsListenerContainerFactory<?> queueListenerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
//factory.setPubSubDomain(true);
// This provides all boot's default to this factory, including the message converter
configurer.configure(factory, connectionFactory);
return factory;
}
@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(JmsSampleApplication.class, args);
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
// Send a message with a POJO - the template reuse the message converter
System.out.println("Sending an email message.");
jmsTemplate.convertAndSend("mailbox.topic", new Email("info@example.com", "Hello"));
jmsTemplate.convertAndSend("mailbox.queue", new Email("info@example.com", "Hello"));
}
}
Run Code Online (Sandbox Code Playgroud)
听众
package org.springboot.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* Created by RGOVIND on 10/20/2016.
*/
@Component
public class HelloTopicListener {
@JmsListener(destination = "mailbox.topic", containerFactory = "topicListenerFactory")
public void receiveTopicMessage(Email email) {
System.out.println("Received <" + email + ">");
}
@JmsListener(destination = "mailbox.queue", containerFactory = "queueListenerFactory")
public void receiveQueueMessage(Email email) {
System.out.println("Received <" + email + ">");
}
}
Run Code Online (Sandbox Code Playgroud)
完成此操作后,您就可以订阅所选主题.
当然,有多种方法,您可以为不同的jmsTemplates创建一个bean映射,每个jmsTemplates都可以根据队列或主题在需要时使用.模板和bean可以在您选择喜欢的SO方法中讨论的方法中实例化.希望能帮助到你
dle*_*rob 12
标记正确的答案几乎是正确的.它仍然无法工作,因为:
factory.setPubSubDomain(true)
Run Code Online (Sandbox Code Playgroud)
必须来之后:
configurer.configure(factory, connectionFactory);
Run Code Online (Sandbox Code Playgroud)
否则,在配置默认值时,pubSubDomain标志设置为true将丢失,并且工厂实例仍将使用队列而不是主题.
在Spring Boot的Application.properties中,尝试设置以下属性:
spring.jms.pub-sub-domain=true
Run Code Online (Sandbox Code Playgroud)
然后,将此属性用于您用来侦听主题的容器工厂。
| 归档时间: |
|
| 查看次数: |
13443 次 |
| 最近记录: |