ActiveMQ-springboot-demo示例

使用springboot构建启动ActiveMQ的生产端及消费端。

https://github.com/phs999/MQ/tree/main/activemq-springboot

另外添加连接池依赖:

  <dependency>
            <groupId>org.messaginghub</groupId>
            <artifactId>pooled-jms</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

 

application.yml

spring:
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
    pool:
      enabled: true
      max-connections: 15
      idle-timeout: 10s
    packages:
      trust-all: true
    #启用topic支持
  jms:
    pub-sub-domain: true

添加bean注入-ListenerContainer:

package phs.activemqspringboot;

import javax.jms.ConnectionFactory;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

@Configuration
@EnableJms
public class ActiveMqConfig {
    // topic模式的ListenerContainer
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }
    // queue模式的ListenerContainer
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }
}

 消息接收:

package phs.activemqspringboot.service;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class Receiver {
    
    @JmsListener(destination = "springboot" ,containerFactory="jmsListenerContainerTopic")
    public void receive(String msg){
        System.out.println("收到消息:"+msg);
    }
    @JmsListener(destination = "user" ,containerFactory="jmsListenerContainerQueue")
    public void receive2(HashMap msg){
        System.out.println("收到消息:"+msg);
    }

}

消息发送:

package phs.activemqspringboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.*;

@Service
public class SenderService {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private JmsTemplate jmsTemplate;

    public void send(String des, String content) {
        jmsMessagingTemplate.convertAndSend(des,content);

    }
    public void send2(String des, String content) {
        jmsTemplate.send(des, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage("q23423");
                textMessage.setStringProperty("name","zd");
                return textMessage;
            }
        });
    }

    public void send3(String des, String content) throws JMSException {
        ConnectionFactory connectionFactory=jmsTemplate.getConnectionFactory();
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("user");
        //5.消息生产者
        MessageProducer producer = session.createProducer(queue);
        //Topic topic = session.createTopic("ff");
        //map
        MapMessage mapMessage = session.createMapMessage();
        mapMessage.setString("name", "zs");
        mapMessage.setInt("age", 34);

        mapMessage.setLongProperty("server", 1);
        producer.send(mapMessage);
        //7.关闭连接
        connection.close();

    }
}

添加Controller用于效果测试:

package phs.activemqspringboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import phs.activemqspringboot.service.Receiver;
import phs.activemqspringboot.service.SenderService;

import javax.jms.JMSException;

@RestController
public class MainController {

    @Autowired
    SenderService senderSrv;


    @RequestMapping("send")
    public String send() throws JMSException {
        senderSrv.send("springboot","hello");
        senderSrv.send2("springboot","hello");
        senderSrv.send3("springboot","hello");

        return "ok";
    }

}

启动应用后浏览器访问localhost:8080/send测试消息收发。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值