SpringBoot整合ActiveMQ異步創建用戶
什麼是ActiveMQ
ActiveMQ是一種開源的基於JMS(Java Message Servie)規範的一種消息中間件的實現,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";
}
}
下期再見。。。