ActiveMQ与Spring整合

2021-01-19 14:12

阅读:656

标签:override   red   message   icp   自带   system   rri   持久化   cep   

1、jar包依赖

  因为ActiveMQ5.12后的jar包自带spring,不方便与spring整合,可以使用ActiveMQ5.11.2的jar包

  (ActiveMQ默认的端口号为:8161)

 


        org.springframework
            spring-jms
        org.apache.activemq
            activemq-all
        

 

2、Spring配置文件

  2.1、queue:点对点主题(连接ActiveMQ默认的端口号为:61616)

    在该主题下,会自动将生产者每次提供的信息持久化,当出现多个消费者时,只有第一个消费者能获取生产者的信息

    生产者的配置如下:

    
    "targetConnectionFactroy" class="org.apache.activemq.ActiveMQConnectionFactory">
        "brokerURL" value="tcp://192.168.25.132:61616">"connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
        "targetConnectionFactory" ref="targetConnectionFactroy">"jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        "connectionFactory" ref="connectionFactory">"queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">
        "name" value="queue_text">     //自定义生产者名字
    

  生产者demo:

@Component
public class QueueProducer {
    @Autowired
    private JmsTemplate jmsTemplate;
    
    @Autowired
    private Destination queueTextDestination;
    
    /**
     * 发送消息
     */
    public void sendTextMessage(final String text){
        jmsTemplate.send(queueTextDestination, new MessageCreator() {
            
            @Override
            public Message createMessage(Session session) throws JMSException {
                
                return session.createTextMessage(text);
            }
        });
    }
}

  queue主题下消费者的配置:

    
    
    "myMessageListener" class="com.jd.demo.MyMessageListener">class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        "connectionFactory" ref="connectionFactory" />
        "destination" ref="queueTextDestination" />
        "messageListener" ref="myMessageListener" />
    

  

    2.1、topic:订阅式主题(连接ActiveMQ默认的端口号为:61616)

      在该主题下,生产者的消息不会被持久化,如果没有消费者接收,则消息就会丢失;如果有多个消费者在等待接收消息,生产者的消息会同时被所有消费者接收。

      生产者的配置如下:

    
    "targetConnectionFactroy" class="org.apache.activemq.ActiveMQConnectionFactory">
        "brokerURL" value="tcp://192.168.25.132:61616">"connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
        "targetConnectionFactory" ref="targetConnectionFactroy">"jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        "connectionFactory" ref="connectionFactory">"topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">
        "name" value="topic_text">

    生产者demo:

@Component
public class TopicProducer {
    @Autowired
    private JmsTemplate jmsTemplate;
    
    @Autowired
    private Destination topicTextDestination;
    
    //发送消息
    public void sendTextMessage(final String text){
        jmsTemplate.send(topicTextDestination, new MessageCreator() {
            
            @Override
            public Message createMessage(Session session) throws JMSException {
                
                return session.createTextMessage(text);
            }
        });
    }

}

 

    消费者配置如下:


    "myMessageListener" class="com.jd.demo.MyMessageListener">class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        "connectionFactory" ref="connectionFactory" />
        "destination" ref="topicTextDestination" />
        "messageListener" ref="myMessageListener" />
    

3、消费者demo

public class MyMessageListener implements MessageListener{

    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println("接收到的消息是:" + textMessage.getText());
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

ActiveMQ与Spring整合

标签:override   red   message   icp   自带   system   rri   持久化   cep   

原文地址:https://www.cnblogs.com/709539062rao/p/12907773.html


评论


亲,登录后才可以留言!