Active MQ learning Broker

I. concept

Equivalent to an ActiveMQ service instance

Broker actually starts ActiveMQ in the form of code, embeds MQ into java code, so that it can be started at any time. When it is used, it can save resources and ensure reliability.

2, Start ActiveMQ according to different configuration files

1. First copy and rename activemq.xml in the conf folder under the root directory of ActiveMQ to activemq02.xml

Command (cp activemq.xml activemq02.xml)

2. Start activemq02.xml. The default is activemq.xml

Command (. / activemq start xbean:file:/usr/local/activeMQ/apache-activemq-5.15.11/conf/activemq02.xml)

3, Embedded Broker

Use ActiveMQ Broker as an independent message server to build java applications. ActiveMQ also supports communication in virtual machine. Based on embedded broker, it can integrate other java applications seamlessly

Four, code

1. Introducing package into pom.xml

  <!--activemq-->
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-all</artifactId>
      <version>5.15.9</version>
    </dependency>
    <!--fastjson-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.9</version>
    </dependency>

2. broker code


import org.apache.activemq.broker.BrokerService;

/**
 * @ProjectName: springbootActiveMQ
 * @Package: cn.**.test
 * @Author: huat
 * @Date: 2020/1/10 16:04
 * @Version: 1.0
 */
public class EmbedBroker {
    public static void main(String[] args) throws Exception {
        //ActiveMQ also supports communication in virtual machines and embedding broker s
        BrokerService brokerService=new BrokerService();
        //Embedding activeMQ into java programs
        brokerService.setUseJmx(true);
        //Now we embed activeMQ into java program, so we use native
        brokerService.addConnector("tcp://127.0.0.1:61616");
        //Startup program
        brokerService.start();
    }

}

3. Queue producer code


import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @ProjectName: springbootActiveMQ
 * @Package: cn.**.test
 * @Author: huat
 * @Date: 2020/1/2 17:04
 * @Version: 1.0
 */
public class ActiveMQTest {
    //url path
    private static final String ACTRIVE_URL="tcp://127.0.0.1:61616";
    //Queue name
    private static final String QUEUE_NAME="queue01";

    public static void main(String[] args) {
        //1. Create connection factory
        //If the account password has not been modified, the account password is admin by default
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTRIVE_URL);

        try {
            //2. Get connection through connection factory
            Connection connection = activeMQConnectionFactory.createConnection();
            connection.start();
            //3. Create session session
            //There will be two parameters in it. The first is things and the second is sign in
            Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            //4. Create a destination (queue or topic specifically), here is to create a queue
            Queue queue=session.createQueue(QUEUE_NAME);
            //5. Create message producer, queue mode
            MessageProducer messageProducer = session.createProducer(queue);
            //6. Three messages are produced by messageProducer and sent to MQ message queue
            for (int i=0;i<3;i++){
                //7, Create message
                TextMessage textMessage = session.createTextMessage("msg----->" + i);//Create a text message

                //8. Send to mq via messageProducer
                messageProducer.send(textMessage);
                //9. Data non persistence
                messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
            }
            messageProducer.close();
            session.commit();
            session.close();
            connection.close();
            System.out.println("Message sent successfully");
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}

3. Queue consumer code

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @ProjectName: springbootActiveMQ
 * @Package: cn.**.test
 * @Author: huat
 * @Date: 2020/1/3 8:47
 * @Version: 1.0
 */
public class ActiveMQConsumer {
    //url path
    private static final String ACTRIVE_URL="tcp://127.0.0.1:61616";
    //Queue name
    private static final String QUEUE_NAME="queue01";

    public static void main(String[] args) {
        //1. Create connection factory
        //If the account password has not been modified, the account password is admin by default
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTRIVE_URL);
        //If the account password is changed
        //The first parameter is the account, the second is the password, and the third is the requested url
        //ActiveMQConnectionFactory activeMQConnectionFactory1=new ActiveMQConnectionFactory("admin","admin",ACTRIVE_URL);
        try {
            //2. Get connection through connection factory
            Connection connection = activeMQConnectionFactory.createConnection();
            connection.start();
            //3. Create session session
            //There will be two parameters in it. The first is things and the second is sign in
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //4. The name of the queue accepted here should be the same as that of the sender
            Queue queue = session.createQueue(QUEUE_NAME);
            //5. Create consumer
            MessageConsumer consumer = session.createConsumer(queue);
            //6. Consume messages by listening
            while(true){
                //The receive method called by MessageConsumer is a synchronous call, blocking the thread until the message arrives
                //Send in whatever format, and accept in whatever format
                //receive message, unlimited time
                TextMessage message=(TextMessage)consumer.receive();

                //receive message with parameters, limit time, in milliseconds
                //TextMessage message=(TextMessage)consumer.receive(4000L);

                if(null != message){
                    System.out.println("The message received is------>"+message.getText());
                }else{
                    break;
                }
            }
            //7. Closed resources
            consumer.close();
            session.close();
            connection.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

Keywords: Programming Session xml Java Apache

Added by rami103 on Fri, 10 Jan 2020 21:00:53 +0200