ActiveMQ Installation and Use

1. Installation

Official website: http://activemq.apache.org/

Baidu Disk Address:
Link: https://pan.baidu.com/s/1A55X-vchhsaxaoaIXDUw8g 
Extraction Code: qby6
 After copying this content, open Baidu Netdisk Mobile App, it is more convenient to operate oh

1.1 Download

Enter the official website and slide down to this location

Click to download the latest version of windows

1.2 Run

1.2.1 Unzip and Start

Put the downloaded compressed file in the specified directory according to your needs, and then unzip it.
Unzip and start the activemq.bat file in the apache-activemq-5.15.12binwin64 directory

1.2.2 Logon

Access: http://localhost:8161
 Click on Manage ActiveMQ broker and you can see it by clicking on the logo below

Account and password are admin
http://localhost:8161/admin/

Log in successfully, see the following page

1.3 A little introduction, followed by more details

ActiveMQ uses standard producer and consumer models

1. Queue queue (P2P message model), where the producer produces a message and only one consumer can consume it.If you send messages to your WeChat friends.
2. Topic Theme/Broadcast (Pub/Sub message model), where the producer produces a message that can be consumed by multiple consumers.Such as WeChat Public Number to send messages to fans.

Correspondence between JMS and ActiveMQ | JMS Message Model | P2P Mode | pub/sub mode | | ------------ | ------------ | ------------ | | ActiveMQ Message | Queue Queue | Topic Queue | |Features|One-to-one, send by one person, allow only one person to receive|One-to-many, send by one person, allow more than one person to receive|

Sender: Producer
 Receiver: Consumer

2. Use Java to manipulate ActiveMQ

2.1 Create a generic maven project

Simple cut-off of the diagram at creation

2.1.1 Add a pom dependency

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

If your warehouse is not dependent, you will download it after importing, just wait a moment

2.1.2 Writing producers

Write a test class using the JMS native API, and the small message middleware writes messages as follows:

1. Create a connection factory
 2. Get connections from the connection factory
 3. Start the connection
 4. Get Sessions
 5. Create Queue Queue
 6. Create producers
 7. Create a message
 8. Send a message
 9. Submit Request
 10. Close various resources

2.1.2.1 Create package com.manlu.queue, create class ActiveMQProducter

package com.manlu.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;

/**
 * @author Roaming h
 * Generator sends message
 */
public class ActiveMQProducter {
    public static void main(String[] args) throws Exception {
        // Connect Factory
        // Use default username, password, path
        // Because: underlying implementation: final String default URL = "tcp://"+DEFAULT_BROKER_HOST+ ":" + DEFAULT_BROKER_PORT;
        // So: path tcp://host:61616
        // 1. Create a connection factory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        // 2. Create a connection
        Connection connection = connectionFactory.createConnection();
        // 3. Open Connection
        connection.start();
        // 4. Create Sessions
        //First parameter: whether to open a transaction
        //Second parameter: whether the message is automatically acknowledged
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        //Create Queue
        Queue queue = session.createQueue("hello");
        // 5. Create producers
        MessageProducer producer = session.createProducer(queue);
        // 6. Create a message
        TextMessage message = session.createTextMessage("hi i am manlu");
        // 7. Send a message
        producer.send(message);
        // 8. Close resources
        session.commit();
        producer.close();
        session.close();
        connection.close();
        System.out.println("Message Generation Successful");
    }
}

2.1.2.2 View console, message sent successfully

2.1.2.3 Check to see if the message was sent successfully (access the site logged in at installation time)

Default tcp connection activeMQ port 61616!!

2.1.3 Writing Consumers

Write test classes using JMS native API s to consume messages to message middleware development steps:

1. Create Link Factory
 2. Create links
 3. Start Link
 4. Get Sessions
 5. Create a queue
 6. Create consumers
 7. Consumer News
 8. Submit
 9. Close resources

2.1.3.1 Create an ActiveMQConsumer class that uses MessageConsumer to complete consumption

package com.manlu.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;

