ActiveMQ JMS Sample 运行步骤
Step 1. 安装:下载apache-activemq-5.4.0-bin.zip,并解压
Step 2. 运行:${ActiveMQ_HOME}/bin/activemq.bat
Step 3. 创建Java工程,类库为 ${ActiveMQ_HOME}/lib/*.jar
Step 4. 编译运行 Receiver.java
Step 5. 编译运行Sender.java
|
文件: |
SampleJMS_ActiveMQ.zip |
大小: |
8KB |
下载: |
下载 | |
Receiver.java
import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.Session; import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory;
public class Receiver { public static void main(String[] args) { ConnectionFactory connFac = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
Connection conn = null;
try { conn = connFac.createConnection(); conn.start();
Session session = conn.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("myQueue");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() { public void onMessage(Message msg) { try { System.out.println("RECEIVE[1] : " + ((TextMessage) msg).getText()); } catch (JMSException e) { e.printStackTrace(); } } });
MessageConsumer consumer1 = session.createConsumer(destination); consumer1.setMessageListener(new MessageListener() { public void onMessage(Message msg) { try { System.out.println("RECEIVE[2] : " + ((TextMessage) msg).getText()); } catch (JMSException e) { e.printStackTrace(); } } }); } catch (Exception e) { e.printStackTrace(); } // 注释掉以保持监听状态 // finally { // try { // if (null != connection) // connection.close(); // } catch (Throwable ignore) { // } // } } }
|
Sender.java
/* * 摘要来源:http://guoliangqi.javaeye.com/blog/634462 */
import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory;
public class Sender {
public static void main(String[] args) {
ConnectionFactory connFac = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
Connection conn = null;
try { conn = connFac.createConnection(); conn.start();
Session session = conn.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("myQueue"); MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i = 0; i < 20; i++) { TextMessage msg = session.createTextMessage("Message_" + i); producer.send(msg); if (i % 2 == 0) { session.commit(); System.out.println("SEND[1] : [" + msg.getText() + "] SUCCESS!"); } else { session.rollback(); System.out.println("SEND[1] : [" + msg.getText() + "] FAILED!"); } Thread.sleep(500); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != conn) conn.close(); } catch (Throwable ignore) { } } } }
|
阅读(1149) | 评论(1) | 转发(0) |