I. Opening Speech
Following the previous article on sending and receiving messages using jms in weblogic, this article uses ActiveMQ, an open source component of apache, to discuss the topic of JMS. This article is just an example of activating ActiveMQ, hope to help you.
Ii. ActiveMQ
1. ActiveMQ introduction:
ActiveMQ is an open-source message bus with powerful Apache capabilities. It fully supports the implementation of JMS1.1 and JavaEE1.4 standard JMS providers.
2. ActiveMQ features:
1) Write clients in multiple languages and protocols. Languages: Java, C, C ++, C #, Ruby, Perl, Python, and PHP. Application Protocol: OpenWire, Stomp REST, WS Notification, XMPP, AMQP
2) fully supports JMS1.1 and J2EE 1.4 specifications (persistence, XA messages, transactions)
3) For Spring support, ActiveMQ can be easily embedded into the system using Spring, and also support Spring features.
4.) passed the tests on common J2EE servers (such as Geronimo, JBoss 4, GlassFish, and WebLogic), and configured by JCA 1.5 resourceadaptors,
ActiveMQ can be automatically deployed on any commercial server compatible with J2EE1.4.
5) supports multiple transfer protocols: in-VM, TCP, SSL, NIO, UDP, JGroups, JXTA
6) supports high-speed message persistence through JDBC and journal
7.) The design ensures high-performance clusters, client-server, and point-to-point
8.) supports Ajax
9) support integration with Axis
10.) You can easily call the embedded JMS provider for testing.
3. Environment preparation:
1) download ActiveMQ:
Http://activemq.apache.org/download.html, I downloaded apache-activemq-5.2.0
2) Run ActiveMQ server
Decompress the downloaded file and double-click bin/activemq. bat to start the server. ActiveMQ has built-in jetty server. The TCP connection port is 61616 by default.
ActiveMQ provides an admin application for ActiveMQ monitoring: http: // 127.0.0.1: 8161/admin
3) create a Java project in Eclipse and import the activemq-all-5.2.0.jar package
4) create two Java classes: Message producer MsgSender and message consumer MsgReceiver.
4. Code testing (P2P ):
1) Message Producer: MsgSender
/** * Message Provider */public class MsgSender {// ConnectionFactory: use to create JMS connectionprivate static ConnectionFactory connectionFactory;// Connection: connect message provider and JMS serverprivate static Connection connection;// Session: a message send or receive threadprivate static Session session;// Destination: use to sign the message typeprivate static Destination destination;// MessageProducer:senderprivate static MessageProducer messageProducer;/** * init the JMS object */public static void init() throws Exception {// use ActiveMQ to to create connection factory.connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");// get the connection from connection factoryconnection = connectionFactory.createConnection();session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);destination = session.createQueue("myQueue");messageProducer = session.createProducer(destination);messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);connection.start();}/** * send activeMq message */public static void sendMessage() throws Exception {for (int i = 1; i <= 5; i++) {TextMessage message = session.createTextMessage("ActiveMq message " + i);System.out.println("send:" + "ActiveMq message " + i);messageProducer.send(message);}session.commit();}/** * release resource */public static void release() throws Exception {messageProducer.close();session.close();connection.close();}/** * main method */public static void main(String[] args) throws Exception {init();sendMessage();release();}}
2) Message consumer: MsgReceiver
/** * Message Consumer */public class MsgReceiver {// ConnectionFactory: use to create JMS connectionprivate static ConnectionFactory connectionFactory;// Connection: connect message provider and JMS serverprivate static Connection connection;// Session: a message send or receive threadprivate static Session session;// use to sign the message typeprivate static Destination destination;// MessageConsumer: receiverprivate static MessageConsumer messageConsumer;/** * init the JMS object */public static void init() throws Exception {// use ActiveMQ to to create connection factory.connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");// get the connection from connection factoryconnection = connectionFactory.createConnection();session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);destination = session.createQueue("myQueue");messageConsumer = session.createConsumer(destination);connection.start();}/** * receive activeMq message */public static void receiveMessage() throws Exception {while (true) {TextMessage message = (TextMessage) messageConsumer.receive();if (message != null) {System.out.println("receive: " + message.getText());} else {break;}}}/** * release resource */public static void release() throws Exception {messageConsumer.close();session.close();connection.close();}/** * main method */public static void main(String[] args) throws Exception {init();receiveMessage();release();}}