You are on page 1of 13

ACTIVE MQ

Mc lc: I. Tm hiu v MOM v JMS .2 1. Message_Oriented Middleware (MOM) ...2 2. Java Message Service (JMS) .3 Apache ActiveMQ ..4 1. Gii thiu ActiveMQ .4 2. Cc im c trng ca ActiveMQ 4 3. V sao nn s dng ActiveMQ ..5 4. Running ActiveMQ ...7 Demo ActiveMQ .7 1. Giao thc kt ni v phng thc lu tr .7 2. Giao thc kt ni FailOver .8 3. Cu hnh activeMQ JDBC 10 4. Simple Test ..11

II.

III.

I.

Tm hiu v Message-Oriented Middleware v Java Message Service 1. Message_Oriented Middleware (MOM) MOM l mt khi nim rt quan trong cc h thng my tinh phn tn .MOM c m t nh mt dng phn mm giao tip mt cch ng tin cy, c kh nng m rng v an ton gia cc ng dng phn tn hoc gia cc h thng. Chng cho php cc giao tip ng dng - ng dng s dng cc API c a ra bi mi nh cung cp v bt u gii quyt cc vn trong khng gian tin nhn doanh nghip. tng tng th ca MOM l n hot ng nh mt trung gian gn kt gia ngi gi tin nhn v ngi nhn tin nhn. Gn kt ny cung cp mt cp hon ton mi ca kt ni lng lo cho nhn tin doanh nghip. Hnh 1.1 minh ha mt cch s dng ca MOM kt ni trung gian v tin nhn khng ch gia cc ng dng v my ch m cn gia cc ng dng vi nhau.

Hnh 1.1: MOM mt mc cao, tin nhn l mt n v thng tin kinh doanh c gi i t mt ng dng khc thng qua MOM. ng dng gi v ng dng nhn thng qua mt MOM c gi l cc im n. Tin nhn c gi v cung cp cho nhng thit b thu m n kt ni hoc ng k ti tin nhn. y l c ch cho php kt ni lng lo gia ngi gi v ngi nhn m khng yu cu cho tng kt ni ti MOM cho vic gi v nhn tin nhn. Ngi gi

khng bit thng tin v ngi nhn v ngc li cn gi l c ch nhn tin khng ng b. MOM b sung nhiu tnh nng mnh m gi tin nhn doanh nghip m trc y khng th c khi cc kt ni l cht ch. Cc tnh nng nh tin nhn lin tc, giao tip mnh m trn cc kt ni chm hoc khng ng tin cy, nh tuyn thng ip phc tp, chuyn i tin nhn, Ngoi ra, nhiu MOM trn th trng hin nay cung cp h tr cho mt tp hp a dng cc giao thc kt ni. Mt s giao thc ph bin c h tr bao gm: HTTP/S, multicast, SSL, TCP/IP, UDP. Mt s nh cung cp thm ch cung cp MOM h tr nhiu loi ngn ng, tip tc h thp ro cn s dng c MOM trong nhiu loi mi trng. 2. Java Message Service (JMS) JMS ng trc cc API MOM trung tm cung cp mt API cho tin nhn doanh nghip. JMS nhm cung cp mt API chun gi v nhn tin nhn bng cch s dng ngn ng lp trnh JAVA cho cc nh cung cp trung lp. API JMS gim thiu s lng kin thc nhn tin doanh nghip v lp trnh JAVA yu cu thc hin mt ng dng nhn tin phc tp trong khi vn duy tr c tnh di ng qua vic trin khai cung cp JMS. JMS khng phi l mt MOM. N l mt API tru tng ha tng tc gia khch hang nhn tin v MOM. Hnh 1.2 cho thy mc cao m JMS cung cp mt API c s dng bi khch hng nhn tin tng tc vi cc nh cung cp JMS MOM c th l x l tng tc vi cc MOM c th. JMS API lm gim cc ro cn i vi vic to ra cc ng dng nhn tin doanh nghip.

Hnh 1.2: JMS

II.

