springboot2整合ActiveMQ

直接上代码,按流程搭建配置即可,其他讲解网上肯定可以自己跟进需要搜索

1、导入依赖

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

2、添加配置

spring:
  activemq:
    broker-url: tcp://192.168.199.145:61616
    in-memory: false #true 使用内置的MQ,false连接服务器
    user: admin
    password: admin
    pool:
      enabled: true #开启连接池
      max-connections: 10

3、相关bean对象

import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;

@Configuration
public class BeanConfig {

    @Autowired
    private Environment environment;

    @Bean
    public ConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(environment.getProperty("spring.activemq.broker-url"));
        connectionFactory.setUserName(environment.getProperty("spring.activemq.user"));
        connectionFactory.setPassword(environment.getProperty("spring.activemq.password"));
        return connectionFactory;
    }

    @Bean
    public JmsTemplate genJmsTemplate() {
        return new JmsTemplate(connectionFactory());
    }

    @Bean
    public JmsMessagingTemplate jmsMessageTemplate() {
        return new JmsMessagingTemplate(connectionFactory());
    }
}

4、send信息

import com.example.demo.service.ProducerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.jms.Destination;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private ProducerService producerService;

    @GetMapping("/activemq")
    private Object order(String msg) {
        try {
            Destination destination = new ActiveMQQueue("order.queue");
            producerService.sendMsg(destination, msg);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "success";
    }
}

5、消费信息

@Component
public class MyListener {

    @JmsListener(destination = "order.queue") // 监听test队列
    public void receiveQueue(String text) {
        System.out.println("Message: " + text);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咛果果

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值