ActvieMQ简单使用

一、创建工程

所需要的依赖有:

    <dependencies>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.xbean/xbean-spring -->
        <dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>4.6</version>
        </dependency>
    </dependencies>

这里推荐大家创建工程的时候就使用Maven,使用后你就知道有多好用了。

二、发送者(生产者)

这里只简单展示ActiveMQ的使用,所以使用一个简单的发送者程序,这里就直接上代码好了:

import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
 * Created with IntelliJ IDEA.
 * User: Roy
 * Date: 2017/12/6 0006
 * Time: 15:31
 * Description:
 */
public class JmsSender {
    public static void main(String args[]) throws Exception{
        Session session = null;
        Connection con = null;
        try{
            //通过工厂模式获取链接
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.28.128:61616");
            con = connectionFactory.createConnection();
            con.start();
            //创建会话(是否使用事务,消息确认机制)
            session = con.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
            //创建目的地(消息队列名称)
            Destination destination = session.createQueue("my-queue");
            //创建生产者(消息发送器)
            MessageProducer producer = session.createProducer(destination);
            //发送消息
            for(int i=0;i<3;i++){
                TextMessage message = session.createTextMessage("message--"+i);
                Thread.sleep(1000);
                producer.send(message);
            }
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
            con.close();
        }
    }
}

直接运行,如果没有报错的话,我们可以在ActiveMQ的控制台中看到刚才发的三条消息
这里写图片描述
其中看到my-queue这个队列中有三条消息,就是我们刚才发的三条。而Messages Enqueued代表在队列中,可以使用的消息数,也是三条。
那么接下来我们就来写消费者。

三、接收者(消费者)

这里也是话不多说,直接上代码:

import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
 * Created with IntelliJ IDEA.
 * User: Roy
 * Date: 2017/12/6 0006
 * Time: 15:56
 * Description:
 */
public class JmsReciver {
    public static void main(String args[]) throws Exception{
        Session session = null;
        Connection con = null;
        try{
            //通过工厂模式获取链接
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.28.128:61616");
            con = connectionFactory.createConnection();
            con.start();
            //创建会话(是否使用事务,消息确认机制)
            session = con.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
            //创建目的地(消息队列名称)
            Destination destination = session.createQueue("my-queue");
            //创建消费者(消息接收器)
            MessageConsumer consumer = session.createConsumer(destination);
            //接收消息
            for(int i=0;i<3;i++){
                TextMessage message = (TextMessage)consumer.receive();
                session.commit();
                System.out.println("接收消息:"+message.getText());
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
            con.close();
        }
    }
}

执行结果是:

接收消息:message--0
接收消息:message--1
接收消息:message--2

可以看到消息都接收到了,再去管理台看一下

这里写图片描述
发现Messages Dequeued中有了3条数据,代表已经使用了3条。

需要注意的是ActiveMQ是异步通信,发送者发送完消息后,接收者甚至可以还没启动,当它启动后再去获取消息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值