1.下載ActiveMQ
去官方網站下載:http://activemq.apache.org/
2.運行ActiveMQ
解壓縮apache-activemq-5.14.0-bin.zip,然後雙擊apache-activemq-5.14.0\bin\activemq.bat運行ActiveMQ程序。
啓動ActiveMQ以後,登陸:http://localhost:8161/admin/ ,賬戶和密碼都是admin,然後可以自己添加一個Queue,這次項目
我們通過代碼創建一個Queue.
好了,activeMQ就已經完成了部署,那麼怎麼把他運用到我們項目中呢,下面我就給大家介紹一下。
首先來看下我這個項目的整個目錄結構,總共分爲3個子項目,domain項目用於存放公共的實體類和工具類,打包成jar包供
其他2個包使用。Service項目則是activeMQ的提供者或者說是生產者,這裏主要是配置activeMQ的生成方式和創建Queue,
那麼剩下來的client項目當然就是MQ的使用者或者說是消費者了,那邊產生消息,這邊消費消息。那我們來看看具體的代碼
吧。
首先介紹domain的項目,這個項目裏面主要定義了三種實體類,User.java Client.java News.java 只是用於測試而已,
那就隨便看其中一個就好了,User.java
- package com.lwl.activemq.domain;
- import java.io.Serializable;
- /**
- * 用戶測試類
- * @author Administrator
- *
- */
- public class User implements Serializable{
- private static final long serialVersionUID = 1L;
- private long id;
- private String username;
- private String password;
- private String sex;
- private int age;
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "User [id=" + id + ", username=" + username + ", password="
- + password + ", sex=" + sex + ", age=" + age + "]";
- }
- }
domain項目添加好這3個實體類,就把這個項目通過maven打包成jar供其他2個項目使用即可。那接下來我們來看下service項目的代碼結構吧:
首先我們來看一下最主要的MQ的配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jms="http://www.springframework.org/schema/jms"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
- <!-- 這裏暴露內部統一使用的MQ地址 -->
- <bean id="internalTargetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL" value="tcp://localhost:61616" />
- </bean>
- <bean id="internalConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
- destroy-method="stop">
- <property name="connectionFactory" ref="internalTargetConnectionFactory" />
- <property name="maxConnections" value="20" />
- </bean>
- <!-- Spring提供的JMS工具類,它可以進行消息發送、接收等 -->
- <bean id="internalJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory" ref="internalConnectionFactory" />
- </bean>
- <!-- 推送給用戶信息 創建一個Queue-->
- <bean id="userServiceQueue" class="org.apache.activemq.command.ActiveMQQueue">
- <constructor-arg>
- <value>user.service.queue</value>
- </constructor-arg>
- </bean>
- <!-- 推送給新聞信息 創建一個Queue-->
- <bean id="newsServiceQueue" class="org.apache.activemq.command.ActiveMQQueue">
- <constructor-arg>
- <value>news.service.queue</value>
- </constructor-arg>
- </bean>
- <!-- 推送給客戶信息 創建一個Queue-->
- <bean id="clientServiceQueue" class="org.apache.activemq.command.ActiveMQQueue">
- <constructor-arg>
- <value>client.service.queue</value>
- </constructor-arg>
- </bean>
- </beans>
那我們看下怎麼運用定義的這個配置文件呢?
首先我們定義一個通用的推送接口PushService.java
- package com.lwl.activemq.service;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- /**
- * 推送的接口
- * @author Administrator
- * @create 2016-8-10 下午3:41:03
- * @version 1.0
- */
- public interface PushService {
- public final ExecutorService pushExecutor = Executors.newFixedThreadPool(10);
- public void push(Object info);
- }
然後又實現了3中不同的推送內容:ClientPushServiceImpl.java NewsPushServiceImpl.java UserPushServiceImpl.java就拿其中的一個來舉例,其他2個模式是一樣的
- package com.lwl.activemq.service.impl;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.Session;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Service;
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
- import com.alibaba.fastjson.JSON;
- import com.lwl.activemq.domain.User;
- import com.lwl.activemq.service.PushService;
- @Service("userPushService")
- public class UserPushServiceImpl implements PushService {
- @Autowired
- private JmsTemplate jmsTemplate;
- /**
- * 這裏是根據MQ配置文件定義的queue來注入的,也就是這裏將會把不同的內容推送到不同的queue中
- */
- @Autowired
- @Qualifier("userServiceQueue")
- private Destination destination;
- @Override
- public void push(final Object info) {
- pushExecutor.execute(new Runnable() {
- @Override
- public void run() {
- jmsTemplate.send(destination, new MessageCreator() {
- public Message createMessage(Session session) throws JMSException {
- User p = (User) info;
- return session.createTextMessage(JSON.toJSONString(p));
- }
- });
- }
- });
- }
- }
接口也已經實現好了,剩下的就是看我們怎麼調用它了,那我們看看控制器吧:
- package com.lwl.activemq.controller;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.lwl.activemq.domain.Client;
- import com.lwl.activemq.domain.News;
- import com.lwl.activemq.domain.User;
- import com.lwl.activemq.result.ResultRespone;
- import com.lwl.activemq.service.PushService;
- @Controller
- @RequestMapping("/push")
- public class PushController {
- @Resource(name="userPushService")
- private PushService userPushService;
- @Resource(name="newsPushService")
- private PushService newsPushService;
- @Resource(name="clientPushService")
- private PushService clientPushService;
- /**
- * 用戶推送
- * @param info
- * @return
- * @author Administrator
- * @create 2016-8-10 下午4:22:28
- */
- @RequestMapping(value="/user",method=RequestMethod.POST)
- @ResponseBody
- public ResultRespone userPush(User info){
- ResultRespone respone = new ResultRespone();
- try {
- userPushService.push(info);
- respone.setData(info);
- } catch (Exception e) {
- e.printStackTrace();
- respone = new ResultRespone(false, e.getMessage());
- }
- return respone;
- }
- /**
- * 新聞推送
- * @param info
- * @return
- * @author Administrator
- * @create 2016-8-10 下午4:22:38
- */
- @RequestMapping(value="/news",method=RequestMethod.POST)
- @ResponseBody
- public ResultRespone newsPush(News info){
- ResultRespone respone = new ResultRespone();
- try {
- newsPushService.push(info);
- respone.setData(info);
- } catch (Exception e) {
- e.printStackTrace();
- respone = new ResultRespone(false, e.getMessage());
- }
- return respone;
- }
- /**
- * 客戶推送
- * @param info
- * @return
- * @author Administrator
- * @create 2016-8-10 下午4:22:48
- */
- @RequestMapping(value="/client",method=RequestMethod.POST)
- @ResponseBody
- public ResultRespone clientPush(Client info){
- ResultRespone respone = new ResultRespone();
- try {
- clientPushService.push(info);
- respone.setData(info);
- } catch (Exception e) {
- e.printStackTrace();
- respone = new ResultRespone(false, e.getMessage());
- }
- return respone;
- }
- }
控制器也寫好了,剩下的就是前段頁面的調用了,那就快來看看吧index.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <script type="text/javascript" src="resources/jquery-1.9.1.js"></script>
- </head>
- <body>
- <br/><br/><br/>
- 用戶姓名:<input type="text" id="username" />
- 用戶密碼:<input type="text" id="password" />
- 用戶性別:<input type="text" id="sex" />
- <input type="button" value="推送用戶信息" id="pushUser" />
- <br/><br/><br/>
- 新聞標題:<input type="text" id="title" />
- 新聞內容:<input type="text" id="content" />
- 新聞路徑:<input type="text" id="url" />
- 新聞作者:<input type="text" id="author" />
- <input type="button" value="推送新聞信息" id="pushNews" />
- <br/><br/><br/>
- 客戶姓名:<input type="text" id="name" />
- 客戶地址:<input type="text" id="address" />
- 客戶手機:<input type="text" id="mobile" />
- <input type="button" value="推送客戶信息" id="pushClient" />
- <script type="text/javascript">
- $("#pushUser").click(function(){
- var data = {
- username : $("#username").val(),
- password : $("#password").val(),
- sex : $("#sex").val()
- };
- ajaxDo("/activemq-service/push/user",data);
- });
- $("#pushNews").click(function(){
- var data = {
- title : $("#title").val(),
- content : $("#content").val(),
- author : $("#author").val(),
- url : $("#url").val()
- };
- ajaxDo("/activemq-service/push/news",data);
- });
- $("#pushClient").click(function(){
- var data = {
- name : $("#name").val(),
- address : $("#address").val(),
- mobile : $("#mobile").val()
- };
- ajaxDo("/activemq-service/push/client",data);
- });
- function ajaxDo(url,data){
- $.ajax({
- url:url ,
- type: "post",
- dataType: "json",
- data: data,
- success:function(result){
- if(result.success){
- var obj = JSON.stringify(result.data);
- alert(obj);
- }else{
- alert(result.msg);
- }
- }
- });
- }
- </script>
- </body>
- </html>
現在代碼都已經完成了,那就可以啓動項目了,啓動項目之前首先要啓動activeMQ,然後再啓動activemq-service項目,打開瀏覽器我們就可以模擬推送內容了:
此時在刷新我們的MQ頁面你就會發現自動創建好了Queue,而且在User那個裏面會有1個消息未被消費掉:
發送端的代碼就這樣已經完成了,明天將會繼續把接收端的代碼寫出來,並且通過websocket推送到前端顯示出來