SpringBoot整合ActiveMQ异步创建用户

SpringBoot整合ActiveMQ异步创建用户

        • 什么是ActiveMQ
        • ActiveMQ的安装与启动
        • 整合
            • pom.xml
            • application.yml
            • User.java
            • UserConvert.java
            • ActivemqConfig.java
            • 接受消息AMQLitenner.java
            • 测试TestConvert.java

什么是ActiveMQ

ActiveMQ是一种开源的基于JMS(Java Message Servie)规范的一种消息中间件的实现,ActiveMQ的设计目标是提供标准的,面向消息的,能够跨越多语言和多系统的应用集成消息通信中间件。

ActiveMQ的安装与启动

ActiveMQ官网
在这里插入图片描述

解压后进入apache-activemq-5.15.9/bin目录
执行./activemq start启动ActiveMQ
浏览器输入ActiveMQ启动的服务器ip:8161便可进入web界面,点击Manage ActiveMQ broker可以查看消息推送的状态,默认账号密码为admin,admin

整合

pom.xml
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

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

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

        <!-- mq消息集成 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
        </dependency>
    </dependencies>
application.yml
spring:
  activemq:
    broker-url: tcp://localhost:61616
server:
  port: 8083
User.java
public class User implements Serializable {
     

    private Integer id;

    private String name;
}
UserConvert.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pojo.User;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.io.IOException;


public class UserConvert implements MessageConverter {
     
    /**
     * 发送消息时转换
     */
    @Override
    public Message toMessage(Object o, Session session) throws JMSException, MessageConversionException {
     
        try {
     
            ObjectMapper mapper = new ObjectMapper();
            String ret = mapper.writeValueAsString(o);
            return session.createTextMessage(ret);
        } catch (JsonProcessingException e) {
     
            e.printStackTrace();
        }
        return session.createTextMessage("error");
    }

    /**
     * 接受消息时转换
     */
    @Override
    public User fromMessage(Message message) throws JMSException, MessageConversionException {
     
        try {
     
            TextMessage msg = (TextMessage) message;
            ObjectMapper mapper = new ObjectMapper();
            String text = msg.getText();
            return mapper.readValue(text, User.class);
        } catch (IOException e) {
     
            e.printStackTrace();
        }
        return null;
    }
}

ActivemqConfig.java
import com.jms.UserConvert;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;

/**
 * @version v1.0
 * @ProjectName: springboot_activemq
 * @ClassName: ActivemqConfig
 * @Author: xueqimiao
 * @Date: 2020/4/3 17:11
 */
@Configuration
public class ActivemqConfig {
     

    @Value("${spring.activemq.broker-url}")
    private String broker_url;

    @Bean
    public JmsMessagingTemplate jmsMessagingTemplate() {
     
        return new JmsMessagingTemplate(new ActiveMQConnectionFactory(broker_url));
    }

    @Bean
    public JmsListenerContainerFactory<?> containerFactory() {
         //配置监听容器工厂
        DefaultJmsListenerContainerFactory containerFactory = new DefaultJmsListenerContainerFactory();
        containerFactory.setConnectionFactory(new ActiveMQConnectionFactory(broker_url));
        containerFactory.setPubSubDomain(true);
        return containerFactory;
    }

    @Bean
    public JmsListenerContainerFactory queueContainer(ActiveMQConnectionFactory connectionFactory){
     
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(Boolean.FALSE);//支持queue
        factory.setMessageConverter(new UserConvert());
        return  factory;
    }
}
接受消息AMQLitenner.java
import com.pojo.User;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class AMQLitenner {
     

    @JmsListener(destination = "user_create_queue", containerFactory = "queueContainer")
    public void user_create_queue(User user) {
     
        System.out.println("user_create_queue 接受到消息:  " + user);
    }

}
测试TestConvert.java
import com.pojo.User;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @version v1.0
 * @ProjectName: springboot_activemq
 * @ClassName: testConvert
 * @Author: Administrator
 * @Date: 2020/4/7 10:12
 */
@Controller
public class TestConvert {
     

    @Autowired
   private JmsTemplate jmsTemplate;
   
    @GetMapping("/testConvert")
    @ResponseBody
    public String test(){
     
        ActiveMQQueue destination = new ActiveMQQueue("user_create_queue");
        jmsTemplate.setMessageConverter(new UserConvert());
        jmsTemplate.convertAndSend(destination,new User(1,"测试一"));
        return "success";
    }
}

SpringBoot整合ActiveMQ异步创建用户_第1张图片
在这里插入图片描述

下期再见。。。

你可能感兴趣的:(java,springboot,activemq,activemq,java,spring,boot,中间件,spring)