search
HomeWeChat AppletMini Program DevelopmentHow to use ActiveMQ to test applet

Introduction Active MQ is a message queue manager, middleware used for communication. Now let me introduce to you how to use ActiveMQ to test the applet.

First you need to download ActiveMQ and modify the memory

Modify activemq.bat in the bin directory
Add

set ACTIVEMQ_OPTS=-Xms1G -Xmx1G

in the last line After saving, execute the bat file
New maven project
Add dependencies to the pom file

<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>

Sender program

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);
        }
    }
}

Receiver program

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){

            }
        }
    }
}

Related recommendations:

1ActiveMQ uses 2ActiveMQC#Client to implement 3ActiveMQC Client to implement learning through

##Yii (5)--Active Record association, yii--active

The above is the detailed content of How to use ActiveMQ to test applet. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft