zoukankan      html  css  js  c++  java
  • ActiveMQ(二)——ActiveMQ的安装和基本使用

    一:安装

     2.启动之后成功

    二、创建实例测试ActiveMQ

    配置Maven所需的依赖

    <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.9.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.xbean</groupId>
    <artifactId>xbean-spring</artifactId>
    <version>3.16</version>
    </dependency>
    

    消息发送

    public class QueueSender {
    public static void main(String[] args) throws Exception{
        //连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = connectionFactory.createConnection();
        connection.start();
    
        //带事务的session
        Session session = connection.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();
        session.close();
        connection.close();
    }
    }

     消息接收

    public class QueueReceive {
    public static void main(String[] args) throws Exception{
        //连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = connectionFactory.createConnection();
        connection.start();
    
        //带事务的session,并且自动签收
        final Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("my-queue");
        MessageConsumer consumer = session.createConsumer(destination);
    
        int i = 0;
        while (i<3){
            i++;
            TextMessage message = (TextMessage) consumer.receive();
            session.commit();
            System.out.println("收到的消息是:"+message.getText());
        }
        session.close();
        connection.close();
    }
    }
    
  • 相关阅读:
    普通类型(Trivial Type)和标准布局类型(Standard-layout Type)以及POD类型
    设计模式
    网络相关的学习和命令总结
    sheel命令学习和工作总结。
    Makefile的学习
    [UI基础][实现]九宫格之应用程序管理
    [嵌入式][分享][交流]发布一个消息地图的模块
    [UI基础][不会说话的汤姆猫]
    [UI基础][QQ登陆界面]
    volatile的陷阱
  • 原文地址:https://www.cnblogs.com/cainame/p/11504774.html
Copyright © 2011-2022 走看看