Apache AvtiveMQ 1. Gii thiu ActiveMQ ActiveMQ l mt MOM m ngun m, tun th JMS 1.1, c cung cp bi Apache Software Foundation. ActiveMQ cung cp tnh sn sang cao, hiu sut, kh nng m rng, tin cy bo mt cho tin nhn doanh nghip. Mc tiu ca ActiveMQ l cung cp da trn tiu chun, tch hp ng dng tin nhn nh hng trn nhiu ngn ng v nn tng nht c th. ActiveMQ thc hin cc JMS c th v cung cp thm mt s tnh nng trn u cc JMS ny. Cc tnh nng b sung bao gm nhng hng mc nh qun l JMX ca cc broker, ch master/slave, tin nhn nhm, tng s th t ca thng ip, cho php ngi dung nhn tin nhn c khi kch hot ng k, x l tnh hung ngi dng c kt ni chm, ch o gim thiu s lng kt ni n cc broker, thng ip duy tr tinh vi, h tr ngi dng x l tin nhn ln, h tr chuyn i tin nhn, h tr m hnh EIP thng qua Apache Camel, nhn i hng i x l nhiu hn, 2. Cc im c trng ca ActiveMQ - Tun th JMS: - Kt ni: Active cung cp mt lot cc la chn kt ni, h tr nhiu giao thc nh HTTP/S, JGroup, JXTA, multicast, SSL, TCP, UDP, XMPP, Vic h tr nhiu giao thc khin ActiveMQ linh hot hn. Nhiu h thng hin nay ch cho php s dng mt giao thc m khng c ty chn thay i. Do , mt nn tng nhn tin h tr nhiu giao thc lm gim cc ro cn khi s dng. - Tnh duy tr v bo mt: ActiveMQ cung cp nhiu cch duy tr tin nhn v c th la chn s dng. Ngoi ra tnh bo mt trong activeMQ c th hon ton ty chnh cho kiu xc thc v y quyn tt nht cho nhu cu s dng. - Tch hp vi nhiu JAVA Containers: activeMQ cung cp kh nng d dng tch hp vi nhiu java container ph bin nh: Apache Geronimo, Apache Tomcat, JBoss, Jetty, Spring Framework, POJOs, Weblogic, Websphere - API Client: ActiveMQ cng cung cp mt API Client cho nhiu ngn ng bn cnh JAVA bao gm C/C++, .NET, Perl, Python, Ruby, iu ny m ra cho ActiveMQ c hi c s dng bn ngoi cc h thng s dng JAVA. Nhiu ngn ng khc cng c th truy nhp vo cc tnh nng v tin ch c cung cp bi activeMQ trng qua cc API Client khc nhau. Tuy nhin, activeMQ broker vn chy trong mt my o

JAVA nhng cc client c th c vit bng bt k ngn ng c h tr. - Mng li Broker: Nhiu broker c th lm vic cng nhau nh mt mng li broker cho cc mc ch m rng. - Tnh nng Broker nng cao v ty chn Clients: activeMQ cung cp nhiu tnh nng phc tp cho broker v c client kt ni vi broker. - M hnh kt hp vi Apache Camel: - Qun tr d dng v mnh m 3. V sao nn s dng ActiveMQ? Khi xem xt vic thit k cc ng dng phn phi, kt ni ca cc ng dng l rt quan trng. Kt ni dng ch s ph thuc gia cc ng dng hay h thng vi nhau. Nu mt ng dng hoc mt h thng thay i m nh hng cc ng dng hoc h thng khc th kt ni c gi l kt ni cht ch. Ngc li, nu ng dng hoc h thng b thay i m vn khng nh hng n ng dng hoc h thng khc th kt ni c gi l kt ni lng lo. Cc thit k s dng kt ni cht ch s kh duy tr hn so vi s dng kt ni lng lo. Cc kt ni lng lo c th d dng i ph vi nhng thay i khng c bo trc. Cc cng ngh COM, CORBA, DCE, EJB s dng k thut c gi l Remote Procedural Calls (RPC) c coi l mt kt ni cht ch. S dng RPC, khi mt ng dng gi mt ng dng khc, ng dng gi s b chn cho n khi ng dng nhn tr v quyn iu khin cho ng dng gi. S di y m t khi nim ny:

