springboot整合activemq

这是我使用的springboot版本

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 1.配置依赖

pom中加入:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.14.5</version>
        </dependency>

2.application.properties中加入activemq的配置:

#activemq
spring.activemq.broker-url=tcp://ip:端口
spring.activemq.in-memory=true
spring.activemq.user=用户名
spring.activemq.password=密码
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=10

3.消费者监听获取数据:

package com.hxkg.monitor.service;


import com.hxkg.monitor.entity.customized.MonitorInfoAO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 获取activemq消息并入库
 * @author yangfeng
 * @date 2019-03-25
 */
@Component
public class Consumer {

  @Resource
  private IMonitorInfoService monitorInfoService;

  private static Logger LOG = LoggerFactory.getLogger(Consumer.class);

  // 使用JmsListener配置消费者监听的队列,其中msg是接收到的消息
  @JmsListener(destination = "wlanmsg")
  public void receiveQueue(String msg) {

    LOG.info("Consumer收到的报文为:" + msg);

    if (!StringUtils.isEmpty(msg)) {
      //处理传递过来的字符串
      String[] result = msg.substring(1, msg.length() - 1).split(",");
      if (null != result && result.length > 0) {
        MonitorInfoAO monitor = new MonitorInfoAO();
        for (String o : result) {
          //key转为小写并使用trim对字符串前后去空格
          String tmp = o.toLowerCase().trim();
          if (tmp.startsWith("to=")) {
            monitor.setNextTo(tmp.replace("to=", ""));
          } else if (tmp.startsWith("from=")) {
            monitor.setSourceFrom(tmp.replace("from=", ""));
          } 
        }
        monitor.setCreateTime(new Date());
        //存入数据库表
        monitorInfoService.insert(monitor);
      }
    }
  }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值