1、activeMQ:html
1.下載:http://activemq.apache.org/download.htmlweb
2.解壓文件,在bin/win64(若是你的電腦是32位的,打開win32文件夾),雙擊activemq.bat,啓動activeMQ服務器spring
2、建立spring boot項目:apache
一、項目名稱jms_activeMQ_demojson
二、pom.xml:服務器
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<artifactId>jms_activeMQ_demo</artifactId>
<packaging>war</packaging>
<dependencies>
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency> <!-- json格式化 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.1</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<classifier>jdk15</classifier>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>${project.artifactId}</warName>
</configuration>
</plugin>
</plugins>
</build>
</project>app
三、application.properties文件:dom
# Tomcat
server.port=8761
server.servlet-path=/maven
###REDIS
####### #######################
# database name
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
#若是此處設置爲true,須要加activemq-pool依賴包,不然會自動配置失敗,報JmsMessagingTemplate注入失敗
spring.activemq.pool.enabled=false
#配置消息模型爲pub/sub方式,在application.properties添加以下配置信息
#這裏簡單對這個配置說明下:若是爲True,則是Topic;若是是false或者默認,則是queue
spring.jms.pub-sub-domain=truetcp
4.生產者開發:
/**
*
* @author 衛江波
* @2018年1月4日上午11:11:30
*/
@Service
public class Producer {
//也能夠注入JmsTemplate,JmsMessagingTemplate對JmsTemplate進行了封裝
@Autowired
private JmsMessagingTemplate jmsTemplate;
public void sendMessage(Destination destination, final String message){
jmsTemplate.convertAndSend(destination, message);
}
//接收通道名爲out.queue的信息
@JmsListener(destination = "out.queue")
public void receiveQueue(String text) {
System.out.println("i am product ,i recevice return :"+text);
}
}
5.接受者開發:
/**
* 接收消息 示例代碼
*
* @author 衛江波
* @2018年1月3日上午11:14:19
*/
@Component
public class Consumer {
/**接收消息的方法*/
//@JmsListener(destination = "mytest.queue")
@JmsListener(destination = "mytest.topic")
@SendTo("out.queue") //該註解的意思是將return回的值,再發送的"out.queue"隊列中
public String receiveMessage1(String message){
System.out.println(" I am consumer1,i receive message:" + message);
if(!StringUtils.isEmpty(message)){
JSONObject msg = JSONObject.fromObject(message);
System.out.println("I am consumer1,i receive result:"+msg.getString("result"));
}
return "return de context1";
}
/**接收消息的方法*/
//@JmsListener(destination = "mytest.queue")
@JmsListener(destination = "mytest.topic")
@SendTo("out.queue") //該註解的意思是將return回的值,再發送的"out.queue"隊列中
public String receiveMessage2(String message){
System.out.println(" I am consumer2,i receive message:" + message);
if(!StringUtils.isEmpty(message)){
JSONObject msg = JSONObject.fromObject(message);
System.out.println("I am consumer2,i receive result:"+msg.getString("result"));
}
return "return de context2";
}
}
6.測試類編寫:
/** * 點對點:消息生產者生產消息發佈到queue中,而後消息消費者從queue中取出,而且消費消息。這裏須要注意:消息被消費者消費之後,queue中再也不有存儲, * 因此消息消費者不可消費到已經被消費的消息。Queue支持存在多個消息消費者,可是對一個消息而言,只會有一個消費者能夠消費。 * 發佈/訂閱:消息生產者(發佈)將消息發佈到topic中,同時有多個消息消費者(訂閱)消費該消息。和點對點方式不一樣,發佈到topic的消息會被全部訂閱者消費。 * @author 衛江波 * @2018年1月4日上午11:20:39 */ @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootJmsTest { @Autowired private Producer producer; @Test public void contextLoads() throws InterruptedException { // 組裝數據 JSONObject data = new JSONObject(); data.put("result", "本地測試,activeMQ"); //JMS規範兩種經常使用的消息模型:點對點(point to point ,queue)和發佈/訂閱(publish/subscribe,topic)。 //Destination queue = new ActiveMQQueue("mytest.queue"); Destination topic = new ActiveMQTopic("mytest.topic"); // 推送消息 //producer.sendMessage(queue, data.toString()); producer.sendMessage(topic, data.toString()); System.out.println(" 消息 發送完畢 "); } }