ActiveMQ(li)

1、ActiveMQhtml

  首先,ActiveMQ不是一個框架,它不是struct,webx,netty這種框架,它更像是tomcat服務器,由於你使用它以前必須啓動它,activeMQ和JMS的關係有點相似於tomcat和servlet的關係,tomcat實現了servlet規範,同理activeMQ實現了JMS規範,它們都是服務器,在使用其服務以前須要先把服務器啓動起來。java

//#準備activeMQ的環境和tomcat的環境同樣簡單。
//1.下載:wget http://apache.freelamp.com//activemq/apache-activemq/5.4.2/apache-activemq-5.4.2-bin.tar.gz
//2.解壓:tar xvf apache-activemq-5.4.2-bin.tar.gz
//3.啓動:進入bin目錄執行activemq腳本,輸入 http://localhost:8161,ActiveMQ服務器成功啓動以後,咱們就能夠向這個服務器發送消息,或者從這個服務器接收消息了

  上面是Linux環境下的安裝,解壓和啓動。下面是Windows環境下的ActiveMQ的配置和使用。http://activemq.apache.org/download.html下載穩定版本。web

2、ActiveMQ發消息apache

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
 
public class SendMessage {
 
    private static final String url          = "tcp://10.20.156.22:61616";
    private static final String QUEUE_NAME   = "yunpeng.jiangyp";
    protected String            expectedBody = "<hello>world!</hello>";
 
    public void sendMessage() throws JMSException {
 
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();
 
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination desion = session.createQueue(QUEUE_NAME);
        MessageProducer producer = session.createProducer(desion);
        TextMessage message = session.createTextMessage(expectedBody);
        producer.send(message);
        connection.close();
    }
 
    public static void main(String[] args) throws JMSException {
        SendMessage sndMsg = new SendMessage();
        sndMsg.sendMessage();
    }
}

這段代碼很簡單,就是向activemq服務器發送一條消息,當成功發送給activemq以後,能夠在(http://10.20.156.22:8161/admin/queues.jsp)activemq服務器的控制檯看到這條消息。控制檯顯示yunpeng.jiangyp堆積了2條消息。tomcat

3、ActiveMQ收消息服務器

 

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
 
public class ReceiveMessage {
 
    private static final String url        = "tcp://10.20.156.22:61616";
    private static final String QUEUE_NAME = "yunpeng.jiangyp";
 
    public static void main(String[] args) throws JMSException {
 
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
        Connection connection = factory.createConnection();
        connection.start();
 
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination desion = session.createQueue(QUEUE_NAME);
        MessageConsumer consumer = session.createConsumer(desion);
        while (true) {
 
            Message message = consumer.receive(1000);
            if (message == null) break;
            if (message instanceof TextMessage) {
                TextMessage textMsg = (TextMessage) message;
                String msg = textMsg.getText();
                System.out.println(msg);
            }
        }
        connection.close();
 
    }
}

 

  這段代碼也很簡單,就是從activemq服務器接收一條消息,能夠發現發送和接收消息都須要建立Connection,Session,Queue,惟一不一樣的是發送消息是建立MessageProducer,而接收消息是建立MessageConsumer,這也很是形象的證實了activemq的生產者和消費者模式。運行這段代碼會打印出以前發送的消息信息,同時控制檯會顯示兩條堆積的消息被消費。session

  在JMS中兩種發送消息的方式,包含point-to-point 和 publish/subscribe。框架

4、jsp

相關文章
相關標籤/搜索