activeMQ入门demo

首先从官网下载http://activemq.apache.org/download-archives.html
解压后执行bin下面的activemq.bat即可启动
新建maven项目,导入jar包

<dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-client</artifactId>
            <version>5.14.5</version>
        </dependency>

activemq主要有以下几个成员:

  1. ConnectionFactory: 连接工厂,封装好的连接池
  2. Connection: 连接
  3. Session:
  4. Destination:消息目的地
  5. MessageProduce:消息产生者
  6. MessageConsumer:消费者

上代码,以下是生产者:

public class Sender {

    private final static int num = 5;

    public static void main(String[] args) {
        ConnectionFactory connectionFactory;// 连接工厂
        Connection connection = null;// 连接
        Session session;// 消息进程
        Destination destination;// 消息目的地
        MessageProducer produce;// 消息发送者
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");// 建立连接工厂
        try {
            connection = connectionFactory.createConnection();// 从连接工厂获取连接
            connection.start();//
            /*AUTO_ACKNOWLEDGE:当客户端从 receive 或 onMessage成功返回时,Session 自动签收客户端的这条消息的收条
             *CLIENT_ACKNOWLEDGE:需要调用acknowledge方法来接收 */
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("hello");// 设置消息队列,发送者跟消费者要相同才能相互传递消息
            produce = session.createProducer(destination);
            produce.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // 发送业务消息
            sendMessage(session, produce, "test");
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection) {
                    connection.close();
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }

        }
    }

    private static void sendMessage(Session session, MessageProducer produce, String name) throws JMSException {
        TextMessage message = session.createTextMessage("i am message number" + name);
        produce.send(message);
    }

}

消费者:

public class Receiver {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory;
        Connection connection = null;
        Session session;
        Destination destination;
        MessageConsumer consumer;//消费者
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//设置为true即消费消息后不清除
            destination = session.createQueue("hello");
            consumer = session.createConsumer(destination);
            while (true) {
                TextMessage message = (TextMessage) consumer.receive(30*1000);//设置消费者消费时间
                if (null != message) {
//                  message.acknowledge();
                    System.out.println("shoudao "+message.getText());
                } else {
                    break;
                }
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection) {
                    connection.close();
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值