ng dng gi (Application One) trong s b chn cho n khi ng dng nhn (Application Two) tr v quyn iu khin. Nhiu kin trc h thng s dng RPC rt thnh cng. Tuy nhin, c rt nhiu bt li cho cng ngh ny, ph bin nht l chi ph duy tr bo dng h thng cao hn v nhng thay i nh cng nh hng n c h thng. Ta so snh vi m hnh s dng kt ni lng lo di y:

Application One to mt cuc gi gi tin nhn n MOM trong kt ni mt chiu. Sau , application Two to mt cuc gi nhn tin nhn t MOM cng trong kt ni mt chiu. Khng c bt k mt ng dng no bit thng tin v ng dng kia, thm ch c s tn ti v khng c thi gian kt ni gia cc ng dng. Theo m hnh ny, chi ph bo tr thp hn nhiu v nhng thay i trong ng dng ny c t hoc khng nh hng n ng dng khc. i vi nhng l do ny, m hnh s dng kt ni lng lo cung cp mt li th ln so vi s dng kt ni cht ch khi p dng trong m hnh ng dng phn tn. y l cch m activeMQ tip cn. ActiveMQ cung cp cc li ch ca kt ni lng lo nh ni trn. ActiveMQ thng c a vo mt kin trc gim thiu cc kt ni cht ch c in ca cc ng dng s dng RPC. Thit k ny c gi l khng ng b, khi cc cuc gi t cc ng dng khng tng tc v ph thuc ln nhau. Cc ng dng c th da vo cc thc thi ActiveMQ ca cc JMS c th v n bo m vic cung cp cc thng ip. Bi v iu ny, ngi ta thng ni cc ng dng gi tin nhn ch gi v qun. Chng gi tin nhn n activeMQ v khng quan tm n lm th no hoc khi no tin nhn c gi n cc ng dng nhn tin nhn. Cc ng dng nhn tin nhn cng khng quan tm cc tin nhn c gi ln activeMQ nh th no v khi no. y l mt li ch c bit mnh m trong mi trng khng ng nht, cho php ng dng nhn v ng dng gi khng cn thit phi vit cng mt ngn ng, thm ch giao thc gi v nhn c th khc nhau. ActiveMQ hot ng nh

mt trung gian cho php tch hp khng ng nht v tng tc mt cch khng ng b. 4. Runnig ActiveMQ - Download activeMQ ti ng dn:
http://activemq.apache.org/activemq-580-release.html

i vi HH Linux c th download bng lnh sau:


[~]# wget http://mirrors.digipower.vn/apache/activemq/apacheactivemq/5.8.0/apache-activemq-5.8.0-bin.tar.gz

Gii nn v chy activeMQ vi cu hnh mc nh:

[~]# tar xf apache-activemq-5.8.0-bin.tar.gz [~]# apache-activemq-5.8.0/bin/activemq start

Port lng nghe: 61616 Port HTTP: 8161 III. Demo ActiveMQ 1. Gii thiu giao thc kt ni v phng thc lu tr: - ActiveMQ cung cp cc phng thc kt ni sau: + Static network: Static protocol FailOver protocol + Dynamic network Multicast Protocol Discovery Protocol Zeroconf Protocol Peer Protocol Fanout Protocol Trong v d demo ta s dng giao thc kt ni FailOver. FailOver hot ng trn c ch Master/Slave v c h tr 3 phng thc lu tr chnh: Pure Master Slave: Shared File System Master Slave JDBC Master Slave 2. Gii thiu v giao thc kt ni FailOver: Giao thc kt ni failover bo m cho hot ng truyn v nhn gia 2 ng dng lin tc ngay khi c mt broker b li. ch ny, chng ta s khi ng ng thi mt s activeMQ ch Master/Slave. Ti mt thi im s c mt activeMQ c t trng thi Master, cn cc activeMQ cn li

