MQ(Message Queue)消息隊列

一 使用場景:

    1.併發量比較大的地方
    2.存在耗時比較長的部分,進行異步處理
 

二:MQ和webservice的區別?

webservice是同步調用。mq是異常消息推送。
 

三:MQ、JMS、Apache ActionMQ 三者關係:

MQ:提出了一個解決問題的方案,消息隊列
JMS:sun公司針對MQ這種方法提出了技術標準API(面向接口)
Apache ActionMQ:是JMS技術規範的具體實現

四:ActiveMQ消息生成/消費的方式

ActiveMQ 使用的是標準生產者和消費者模型
有兩種模式 Queue、Topic
    1.queue (點對點):隊列,生產者生產了一個消息,只能由一個消費者進行消費
            若是消息沒有消費者,消息不會被丟棄
    2.topic(訂閱與發佈):話題,生產者生產了一個消息,能夠由多個消費者進行消費
            若是消息沒有消費者,消息就會被丟棄
            若是消費者不少,那麼服務器的性能會隨着訂閱者的增多而下降

五:ActiveMQ 的安裝和使用

1.解壓即安裝
2.bin目錄配置成環境變量
3.命令框
activemq  啓動服務器
4.訪問:http://localhost:8161/ 用戶名和密碼 都是 admin
 

六: 和spring整合開發

spring整合activemq開發:
模板工程:
主要是配置出 JmsTemplate模板
6.1導入maven依賴
<dependency>
      <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring - jms </ artifactId >
    <version>4.1.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId> activemq - a l l</artifactId>
    <version>5.14.0</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.1.7.RELEASE</version>
</dependency>
xml:
6.2導入名稱空間
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa=" http://www.springframework.org/schema/data/jpa xmlns:task="http://www.springframework.org/schema/task"
    xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/data/jpa 
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core.xsd">
 

6.3建立鏈接工廠

<!-- spring管理的包掃描 -->
<context:component-scan base-package=""></context:component-scan>
<!-- ActiveMQ 鏈接工廠 -->
    <!-- 真正能夠產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供-->
    <!-- 若是鏈接網絡:tcp://ip:61616;未鏈接網絡:tcp://localhost:61616 以及用戶名,密碼-->
    <amq:connectionFactory id="amqConnectionFactory"
        brokerURL="tcp://localhost:61616" userName="admin" password="admin"  />
 
    <!-- Spring Caching鏈接工廠 -->
    <!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory -->  
    <bean id="mqConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <!-- 目標ConnectionFactory對應真實的能夠產生JMS Connection的ConnectionFactory -->  
        <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
        <!-- 同上,同理 -->
        <!-- <constructor-arg ref="amqConnectionFactory" /> -->
        <!-- Session緩存數量 -->
        <property name="sessionCacheSize" value="100" />
    </bean>
6.4配置Spring JmsTemplate 的消息生產者
 <!-- Spring JmsTemplate 的消息生產者 start-->
 
    <!-- 定義JmsTemplate的Queue類型 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 這個connectionFactory對應的是咱們定義的Spring提供的那個ConnectionFactory對象 -->  
        <constructor-arg ref="mqConnectionFactory" />
        <!-- 非pub/sub模型(發佈/訂閱),即隊列模式 -->
        <property name="pubSubDomain" value="false" />
    </bean>
 
    <!-- 定義JmsTemplate的Topic類型 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
         <!-- 這個connectionFactory對應的是咱們定義的Spring提供的那個ConnectionFactory對象 -->  
        <constructor-arg ref="mqConnectionFactory" />
        <!-- pub/sub模型(發佈/訂閱) -->
        <property name="pubSubDomain" value="true" />
    </bean>
 
    <!--Spring JmsTemplate 的消息生產者 end-->
 
 

6.5代碼中發送消息

 

使用spring注入模板

    //注入jms模板
    @Autowired
    @Qualifier("jmsQueueTemplate")
    private JmsTemplate jmsTemplate;
 

使用模板發送消息

//調用MQ服務,發送消息
jmsTemplate.send("bos_sms",new MessageCreator() {
    
    @Override
    public Message createMessage(Session sessionthrows JMSException {
        MapMessage map = session.createMapMessage();
        map.setString("telephone"model.getTelephone());
        map.setString("msg"msg);
        return map;
    }
});
 

6.6小結

消息生產者配置完成。 以上所有放在一個配置文件中 使用的時候使用spring JmsTemplate來生產消息java

 

6.7消費者配置文件

在另外一個服務項目上新建一個配置文件 引用名稱空間
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/data/jpa 
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core.xsd ">
添加配合文件內容
<!-- 掃描包 -->
    <context:component-scan base-package="cn.pehua.bos.mq" />
    
    <!-- ActiveMQ 鏈接工廠 -->
    <!-- 真正能夠產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供-->
    <!-- 若是鏈接網絡:tcp://ip:61616;未鏈接網絡:tcp://localhost:61616 以及用戶名,密碼-->
    <amq:connectionFactory id="amqConnectionFactory"
        brokerURL="tcp://localhost:61616" userName="admin" password="admin"  />
 
    <!-- Spring Caching鏈接工廠 -->
    <!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory -->  
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <!-- 目標ConnectionFactory對應真實的能夠產生JMS Connection的ConnectionFactory -->  
        <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
        <!-- 同上,同理 -->
        <!-- <constructor-arg ref="amqConnectionFactory" /> -->
        <!-- Session緩存數量 -->
        <property name="sessionCacheSize" value="100" />
    </bean>
    
     <!-- 消息消費者 start-->
 
    <!-- 定義Queue監聽器 -->
    <jms:listener-container destination-type="queue" container-type="default" 
        connection-factory="connectionFactory" acknowledge="auto">
        <!-- 這裏的destination 寫的是消息隊列的名字 ref= 寫的是監聽代碼類在spring中管理的名字 -->
        <jms:listener destination="bos_sms" ref="smsConsumer"/>
        <jms:listener destination="bos_email" ref="emailConsumer"/>
    </jms:listener-container>
    
    <!-- 定義Topic監聽器 -->
<!--     <jms:listener-container destination-type="topic" container-type="default"  -->
<!--         connection-factory="connectionFactory" acknowledge="auto"> -->
<!--         <jms:listener destination="spring_topic" ref="topicConsumer1"/> -->
<!--         <jms:listener destination="spring_topic" ref="topicConsumer2"/> -->
<!--     </jms:listener-container> -->
 

6.9完成監聽代碼

import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
 
import org.springframework.stereotype.Service;
@Service("smsConsumer")
public class SmsConsumer implements MessageListener {
 
    @Override
    public void onMessage(Message message) {
        MapMessage mapMessage = (MapMessage) message;
        try {
            //String result = SmsUtils.sendSmsByHTTP(mapMessage.getString("telephoe"), mapMessage.getString("msg"));
            String result = "000/xxx";
            
            if(result.startsWith("000")){
                //發送成功
                System.out.println("短信發送成功");
            }else{
                //發送失敗
                throw new RuntimeException("短信發送失敗:信息碼:"+result);
            }
        
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

7.總結

  1. 導包
  2. 添加和spring 整合配置文件分爲生產者消息者
  3. 生產者生產兩種消息隊列一種Queue 消費一次就會消息。另外一種Topic生產一次能夠屢次消費
  4. 生產者會發送消息頭,接收方要配置接收
  5. 消費者代碼要實現接口
相關文章
相關標籤/搜索