ActiveMQ integration to solve the problem Springboot send and receive messages topic

Environment to build

1. Create a maven project (jar)

2.pom.xml add dependencies

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
    </dependencies>

 

3. Write bootstrap class

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

 

4. Add the corresponding configuration file in the configuration application.properties resources in

url = tcp-spring.activemq.broker: // 192.168.25.131:61616 
# activeMQ this url to the server where the link

 

5. In the class injection JmsMessageTemplate

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;

6. A method of transmitting a message to call JmsMessageTemplate

jmsMessagingTemplate.convertAndSend ( "queue message name", "message content");

Set message type

Message type disposed in the guide category

Queue

@Bean(name="queue")
public Destination getQueue(){
    return new ActiveMQQueue("queue_test");
}

Topic

@Bean(name="topic")
public Destination getTopic(){
    return new ActiveMQTopic("topic_test");
}

Class injection sent message type

Destination injection type objects send messages transmitted message class

Queue

@Resource(name="queue")
private Destination queue;

Topic

@Autowired
@Qualifier(value="topic")
private Destination topic;

Send a message

jmsMessageTemplate.convertAndSend (queue, "the content of the message" ); 
jmsMessageTemplate.convertAndSend (Topic, "the content of the message");

Write consumer

@Component
public class ActiveMQConsumer {
    //接收queue消息
    @JmsListener(destination = "queue_test")
    public void handler(String message){
        System.out.println(message);
    }
    //接收topic消息
    @JmsListener(destination = "topic_test")
    public void handlerTopic(String msessage){
        System.out.println(msessage);
    }
}

Start the test

The method of the controller class added 
@RequestMapping ( "/ Send" )
 public  void sendQueue () { 
    jmsMessagingTemplate.convertAndSend (Queue, "which is a message Queue" ); 
    jmsMessagingTemplate.convertAndSend (Topic, "which is a message Topic" ); 
}

After the run, the console output only the Queue message

problem

Springboot integrated mode can only monitor ActiveMQ Message Queue queue for processing, so how to deal topic messages?

solve:

Add the following to the file Springboot of application.properties

= Sub-Domain-spring.jms.pub to true    // default is false, publish open subscription model

Start the test

After these changes, we can only listen topic of messages, queue messages and can not get.

solve:

Only processed by custom listener class

Add connectionFactory property in @JmsListener listener class

@Component
public class ActiveMQConsumer {
    //接收queue消息
   @JmsListener(destination = "queue_test",containerFactory =     
                   "queueListenerContainerFactory")
    public void handler(String message){
        System.out.println(message);
    }
    //接收topic消息
    @JmsListener(destination = "topic_test",containerFactory = 
                "topicListenerContainerFactory")
    public void handlerTopic(String msessage){
        System.out.println(msessage);
    }
}

Create a profile class that provides two listeners plant configuration in the configuration class

@Configuration
public class ConsumerConfiguration {

    @Value("${spring.activemq.broker-url}")
    private String host;

    @Bean
    public ConnectionFactory getActiveMqConnection(){
        return new ActiveMQConnectionFactory(host);
    }

    @Bean(name="queueListenerContainerFactory")
    public JmsListenerContainerFactory queueListenerContailerFactory(ConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(false);
        return factory;
    }
    @Bean(name="topicListenerContainerFactory")
    public JmsListenerContainerFactory topicListenerContainerFactory(ConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }
}

Run the test

 

Guess you like

Origin www.cnblogs.com/sjq0928/p/11371620.html