消息隊(duì)列之ActiveMQ(一)

一、浩言

我們要變的變態(tài),才能吸引更多的變態(tài)。

二、背景

最近在看消息隊(duì)列的東西,準(zhǔn)備引入到項(xiàng)目中使用,一直說(shuō)要自己先學(xué)會(huì)使用,在使用中學(xué)習(xí)。所以這算是新年的第一個(gè)周末就要加班,就在網(wǎng)上找資料自己嘗試配置下。順便紀(jì)錄下經(jīng)歷了,都是有坑要去踩的。

三、ActiveMQ 在Windows下的使用

我修改了配置文件activemq.xml,將0.0.0.0改成了127.0.0.1

     <transportConnectors>
            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
            <transportConnector name="openwire" uri="tcp://127.0.0.1:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600000"/>
            <transportConnector name="amqp" uri="amqp://127.0.0.1:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600000"/>
            <transportConnector name="stomp" uri="stomp://127.0.0.1:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600000"/>
            <transportConnector name="mqtt" uri="mqtt://127.0.0.1:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600000"/>
            <transportConnector name="ws" uri="ws://127.0.0.1:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600000"/>
        </transportConnectors>

注意點(diǎn):我在測(cè)試的時(shí)候一直報(bào)1G要大于maxFrameSize的100M,所以我在這個(gè)后面加了三個(gè)0

然后啟動(dòng)啟動(dòng)active


Paste_Image.png
Paste_Image.png

我們?cè)趙indows下使用這個(gè)命令查看端口netstat -ano linux下可以使用netstat -lntp查看


Paste_Image.png

在瀏覽器中輸入:http://127.0.0.1:8161/admin/index.jsp會(huì)有如下顯示

active-mq.png

<?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:amq="http://activemq.apache.org/schema/core"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd"> 
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://127.0.0.1:61616"></property>
        <!-- <property name="brokerURL" value="http://127.0.0.1:61616"></property> -->
        <property name="useAsyncSend" value="true"></property>
        <property name="alwaysSessionAsync" value="true"></property>
        <property name="useDedicatedTaskRunner" value="true"></property>
    </bean>

    <!-- 發(fā)送消息的目的地 -->
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
              <!-- 消息隊(duì)列的名字 -->
        <constructor-arg value="mahone.queue"/>
    </bean>
</beans> 

注意點(diǎn):我在這里面看到的我有個(gè)注釋的配置brokerURL,我剛剛開(kāi)始的時(shí)候一直配置成http...,然后怎么測(cè)試都不通,最后才發(fā)現(xiàn)是自己這里配置錯(cuò)了。

Paste_Image.png

下面附上測(cè)試代碼

package com.mouse.moon.test.mq;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMq {
    public static void sendWithAuto(ApplicationContext context) {  
        ActiveMQConnectionFactory factory = null;  
        Connection conn = null;  
        Destination destination = null;  
        Session session = null;  
        MessageProducer producer = null;  
        try {  
            destination = (Destination) context.getBean("destination");  
            factory = (ActiveMQConnectionFactory) context.getBean("targetConnectionFactory");  
            conn = factory.createConnection();  
            session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);  
            producer = session.createProducer(destination);  
            TextMessage message = session.createTextMessage("...Hello JMS-4!");  
            
            
            producer.send(message);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {//關(guān)閉生產(chǎn)者  
                producer.close();  
                producer = null;  
            } catch (Exception e) {
                
            }  
                try {//關(guān)閉session  
                    session.close();  
                    session = null;  
                }  
                catch (Exception e) {  
                    
                }  
                
                try {  //停止連接
                    conn.stop();  
                } catch (Exception e) {  
                    
                }  
            try {//關(guān)閉連接  
                conn.close();  
            } catch (Exception e) {  
            }  
        }  
    }  
    public static void main(String[] args) {  
        System.out.println("-------------start--------------");
        final ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/xml/activemq.xml");  
        sendWithAuto(context);  
        System.out.println("-------------end---------------");
    }  
}
Paste_Image.png
Paste_Image.png

