目录
首先需要下载ActiveMQ,并修改内存
首页 微信小程序 小程序开发 如何使用ActiveMQ测试小程序

如何使用ActiveMQ测试小程序

Sep 14, 2018 pm 03:51 PM

介绍 Active MQ是个消息队列管理器,用于通讯的中间件。现在由我来给大家介绍一下如何使用ActiveMQ测试小程序。

首先需要下载ActiveMQ,并修改内存

修改bin目录下activemq.bat
在最后一行加入

set ACTIVEMQ_OPTS=-Xms1G -Xmx1G
登录后复制

保存后执行该bat文件
新建maven项目
pom文件中加入依赖

<dependencies>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <!--我的mq版本为5.9.0-->
            <version>5.9.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-broker -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-broker</artifactId>
            <version>5.10.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-client -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-client</artifactId>
            <version>5.14.0</version>
        </dependency>
    </dependencies>
登录后复制

发送端程序

import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.
ActiveMQConnectionFactory;import javax.jms.*;public class Send {
   // private static final int SEND_NUMBER = 10;
    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;        //TextMessage message;
        //构造ConnectionFactory实例对象,此处采用ActiveMQ的实现jar
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,                "tcp://localhost:61616"
        );        try{            //构造从工厂得到连接对象
            connection = connectionFactory.createConnection();        
                //启动
            connection.start();         
              //获取操作连接
            session = connection.createSession(Boolean.TRUE,
                    Session.AUTO_ACKNOWLEDGE);          
               //获取session注意参数值test是一个服务器的queue,须在ActiveMQ的console配置
            destination = session.createQueue("test");           
             //得到发送者
            producer = session.createProducer(destination);          
              //设置不持久化,实际情况请根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);      
                  //构造消息,这里写死了,项目就是参数,或者方法获取
            sendMessage(session,producer);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {            try {                if(null != connection)
                    connection.close();
            }catch (Throwable ignore){}
        }
    }    public static void sendMessage(Session session, MessageProducer producer) throws Exception{   
         for (int i = 1; i <= 100000; i++){
            TextMessage message = session.createTextMessage("今日天气asda" + i);//发送消息到目的地
            System.out.println("发送:"+message.getText());
            producer.send(message);
        }
    }
}
登录后复制

接收端程序

import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.
ActiveMQConnectionFactory;import javax.jms.*;public class revice {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory;
        Connection connection = null;
        Session session;
        Destination destination;        //接收者-消费者
        MessageConsumer messageConsumer;
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");   
                     try{
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("test");
            messageConsumer = session.createConsumer(destination);            while(true){
                TextMessage message = (TextMessage) messageConsumer.receive(1000);               
                 if(null != message){
                    System.out.println("收到:"+message.getText());
                }else{                    break;
                }

                message.acknowledge();
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{            try{                if(null != connection){
                    connection.close();
                }
            }catch(Throwable ig){

            }
        }
    }
}
登录后复制

 相关推荐:

1ActiveMQ使用 2ActiveMQC#Client实现 3ActiveMQC++Client实现通

Yii的学习(5)--Active Record的关联,yii--active

以上是如何使用ActiveMQ测试小程序的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)