文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>SampleJMS_ActiveMQ

SampleJMS_ActiveMQ

时间:2010-08-27  来源:btpka3

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

参考来源:http://guoliangqi.javaeye.com/blog/634462
   
文件: 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) {
            }
        }
    }
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载