背景:ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。
ActiveMQ的持久化:http://blog.csdn.net/xh16319/article/details/12144953
選擇ActiveMQ作爲JMS的入門學習中間件,是因爲其擁有以下優點
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.完全支持JMS1.1和J2EE 1.4規範 (持久化,XA消息,事務)
5.通過了常見J2EE服務器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的測試,其中通過JCA 1.5 resource adaptors的配置,可以讓ActiveMQ可以自動的部署到任何兼容J2EE 1.4 商業服務器上
6.支持多種傳送協議:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
7.從設計上保證了高性能的集羣,客戶端-服務器,點對點
8.支持Ajax
9.支持與Axis的整合
10.可以很容易得調用內嵌JMS provider,進行測試
學會了ActiveMQ之後,其它供應商的MQ也可以在短時間內快速上手。
安裝:
ActiveMQ(本文簡稱MQ)要求JDK1.5以上,推薦1.6以上版本。還沒安裝JDK的朋友,請先安裝,在此不贅訴了。
安裝完JDK後,從 http://activemq.apache.org/download.html下載MQ的最新版本,本教程使用版本爲5.4.3。或者點擊連接下載:
解壓後,可以看到MQ目錄下有以下文件和目錄
activemq-all-5.4.3jar:所有MQ JAR包的集合,用於用戶系統調用
bin:其中包含MQ的啓動腳本
conf:包含MQ的所有配置文件
data:日誌文件及持久性消息數據
example:MQ的示例
lib:MQ運行所需的所有Lib
webapps:MQ的Web控制檯及一些相關的DEMO
啓動MQ:
雙擊bin目錄下的activemq.bat文件即可啓動MQ
第一個示例:
新建一個JAVA工程,引用activemq-all-5.5.0.jar,SLFAPI其及對應版本LOG4J的JAR包(懶的上網找的到附件裏下載)
Publisher.java
import java.util.Hashtable;
import java.util.Map;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQMapMessage;
public class Publisher {
protected int MAX_DELTA_PERCENT = 1;
protected Map<String, Double> LAST_PRICES = new Hashtable<String, Double>();
protected static int count = 10;
protected static int total;
protected static String brokerURL = "tcp://localhost:61616";
protected static transient ConnectionFactory factory;
protected transient Connection connection;
protected transient Session session;
protected transient MessageProducer producer;
public Publisher() throws JMSException {
factory = new ActiveMQConnectionFactory(brokerURL);
connection = factory.createConnection();
try {
connection.start();
} catch (JMSException jmse) {
connection.close();
throw jmse;
}
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(null);
}
public void close() throws JMSException {
if (connection != null) {
connection.close();
}
}
public static void main(String[] args) throws JMSException {
Publisher publisher = new Publisher();
while (total < 1000) {
for (int i = 0; i < count; i++) {
publisher.sendMessage(args);
}
total += count;
System.out.println("Published '" + count + "' of '" + total + "' price messages");
try {
Thread.sleep(1000);
} catch (InterruptedException x) {
}
}
publisher.close();
}
protected void sendMessage(String[] stocks) throws JMSException {
int idx = 0;
while (true) {
idx = (int)Math.round(stocks.length * Math.random());
if (idx < stocks.length) {
break;
}
}
String stock = stocks[idx];
Destination destination = session.createTopic("STOCKS." + stock);
Message message = createStockMessage(stock, session);
System.out.println("Sending: " + ((ActiveMQMapMessage)message).getContentMap() + " on destination: " + destination);
producer.send(destination, message);
}
protected Message createStockMessage(String stock, Session session) throws JMSException {
Double value = LAST_PRICES.get(stock);
if (value == null) {
value = new Double(Math.random() * 100);
}
// lets mutate the value by some percentage
double oldPrice = value.doubleValue();
value = new Double(mutatePrice(oldPrice));
LAST_PRICES.put(stock, value);
double price = value.doubleValue();
double offer = price * 1.001;
boolean up = (price > oldPrice);
MapMessage message = session.createMapMessage();
message.setString("stock", stock);
message.setDouble("price", price);
message.setDouble("offer", offer);
message.setBoolean("up", up);
return message;
}
protected double mutatePrice(double price) {
double percentChange = (2 * Math.random() * MAX_DELTA_PERCENT) - MAX_DELTA_PERCENT;
return price * (100 + percentChange) / 100;
}
}
Consumer.java
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Consumer {
private static String brokerURL = "tcp://localhost:61616";
private static transient ConnectionFactory factory;
private transient Connection connection;
private transient Session session;
public Consumer() throws JMSException {
factory = new ActiveMQConnectionFactory(brokerURL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
public void close() throws JMSException {
if (connection != null) {
connection.close();
}
}
public static void main(String[] args) throws JMSException {
Consumer consumer = new Consumer();
for (String stock : args) {
Destination destination = consumer.getSession().createTopic("STOCKS." + stock);
MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);
messageConsumer.setMessageListener(new Listener());
}
}
public Session getSession() {
return session;
}
}
Listener.java
import java.text.DecimalFormat;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
public class Listener implements MessageListener {
public void onMessage(Message message) {
try {
MapMessage map = (MapMessage)message;
String stock = map.getString("stock");
double price = map.getDouble("price");
double offer = map.getDouble("offer");
boolean up = map.getBoolean("up");
DecimalFormat df = new DecimalFormat( "#,###,###,##0.00" );
System.out.println(stock + "\t" + df.format(price) + "\t" + df.format(offer) + "\t" + (up?"up":"down"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
先運行Consumer.java, 輸入參數ORCL,然後運行Publisher.java,輸入參數ORCL,
就可以看到Publisher在發送消息,Consumer在接收消息了。
eclipse帶參數運行:
好了,MQ的安裝與第一個示例程序的介紹就到此爲止了。