ActiveMQ使用教程

jopen 10年前發布 | 285K 次閱讀 ActiveMQ 消息系統

ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規范的 JMS Provider實現,盡管JMS規范出臺已經是很久的事情了,但是JMS在當今的J2EE應用中間仍然扮演著特殊的地位。 

ActiveMQ特性列表 

1. 多種語言和協議編寫客戶端。語言: Java, C, C++, C#, Ruby, Perl, Python, PHP。應用協議: OpenWire,Stomp REST,WS Notification,XMPP,AMQP 
2. 完全支持JMS1.1和J2EE 1.4規范 (持久化,XA消息,事務) 
3. 對Spring的支持,ActiveMQ可以很容易內嵌到使用Spring的系統里面去,而且也支持Spring2.0的特性 
4. 通過了常見J2EE服務器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的測試,其中通過JCA 1.5 resource adaptors的配置,可以讓ActiveMQ可以自動的部署到任何兼容J2EE 1.4 商業服務器上 
5. 支持多種傳送協議:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA 
6. 支持通過JDBC和journal提供高速的消息持久化 
7. 從設計上保證了高性能的集群,客戶端-服務器,點對點 
8. 支持Ajax 
9. 支持與Axis的整合 
10. 可以很容易得調用內嵌JMS provider,進行測試 

1:下載 ActiveMQ 5.6.0 Release 

http://activemq.apache.org/download.html 

放到d盤 

ActiveMQ使用教程

2:運行apache-activemq服務:雙擊 activemq.bat 

ActiveMQ使用教程 

3:效果 

ActiveMQ使用教程

4:所需jar包 

ActiveMQ使用教程

5:spring配置文件applicationContext.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL">
            <value>tcp://localhost:61616?wireFormat.maxInactivityDuration=0</value>
        </property>
    </bean>
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory">
            <ref bean="connectionFactory"/>
        </property>
    </bean>
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0">
            <value>MessageQueue</value>
        </constructor-arg>
    </bean>
</beans>

這時主要是配置activemq服務信息與實現springjms的對應接口 

6:消息產生者 

package test;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.MessageCreator;

/**
 * 消息產生者
 * User: liuwentao
 * Time: 12-6-14 上午11:31
 */
public class MyMessageCreator implements MessageCreator {
    public int n = 0;
    private static String str1 = "這個是第 ";
    private static String str2 = " 個測試消息!";
    private String str = "";
    @Override
    public Message createMessage(Session paramSession) throws JMSException {
        System.out.println("MyMessageCreator  n=" + n);
        if (n == 9) {
            //在這個例子中表示第9次調用時,發送結束消息
            return paramSession.createTextMessage("end");
        }
        str = str1 + n + str2;
        return paramSession.createTextMessage(str);
    }
}

7:發送消息方 

package test;
import javax.jms.Destination;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
/**
 * 發送消息方
 * User: liuwentao
 * Time: 12-6-14 上午11:29
 */
public class MessageSender extends Thread {
    public static void main(String args[]) throws Exception {
        String[] configLocations = new String[] {"test/applicationContext.xml"};
        ApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
        JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
        Destination destination = (Destination) context.getBean("destination");
        for (int i = 1; i < 100; i++) {
            System.out.println("發送 i=" + i);
            //消息產生者
            MyMessageCreator myMessageCreator = new MyMessageCreator();
            myMessageCreator.n = i;
            jmsTemplate.send(destination, myMessageCreator);
            sleep(10000);//10秒后發送下一條消息
        }
    }
}

8:消息接收方 

package test;
import javax.jms.Destination;
import javax.jms.TextMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
/**
 * 消息接收方
 * User: liuwentao
 * Time: 12-6-14 上午11:32
 */
public class MessageReciver{
    public static void main(String args[]) throws Exception {
        String[] configLocations = new String[] {"test/applicationContext.xml"};
        ApplicationContext context = new ClassPathXmlApplicationContext(configLocations);

        JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
        Destination destination = (Destination) context.getBean("destination");

        TextMessage msg = null;
        //是否繼續接收消息
        boolean isContinue = true;
        while (isContinue) {
            msg = (TextMessage) jmsTemplate.receive(destination);
            System.out.println("收到消息 :" + msg.getText());
            if (msg.getText().equals("end")) {
                isContinue = false;
                System.out.println("收到退出消息,程序要退出!");
            }
        }
        System.out.println("程序退出了!");
    }
}

9:測試 

運行 發送方和接收方 (順序隨便) main文件,效果如下: 

ActiveMQ使用教程 

ActiveMQ使用教程 

注:即使 接收方啟動晚,或者 發送方關閉了, 接收方都會正常接收完所有數據 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!