SpringBoot整合activemq

1 篇文章 0 订阅

今天终于有时间来写点东西了,今天就给大家带来一个简单的springboot整合activemq的心得:

项目整体的结构:

pom依赖:

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

application.propertiese配置:具体的意思我没写,大家可以去spring官网查看,毕竟我只是一个四级毕业的,翻译的不是太666

spring.application.name=activemq
server.port=9000
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.max-connections=50
#spring.activemq.user=admin
#spring.activemq.password=admin
spring.activemq.pool.block-if-full=true
spring.activemq.packages.trust-all=true
#配置消息确认模式
spring.jms.listener.acknowledge-mode=auto

ActiveMqCoonfig.java

@Configuration
@EnableJms
public class ActiveMqCoonfig {

    @Bean
    public Queue queue(){
        return new ActiveMQQueue("user");
    }

    @Bean
    public RedeliveryPolicy redeliveryPolicy(){
        RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
        //是否在每次尝试重新发送失败后,增长这个等待时间
        redeliveryPolicy.setUseExponentialBackOff(true);
        //重发次数,默认为6次   这里设置为20次
        redeliveryPolicy.setMaximumRedeliveries(20);
        //重发时间间隔,默认为1秒
        redeliveryPolicy.setInitialRedeliveryDelay(1);
        //第一次失败后重新发送之前等待500毫秒,第二次失败再等待500 * 2毫秒,这里的2就是value
        redeliveryPolicy.setBackOffMultiplier(2);
        //是否避免消息碰撞
        redeliveryPolicy.setUseCollisionAvoidance(false);
        //设置重发最大拖延时间-1 表示没有拖延只有UseExponentialBackOff(true)为true时生效
        redeliveryPolicy.setMaximumRedeliveryDelay(-1);

        return redeliveryPolicy;
    }
}

生产者代码:这里只是demo没打日志,想要什么日志自己可以加上

@RestController
public class ActiveMqController {
    @Autowired
    private LogService logService;
    @Autowired
    private JmsTemplate jmsTemplate;
    @RequestMapping("/msgSend/{phone}")
    public void msgSend(@PathVariable("phone") String phone){
        try {

//这里只是简单的String类型的发送,map。list。object同理只需要转换下类型就可以
            jmsTemplate.convertAndSend("user",phone);
        } catch (JmsException e) {
            e.printStackTrace();
        }
//可以记录日志到数据库
       Log log = new Log();
        log.setLogid(123456L);
        log.setPhone(phone);
        logService.saveLog(log);

        System.out.println("-----"+phone+"-------------------------");
    }
}

 

消费者代码:

@Component
public class Consumer {

    @JmsListener(destination = "user")
    public void getMsgFromActivemq(String message){
        System.out.println("----------------+++++++++++++=="+message);
    }
}

今天的分享就到这里了,之后还有redis,solr等常用的东西的分享!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值