springboot整合activemq配置yml

1、加入依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

如果使用pool的话, 就需要在pom中加入以下依赖:

<dependency>
     <groupId>org.apache.activemq</groupId>
     <artifactId>activemq-pool</artifactId>
     <version>5.14.5</version>
</dependency>

2、yml文件

spring:
    activemq:
        broker-url: tcp://192.168.0.1:61616
        user: admin
        password: admin
        pool:
         # 使用flase,此处改为true报错,不清楚什么原因
          enabled: false
          max-connections: 10

queueName: publish.queue
topicName: publish.topic

3、创建生产者producer

@Service("producer")
public class Producer {

    @Autowired
    private JmsMessagingTemplate  jmsTemplate;

    public void sendMsg(String destinationName ,String message){
        System.out.println("------------ 发送消息"+ message);
        Destination destination = new ActiveMQQueue(destinationName);
        jmsTemplate.convertAndSend(destination,message);
    }
}

4、创建消费者consumer

@Component
public class Consumer {
    @JmsListener(destination = "test.queue")
    public void listen(String text){
        System.out.println("<<<========= 收到消息" + text);
    }
}

注意: @JmsListener是一个可重复的注解,在java7及以下版本jdk中,可以使用@JmsListeners代替它。

5、测试类

 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class JMSProducerTest {

    @Autowired
    private Producer producer;

    @Test
    public void sendMsg() throws Exception {
        for (int i = 0; i < 10 ; i++) {
            producer.sendMsg("test.queue","Queue Message"+i);
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值