Chinaunix首页 | 论坛 | 博客
  • 博客访问: 530227
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2010-08-27 11:41:38

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) {
            }
        }
    }
}


阅读(1113) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-08-29 08:20:36

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com