消息队列 1对1 ActiveMq

依赖===》消息队列核心发送--接收类

依赖:

        <!--消息队列activemq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

消息队列核心发送--接收类:

package com.test.springboot.util;


import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Destination;

/**
 * @Company:wftdlx
 * @Author: wjf
 * @Description: 消息队列  1对1
 * @Date: Created in 9:08 2019/5/21
 */
//发送消息 --  接收消息
@Component
public class ActiveMqHandle {

    @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
    JmsMessagingTemplate jmsTemplate;

    // 发送消息:destination是发送到的队列,message是待发送的消息
    public void producerSendMessageMessage(String message) {
        Destination destination = new ActiveMQQueue("destination1");
        jmsTemplate.convertAndSend(destination, message);
    }


    // 接收消息: 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
    @JmsListener(destination = "destination1")
    public void consumerReceiveMessage(String text) throws Exception {
         System.out.println("Consumer收到的报文为:" + text);
    }


}
    
package com.test.springboot;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJmsActiveMqlicationTests {

    @Autowired
    com.test.springboot.util.ActiveMqHandle ActiveMqHandle;

    @Test
    public void contextLoads() throws InterruptedException {


        for (int i = 0; i < 100; i++) {
            ActiveMqHandle.producerSendMessageMessage("myname is chhliu!!!");
        }
    }

}

  

猜你喜欢

转载自www.cnblogs.com/ynhk/p/10898411.html