/**
 * @author manlu
 * Use MessageConsumer to Complete Consumption
 */
public class ActiveMQConsumer {
    public static void main(String[] args) throws Exception {
        // 1. Create a connection factory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        // 2. Create a connection
        Connection connection = connectionFactory.createConnection();
        // 3. Open Connection
        connection.start();
        // 4. Create Sessions
        /*
         *  First parameter, whether to use transactions
         * If true, session.commit() must be used after operating on message queues;
         * If false is set, session.commit() is not used after the message queue is operated on;
         */
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        // 5. Create a queue
        Queue queue = session.createQueue("hello");
        // 6. Create consumers
        MessageConsumer consumer = session.createConsumer(queue);
        while (true){
            //Failure time, if no new message is received within 10 seconds, indicating that no message exists, you can exit the current cycle
            TextMessage message = (TextMessage) consumer.receive(10000);
            if (message!=null) {
                System.out.println(message.getText());
            }else{
                break;
            }
        }
        // 7. Close the connection
        session.commit();
        session.close();
        connection.close();
        System.out.println("End of consumption");
    }
}

2.1.3.2 View the console and you can see that messages have been consumed

2.1.3.3 View page effects (visit the site you logged in at installation time)

  • Before consumption

  • After consumption

2.1.4 Listener consumption messages

package com.manlu.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;

/**
 * @author Roaming
 * Listener consumption messages
 * Repeat the test generation and consumption process.Implement a system that generates and consumes
 */
public class ActiveMQMonitorConsumer {
    public static void main(String[] args) throws Exception {
        // 1. Create a connection factory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        // 2. Create a connection
        Connection connection = connectionFactory.createConnection();
        // 3. Open Connection
        connection.start();
        // 4. Create Sessions
        /*
         *  First parameter, whether to use transactions
         * If true, session.commit() must be used after operating on message queues;
         * If false is set, session.commit() is not used after the message queue is operated on;
         */
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        // 5. Create a queue
        Queue queue = session.createQueue("hello");
        // 6. Create consumers
        MessageConsumer messageConsumer = session.createConsumer(queue);
        messageConsumer.setMessageListener(new MessageListener() {
            //Automatically call onMessage every time a message is accepted
            @Override
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println(textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        //We don't let the program end because if it does, the listening will end
        while (true){
            //Purpose: Do not let the program die
        }
    }
}

2.2 Multi-consumption-queue

The multi-consumer model in the P2P message model draws the following conclusions:
	-A message can only be consumed by one consumer, not re-consumed
	-Share messages among multiple consumers (load balancing strategy)
	-When a consumer is consuming a message, mq must wait until it receives a successful response before distributing the next message

Note: When testing, it is important to start the consumer before the producer

2.3 Topic

Topic: Theme mode, broadcast mode, pus/sub mode

	// 1. Create a connection factory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        // 2. Create a connection
        Connection connection = connectionFactory.createConnection();
        // 3. Open Connection
        connection.start();
        // 4. Create Sessions
        //First parameter: whether to open a transaction
        //Second parameter: whether the message is automatically acknowledged
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
	// 5. Create Topic Theme Mode
	Topic topic = session.createTopic("manlu123");
	// 6. Create producers
	MessageProducer producer = session.createProducer(topic);

2.4 Multi-consumption Mode-topic

Comparison of 2.4.1 queue and top modes

Same:
  1. There is only one producer
  2. You can have multiple consumers
 Difference:
  1. queue queue mode, a message can only be consumed by another consumer, not repeated consumption
  	When consumers consume a message, they must get a receipt that the message was successfully consumed before they can distribute the next message
	After queue joins the queue, no matter how long it waits, messages are always waiting for consumers to process them
  2. topic broadcast mode, where a message can be consumed by multiple consumers
  	It doesn't matter whether the message is consumed successfully or not
	topic asks for the same time. I'm just hair on my head and you're just closing

Keywords: Programming Session Apache SpringBoot Mobile

Added by jmcc on Fri, 27 Mar 2020 07:57:54 +0200