ng vai tr Slave. Ch c activeMQ Master mi c kh nng nhn thng ip t Producer. i vi phng thc lu tr Shared File System Master Slave v JDBC Master Slave, ch c activeMQ Master mi c truy nhp vo vng lu tr. Khi activeMQ Master b li khng kt ni c vi Producer, sau mt thi gian time out s c mt activeMQ Slave chim quyn Master v thc hin giao tip vi Producer. Khi activeMQ b li c hi phc n cng khng th chim li quyn Master m s c cp quyn Slave.

Hnh 2.1: M hnh FailOver

C php cu hnh URL:


failover:(uri1,...,uriN)?transportOptions or failover:uri1,...,uriN

Transport Options:
Option Name initialReconnectDelay Default Value 10 Description How long to wait before the first reconnect attempt (in ms)

maxReconnectDelay useExponentialBackOff backOffMultiplier

30000 True 2.0

maxReconnectAttempts

startupMaxReconnectAttempts

Randomize

True

Backup

false

Timeout

-1

trackMessages maxCacheSize

false 131072

The maximum amount of time we ever wait between reconnect attempts (in ms) Should an exponential backoff be used btween reconnect attempts The exponent used in the exponential backoff attempts If not 0, then this is the maximum number of reconnect attempts before an error is sent back to the client If not 0, then this is the maximum number of reconnect attempts before an error is sent back to the client on the first attempt by the client to start a connection, once connected themaxReconnectAttempts option takes precedence. use a random algorithm to choose the the URI to use for reconnect from the list provided initialize and hold a second transport connection - to enable fast failover Enables timeout on send operations (in miliseconds) without interruption of reconnection process keep a cache of in-flight messages that will flushed to a broker on reconnect size in bytes for the cache, if trackMessages is enabled

3. Cu hnh activeMQ JDBC Ta s cu hnh activeMQ JDBC chy demo. - Cu hnh database To database cho activeMQ
mysql> CREATE DATABASE activemq;

To user v cp quyn cho user

mysql> CREATE USER 'activemq'@'%'localhost' IDENTIFIED BY 'activemq'; mysql> GRANT ALL ON activemq.* TO 'activemq'@'localhost'; mysql> exit

Cu hnh activeMQ M file cu hnh activemq.xml:

[root@dhcppc2 apache-activemq-5.8.0]# vim conf/activemq.xml

Thm cc trng sau vo file cu hnh:


<persistenceAdapter> <jdbcPersistenceAdapter dataDirectory="${activemq.base}/data" dataSource="#mysql-ds"/> </persistenceAdapter> <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/> <property name="username" value="activemq"/> <property name="password" value="activemq"/> <property name="maxActive" value="200"/> <property name="poolPreparedStatements" value="true"/> </bean>

4. Simple Test - Code gi d liu:


package ActiveMQ;

import javax.jms.Connection; import javax.jms.ConnectionFactory;

10

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 ProducerTool { private static String url = "failover://tcp://192.168.1.208:61616"; public static void sendMessage(String msg, String subject) throws JMSException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

Connection connection = connectionFactory.createConnection(); connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination destination = session.createQueue(subject);

MessageProducer producer = session.createProducer(destination);

TextMessage message = session.createTextMessage(msg);

producer.send(message);

11

connection.close(); }

public static void main(String[] args) throws JMSException { for (int i = 0; i < 10; i++) { sendMessage(i,"UpdateVoiceMail"); } } }

Code nhn d liu:

import java.util.ArrayList;

import javax.jms.*;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Consumer { private static String url = "failover://tcp://192.168.1.208:61616";

private static ArrayList<String> list = new ArrayList<String>();

public static ArrayList<String> getQueue(String subject) throws JMSException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); Connection connection = connectionFactory.createConnection();

12

connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination destination = session.createQueue(subject);

MessageConsumer consumer = session.createConsumer(destination);

for (int i = 0; i < 10; i++) { Message message = consumer.receive(10);

if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; list.add(textMessage.getText()); } } connection.close(); return list; } public static void main(String[] args) throws JMSException { list = getQueue("UpdateVoiceMail"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }

13