一、ActiveMQ
首先,ActiveMQ不是一個框架,它不是struct,webx,netty這種框架,它更像是tomcat服務器,因為你使用它之前必須啟動它,activeMQ和JMS的關系有點類似於tomcat和servlet的關系,tomcat實現了servlet規范,同理activeMQ實現了JMS規范,它們都是服務器,在使用其服務之前需要先把服務器啟動起來。
//#准備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下載穩定版本。
二、ActiveMQ發消息
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條消息。
三、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的生產者和消費者模式。運行這段代碼會打印出之前發送的消息信息,同時控制台會顯示兩條堆積的消息被消費。
在JMS中兩種發送消息的方式,包含point-to-point 和 publish/subscribe。
四、