ActiveMq入门实例

下载ActiveMq:

ActiveMQ官网下载地址:http://activemq.apache.org/download.html

需要注意的是ActiveMq的版本要支持自己的jdk版本:

MQ版本号

 Build-Jdk依赖JDK
apache-activemq-5.0.01.5.0_121.5+
apache-activemq-5.1.01.5.0_121.5+
apache-activemq-5.2.01.5.0_151.5+
apache-activemq-5.3.01.5.0_171.5+
apache-activemq-5.4.01.5.0_191.5+
apache-activemq-5.5.01.6.0_231.6+
apache-activemq-5.6.01.6.0_261.6+
apache-activemq-5.7.01.6.0_331.6+
apache-activemq-5.8.01.6.0_371.6+
apache-activemq-5.9.01.6.0_511.6+
apache-activemq-5.10.01.7.0_12-ea1.7+
apache-activemq-5.11.01.7.0_601.7+
apache-activemq-5.12.01.7.0_801.7+
apache-activemq-5.13.01.7.0_801.7+
apache-activemq-5.14.01.7.0_801.7+
apache-activemq-5.15.01.8.0_1121.8+

下载完了解压出来


在bin下选择对应自己电脑的操作系统,点击activemq.bat运行程序;

启动ActiveMQ以后,登陆:http://localhost:8161/admin/

输入账号密码,默认都是admin,在Queues里面能看到队列情况;

创建Eclipse项目,导入我们需要的jar包,我这里为了省事直接导入了acvicema-all-5.6.0这个jar包;

创建消息发送端:

public class Sender {
    public static void main(String[] args) {
        // ConnectionFactory :连接工厂,JMS 用它创建连接
        ConnectionFactory connectionFactory;
        // Connection :JMS 客户端到JMS Provider 的连接
        Connection connection = null;
        // Session: 一个发送或接收消息的线程
        Session session;
        // Destination :消息的目的地;消息发送给谁.
        Destination destination;
        // MessageProducer:消息发送者
        MessageProducer producer;
        // 构造ConnectionFactory实例对象
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                ActiveMQConnection.DEFAULT_BROKER_URL
              );
        try {
            // 构造从工厂得到连接对象
            connection = connectionFactory.createConnection();
            // 启动
            connection.start();
            // 获取操作连接
            session = connection.createSession(Boolean.TRUE,
                    Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("TestQueue");
            // 得到消息发送者
            producer = session.createProducer(destination);
            // 设置不持久化
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // 发送消息
            producer.send(session.createTextMessage("今天是星期五"));
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection)
                    connection.close();
            } catch (Throwable ignore) {
            }
        }
    }

}

创建消息接收端:

public class Receiver {  
    public static void main(String[] args) {  
        // ConnectionFactory :连接工厂,JMS 用它创建连接  
        ConnectionFactory connectionFactory;  
        // Connection :JMS 客户端到JMS Provider 的连接  
        Connection connection = null;  
        // Session: 一个发送或接收消息的线程  
        Session session;  
        // Destination :消息的目的地;消息发送给谁.  
        Destination destination;  
        // 消费者,消息接收者  
        MessageConsumer consumer;  
        connectionFactory = new ActiveMQConnectionFactory(  
                ActiveMQConnection.DEFAULT_USER,  
                ActiveMQConnection.DEFAULT_PASSWORD,  
                ActiveMQConnection.DEFAULT_BROKER_URL);  
        try {  
            // 构造从工厂得到连接对象  
            connection = connectionFactory.createConnection();  
            // 启动  
            connection.start();  
            // 获取操作连接  
            session = connection.createSession(Boolean.FALSE,  
                    Session.AUTO_ACKNOWLEDGE);  
            //设置从哪里获取消息
            destination = session.createQueue("TestQueue");
            //创建消息接受者
            consumer = session.createConsumer(destination);  
            TextMessage message =  (TextMessage)consumer.receive();
            if(message!=null){
                System.out.println("收到消息:"+message.getText());
            }
            
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (null != connection)  
                    connection.close();  
            } catch (Throwable ignore) {  
            }  
        }  
    }  

先运行发送端,再运行接收端:

这样就完成了一个最基本的一个例子,仅供参考~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值