在做測(cè)試的時(shí)候還遇到如下問(wèn)題

org.apache.activemq.ConfigurationException: There are 1 consumer options that couldn't be set on the consumer. Check the options are spelled correctly. Unknown parameters=[{perfetchSize=100}]. This consumer cannot be started.

這是在設(shè)置名字的時(shí)候附帶的參數(shù),我把這個(gè)去掉了就只能看到生產(chǎn)者的名字是"mahone.queue""
消費(fèi)端代碼:

package com.mouse.moon.test.mq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @author Mahone
 * @description:消費(fèi)端
 *
 */
public class TestConsumer  extends Thread implements MessageListener{
    
    private Connection conn = null;
    
    private Destination destination = null ;
    
    private Session session = null;

    @Override
    public void onMessage(Message message) {
        try{
            TextMessage tm = (TextMessage)message;
            System.out.println("receive message:"+tm.getText());
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
    
    @Override
    public void run(){
        receive();
    }
    
    public void receive(){
        ConnectionFactory factory = null;
        Connection conn = null ;
        
        try{
            final ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/xml/activemq.xml");
            factory = (ActiveMQConnectionFactory)context.getBean("targetConnectionFactory");
            conn = factory.createConnection();
            conn.start();
            session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = (Destination)context.getBean("destination");
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(this);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String args[]){
        TestConsumer mythread = new TestConsumer();
        mythread.start();
    }
}

Paste_Image.png

上述代碼有參考該<a >博客</a>

四、消息隊(duì)列的作用

1:解耦操作
例如說(shuō)其中A更新好了之后,其他地方(B,C,D)也需要更新,那么使用消息隊(duì)列之后,A更新后,將更新通知放入消息隊(duì)列中,B,C,D中需要更新只需要訂閱這個(gè)操作就好,實(shí)現(xiàn)解耦。

2、實(shí)現(xiàn)異步操作
例如上面說(shuō)的,A更新后,其他的更新都是異步進(jìn)行處理,并不需要同步操作。

3、對(duì)流量進(jìn)行控制
有時(shí)候訪問(wèn)多了,流量就大了,忽高忽低的,如果放入隊(duì)列中,按照順序進(jìn)行操作,即是先存起來(lái),然后在按照順序進(jìn)行發(fā)送。

五:浩語(yǔ)

                                 __                                                     
                  __  _  ____ __|  |__ _____    ___
                  \ \/ \/ /  |  \  |  \\__  \  /  _ \   
                   \     /|  |  /   Y  \/ __ \(  <_> )
                    \/\_/ |____/|___|  (____  /\____/ 
                                          \/     \/          
                       任何事情都是要靠努力和用心。                                                   
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,677評(píng)論 19 139
  • 背景介紹 Kafka簡(jiǎn)介 Kafka是一種分布式的,基于發(fā)布/訂閱的消息系統(tǒng)。主要設(shè)計(jì)目標(biāo)如下: 以時(shí)間復(fù)雜度為O...
    高廣超閱讀 13,059評(píng)論 8 167
  • 愿你有高跟鞋也有跑鞋,喝茶也能喝點(diǎn)小酒。愿你有勇敢的朋友,有牛逼的對(duì)手。愿你對(duì)過(guò)往的一切情深意重,但從不回頭。愿你...
    Tina是個(gè)妖怪小姐姐閱讀 297評(píng)論 0 1
  • 我是你的傘 愿為你遮擋炙烈的太陽(yáng) 清涼的依戀,炙烈的愛(ài) 你的笑在陽(yáng)光下最燦爛 在雨季來(lái)臨的時(shí)候 我纏綿在你的身旁 ...
    淘猴侯孫行閱讀 643評(píng)論 3 5
  • 熊洲游敦煌
    煜瑤閱讀 125評(píng)論 0 0

友情鏈接更多精彩內(nèi)容