ActiveMQ集成SpringBoot

ActiveMQ集成SpringBoot

(1)创建项目导入依赖

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

(2)编写application.xml

#ActiveMQ配置
spring:
  activemq:
    broker-url: tcp://127.0.0.1:61616
    user: admin
    password: admimn
    pool:
      max-connections: 100
  jms:
    pub-sub-domain: true   #false:队列  true:主题

#队列名称
myqueue: boot-activemq-queue
#主题名称
mytopic: boot-activemq-topic

(3)编写配置类

/**
 * 队列配置类
 */
@Component
public class queueConfig {

    @Value("${myqueue}")
    private String myQueue;

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

```java
/**
 * 主题配置类
 */
@Component
public class TopicConfig {

    @Value("${mytopic}")
    private String mytopic;

    @Bean
    public Topic topic(){
        return new ActiveMQTopic(mytopic);
    }
}

(4)编写生产类

/**
 * Queue生产类
 */
//@Component
public class QueueProducer {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Queue queue;

    @SuppressWarnings("deprecation")
    public void produceMsg(){
        jmsMessagingTemplate.convertAndSend(queue,"message:"+ new Date().toLocaleString());
    }
}
/**
 * topic生产类
 */
@Component
public class TopicProducer {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Topic topic;

    @SuppressWarnings("deprecation")
    //定时发送消息 3秒
    @Scheduled(fixedDelay = 3000)
    public void produceMsg(){
        jmsMessagingTemplate.convertAndSend(topic,"message:"+new Date().toLocaleString());
    }
}

(5)编写消费类

/**
 * queue消费类
 */
@Component
public class QueueConsumer {

    @JmsListener(destination = "${myqueue}")
    public void receive(TextMessage textMessage) throws JMSException {
        System.out.println("Queue消费者接受消息:"+textMessage.getText());
    }
}


/**
 * topic消费类
 */
@Component
public class TopicConsumer {

    @JmsListener(destination = "${mytopic}")
    public void receive(TextMessage textMessage) throws JMSException {
        System.out.println("Topic收到的信息:"+textMessage.getText());
    }
}

(5)测试

@SpringBootApplication
//启用jms
@EnableJms
//定时发送消息
@EnableScheduling
public class ActivemqbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(ActivemqbootApplication.class, args);
    }

}

@SpringBootTest
class ActivemqbootApplicationTests {


    @Autowired
    TopicProducer topicProducer;

    @Test
    void contextLoads() {
        topicProducer.produceMsg();
    }

}

注意:
队列模式:
先启动ActivemqbootApplicationTests方法,在启动ActivemqbootApplication

主题模式:
先启动ActivemqbootApplication ,后启动ActivemqbootApplicationTests

一个项目同时监听队列和主题配置


(1)application.yml配置

#ActiveMQ配置
spring:
  activemq:
    broker-url: tcp://127.0.0.1:61616
    user: admin
    password: admin
  jms:
    pub-sub-domain: false

(2)创建config类

@Configuration
public class TopicConfig {

    @Bean("configContainerFactory")
    public DefaultJmsListenerContainerFactory configContainerFactory(ConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory containerFactory = new DefaultJmsListenerContainerFactory();
        containerFactory.setConnectionFactory(connectionFactory);
        containerFactory.setPubSubDomain(true);
        return containerFactory;
    }

}

(4)生产者类

@RestController
@RequestMapping("/active")
public class ActiveMQProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    /**
     * 队列生产者
     * @return
     */
    @RequestMapping("/sendqueue")
    public Map<String,Object> queuehello(){
        Map<String,Object> map = new HashMap<>();
        jmsTemplate.setDefaultDestination(new ActiveMQQueue("queue-hello"));
        jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage("Hello-queue------" + new Date().toLocaleString());
                map.put("resources","发送成功");
                return textMessage;
            }
        });
        return map;
    }
    /**
     * 队列生产者
     * @return
     */
    @RequestMapping("/sendtopic")
    public Map<String,Object> topichello(){
        Map<String,Object> map = new HashMap<>();
        jmsTemplate.setDefaultDestination(new ActiveMQTopic("topic-hello"));
        jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage("Hello-queue----" + new Date().toLocaleString());
                map.put("resources","发送成功");
                return textMessage;
            }
        });
        return map;
    }

}

(4)消费类

@Component
public class ActiveMqConsumer {

    @JmsListener(destination = "queue-hello")
    public void queueConsumer(TextMessage textMessage) throws JMSException {
        System.out.println("Queue收到的信息:"+textMessage.getText());
    }
    //containerFactory 名称与config类的@Bean("configContainerFactory")一样
    @JmsListener(destination = "topic-hello",containerFactory = "configContainerFactory")
    public void topicConsumer(TextMessage textMessage) throws JMSException {
        System.out.println("Topic收到的信息:"+textMessage.getText());
    }
}

(5)测试类
1.消费者启动类

@SpringBootApplication
@EnableJms
public class ActivemqConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ActivemqConsumerApplication.class, args);
        System.out.println("消费者启动");
    }
}

2.生产者启动类

@SpringBootApplication
@EnableJms
public class ActivemqProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ActivemqProducerApplication.class, args);
        System.out.println("生产者项目");
    }
}

如果哪里不对还望各位大佬指导

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值