消息队列-ActiveMQ P2P模式

1.导依赖包

<dependency>
  <groupId>org.apache.activemq</groupId>
   <artifactId>activemq-all</artifactId>
   <version>5.15.10</version>
</dependency>

2.消息生产者

public class Sender {

    public static void main(String[] args) throws JMSException, InterruptedException {
        // 1. 建立工厂对象
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_BROKER_URL,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
        //2. 从工厂里获取一个连接
        Connection connection = factory.createConnection();
        connection.start();
        //3. 从连接中获取Session(会话)
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4. 从会话中获取目的地(Destination),消费者会从这个目的地取消息
        Queue queue = session.createQueue("lance");
        //5. 从会话中创建消息提供者
        MessageProducer producer = session.createProducer(queue);
        int i = 1;
        try {
            while (true) {
                //从会话中创建文本消息(也可以创建其它类型的消息体)
                TextMessage textMessage = session.createTextMessage("hello" + i);
                Thread.sleep(1000);
                // 通过消息提供者发送消息到ActiveMQ
                producer.send(textMessage);
                i++;
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            connection.close();
        }
        System.out.println("exit");

    }
}

3.消息消费者

public class Recviver {

    public static void main(String[] args) throws JMSException {
        // 1. 建立工厂对象
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_BROKER_URL,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
        //2. 从工厂里获取一个连接
        Connection connection = factory.createConnection();
        connection.start();
        //3. 从连接中获取Session(会话)
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4. 从会话中获取目的地(Destination),消费者会从这个目的地取消息
        Destination destination = session.createQueue("lance");
        //5. 从会话中创建消息消费者
        MessageConsumer consumer = session.createConsumer(destination);

        //6. 持续的消费
        try {
            while (true) {
                TextMessage textMessage = (TextMessage) consumer.receive();
                System.out.println("消费:" + textMessage.getText());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            connection.close();
        }


    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值