ActiveMQ entry and installation (linux)

1). ActiveMQ

Apache ActiveMQ is provided by an open source messaging system completely implemented in Java, so it is a good support for J2EE proposed JMS (Java Message Service, the Java Message Service) specification. JMS is a set of Java application program interface, which provides message creation, sending, reading and other services. JMS provides a set of common application programming interfaces and grammar response, similar to the unified database access interface JDBC Java, which is a vendor-independent API, so that Java programs can communicate well with the message components from different vendors. ActiveMQ provides a number of advanced features, including load balancing and message data availability. Connecting a plurality of "master" can be dynamically agent in response to consumer demand by moving messages between nodes background. Agents can also be paired together in the main from the configuration, so that if the primary server fails, will take over from the server to ensure that clients can obtain their critical data and eliminate costly downtime.

2) .ActiveMQ benefits

  • Application of Decoupling
  • Consumers peak flow
  • Asynchronous Processing
  • Message traffic

3) .ActiveMQ mounting

  3.1 ActiveMQ's Official Web site http://activemq.apache.org/

  3.2 Download

 

 

 LINUX version 3.3 download, and upload it to a virtual machine,

 

 

 3.4 Jump to extract the complete bin directory

 

 

 3.5  ./activemq Start command

Start and maintain virtual machine firewall is turned off

 

 

 Visit http 3.6 on this machine: // virtual machine ip: The default port number 8161 

 

 

 

 

 

 

 

 

 

 

4) using JAVA operation api

4.1 introduces Maven relies

        <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
        <!--ActiveMQ依赖包-->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.9</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.xbean/xbean-spring -->
        <!--ActiveMQ和SPring整合包-->
        <dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>4.14</version>
        </dependency>

 

4.2消息的生产者

package com.yjc.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
/**
 * 消息的生产者
 * */
public class JMSProducer {
    //ActiveMQ的url
    private static final String ACTIVEMQ_URL="tcp://192.168.118.3:61616";
    //Queue的名称
    private  static final  String QUEUE_NAME="queue001";

    public static void main(String[] args) throws JMSException {
        //1.创建ActiveMQ的连接工程,给定ActiveMQ的url,使用默认账号和密码
        ActiveMQConnectionFactory activeFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2.创建active的连接对象,抛出或捕获异常,并启动connection
        Connection connection = activeFactory.createConnection();
        connection.start();
        //3.创建会话对象,两个参数前者为事务,后者为签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4.创建目的地,目的地的类型为队列(Queue),参数为队列的名称
        //Queue和Topic都继承自Destination接口
        Queue queue = session.createQueue(QUEUE_NAME);
        //5.创建消息的生产者
        MessageProducer producer = session.createProducer(queue);
        for (int i=1;i<=50;i++){
            //6.通过session创建消息,
            TextMessage textMessage = session.createTextMessage("this is msg\t"+i);
            //7.用producer将消息发送至MQ的队列里
            producer.send(textMessage);
        }
        //8.释放资源,先开后关
        producer.close();
        session.close();
        connection.close();
        System.out.println("将消息发送至MQ队列中成功!!!");

    }
}

4.3消息的消费者

package com.yjc.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

public class JMSConsumer {
    //ActiveMQ的url
    private static final String ACTIVEMQ_URL="tcp://192.168.118.3:61616";
    //Queue的名称
    private  static final  String QUEUE_NAME="queue001";

    public static void main(String[] args) throws JMSException, IOException {
        System.out.println("----------------------11111111111----------------------------");
        //1.创建ActiveMQ的连接工程,给定ActiveMQ的url,使用默认账号和密码
        ActiveMQConnectionFactory activeFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2.创建active的连接对象,抛出或捕获异常,并启动connection
        Connection connection = activeFactory.createConnection();
        connection.start();
        //3.创建会话对象,两个参数前者为事务,后者为签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4.创建目的地,目的地的类型为队列(Queue),参数为队列的名称
        //Queue和Topic都继承自Destination接口
        Queue queue = session.createQueue(QUEUE_NAME);
       //----------------------上面的步骤和Producer的一样------------------------------------------------
        //5.创建消费者
        MessageConsumer consumer = session.createConsumer(queue);
        //6.消费消息
        /*
        receive(long timeout)方法的重载为等待时间,不传参的话就会一直等待下去
        消费的消息类型需要和生产出的消息类型保持一致

       while(true){
            TextMessage textMessage = (TextMessage) consumer.receive(3000);
            if (textMessage!=null) {
                System.out.println("-----消费信息"+textMessage.getText());
            }else{
                //代表所有的信息都以及被消费完了
                break;
            }
        }*/
       //6.2异步非阻塞方式(监听消息)
       consumer.setMessageListener(new MessageListener(){
           @Override
           public void onMessage(Message message) {

               if (message!=null&&message instanceof TextMessage) {
                   TextMessage message1 =(TextMessage)message;
                   try {
                       String text = message1.getText();
                       System.out.println(text);
                   } catch (JMSException e) {
                       e.printStackTrace();
                   }

               }
           }
       });
        System.in.read();
        //7.释放资源先开后关
        consumer.close();
        session.close();
        connection.close();
        System.out.println("消费完毕!!!");
    }
}

5).思考?

当有多个消息的消费者正在进行消息的监听,此时消息的生产者生产了n条信息,这些消息会怎样被消费者消费??

第一种情况:被最先启动的消费者全部消费

第二种情况:所有的消费者都可以消费所有的消息

第三种情况:按照顺序来进行分配消息

接下来我们测试一下,

第一步:按照先后顺序启动三个消费者并同时进行监听消息

 

 

 

 

 

 

此时ActiveMQ的管理中心如下图所示,只有三个消费者在等待消息(监听),而没有一条消息

 

 

 

 

 第二步:让生产者生产出50条消息

 

 当生产者将消息放入到消息队列当中之后,之前创建的三个消费者监听到了有消息的存在,消费者要对消息进行消费,运行结果如下

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/yjc1605961523/p/11977883.html