簡體   English   中英

如何使用spring boot jms聽主題

[英]How to listen to topic using spring boot jms

我正在嘗試使用以下代碼段聽取主題。 但是它默認監聽隊列。 在這種情況下沒有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 + ">");
        }
    }

我剛從以下網站獲取了完整的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"));

    }
}

聽眾

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 + ">");
    }
}

完成此操作后,您就可以訂閱所選主題。

當然,有多種方法,您可以為不同的jmsTemplates創建一個bean映射,每個jmsTemplates都可以根據隊列或主題在需要時使用。 模板和bean可以在您選擇喜歡的SO方法中討論的方法中實例化。 希望能幫助到你

標記正確的答案幾乎是正確的。 它仍然無法工作,因為:

factory.setPubSubDomain(true) 

必須來之后:

configurer.configure(factory, connectionFactory);

否則,在配置默認值時,pubSubDomain標志設置為true將丟失,並且工廠實例仍將使用隊列而不是主題。

在Spring Boot的Application.properties中,嘗試設置以下屬性:

spring.jms.pub-sub-domain=true

然后,將此屬性用於您用於收聽主題的容器工廠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM