您当前的位置: 首页 >  spring

SpringBoot整合ActiveMQ:队列 之 生产者消费者之定时发送消息

梁云亮 发布时间:2020-04-27 18:25:22 ,浏览量:3

示例:要求每隔3s往MQ发送一条消息。

第一步:创建Maven项目 Maven依赖:
    
        junit
        junit
        4.12
    
    
        org.springframework.boot
        spring-boot-starter
    
    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-test
        
            
                org.junit.vintage
                junit-vintage-engine
            
        
    
    
        org.springframework.boot
        spring-boot-starter-activemq
        2.2.6.RELEASE
    

    
    
        org.springframework
        spring-tx
        5.2.5.RELEASE
    
    
        org.springframework
        spring-jms
        5.2.5.RELEASE
    
    
        org.apache.activemq
        activemq-all
        5.15.12
    
    
    
        org.apache.activemq
        activemq-pool
        5.15.12
    
application.yml:
server:
  port: 80
  servlet:
    context-path: /am

spring:
  activemq:
    broker-url: tcp://hcmaster:61616  #ActiveMQ服务器地址及端口
    user: admin
    password: admin
    close-timeout: 5000
    send-timeout: 3000

    # 下面五行配置加上程序报错,程序启动不起来
#    in-memory: false # true表示使用内置的MQ,false表示连接服务器
#    pool:
#      enabled: true # true表示使用连接池,false表示每发送一条数据就创建一个连接
#      max-connections: 10 #连接池最大连接数
#      idle-timeout: 30000 #空闲的连接过期时间,默认为30s

  jms:
    pub-sub-domain: false # 默认值false表示Queue,true表示Topic

queueName: springboot-activemq-queue

# debug: true #显示Debug信息
第二步:配置队列Bean
@Component
public class ActiveMQQueueConfig {
    @Value("${queueName}")
    private String queueName;


    @Bean //在Spring中注入一个名称为activeMQConfig的Bean
    public Queue queue(){
        return  new ActiveMQQueue(queueName);
    }
}
第三步:生产者
@Component
public class QueueProducer {
    //注入springboot封装的工具类,它是Jmstemplate的封装类
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    public void sendQueue(String msg) {
        System.out.println("QueueProvider发送了消息 : " + msg);
        //方法一:添加消息到消息队列
        jmsMessagingTemplate.convertAndSend(queue, msg);
    }

    @Scheduled(fixedDelay = 3000) //该注解修改的方法不能有参数
    public void sendQueueScheduled() {
        String msg ="xixi";
        System.out.println("QueueProvider定时发送了消息 : " + msg+" "+System.currentTimeMillis());
        jmsMessagingTemplate.convertAndSend(queue, msg+System.currentTimeMillis());
    }

}
第四步:消费者
@Component
public class QueueConsumer {

    @JmsListener(destination = "${queueName}")
    public void receiveQueue(String text) {
        System.out.println("QueueConsumer消费了消息: "+text);
    }

}
测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableScheduling   //开启定时发送消息功能
public class QueueTests {
    //注入springboot封装的工具类,它是Jmstemplate的封装类
    @Autowired
    private QueueProducer queueProducer;

    @Test
    public void testSendQueue() {
        queueProducer.sendQueue("haha");
    }

    @Test
    public void testScheduledSendQueue() {//间隔时间定投
        queueProducer.sendQueueScheduled();
        try {
            System.in.read(); //让程序一直执行下去
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
关注
打赏
1688896170
查看更多评论

梁云亮

暂无认证

  • 3浏览

    0关注

    1121博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0498s