java之消息队列ActiveMQ实践

原创论文:https://www.cnblogs.com/goujh/p/8510239.html

1、在安装ActiveMQ

http://activemq.apache.org/activemq-5158-release.html

2、解压启动服务

tar -zxvf apache-activemq-5.15.8-bin.tar.gz
进入目录,运行./bin/activemq start

3、网页查看

网址:http://139.199.64.189:8161/
点击:
Manage ActiveMQ broker
输入默认用户和密码都为:admin

4、创建maven工程,在pom.xml文件中添加

<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-core -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
            <version>5.7.0</version>
        </dependency>

5、创建JMSConsumer.java文件,这是一个消费者

package com.activemq.demo.method1;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSConsumer {
    //默认连接用户名
    private static final String USERNAME = "admin";//ActiveMQConnection.DEFAULT_USER;
    //默认连接密码
    private static final String PASSWORD = "admin";//ActiveMQConnection.DEFAULT_PASSWORD;
    //默认连接地址,默认端口为61616
    private static final String BROKERURL = "tcp://ip:61616";//ActiveMQConnection.DEFAULT_BROKER_URL;
    public static void main(String[] args) {
        //连接工厂
        ConnectionFactory connectionFactory;
        //连接
        Connection connection = null;
        //会话,接收或者发送消息的线程
        Session session;
        //消息目的地
        Destination destination;
        //消息的消费者
        MessageConsumer messageConsumer;
        //实例化连接工厂
        connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKERURL);
        try {
            //通过工厂获取连接
            connection = connectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建会话
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //创建一个连接Hello World!的消息队列
            destination = session.createQueue("Hello World");
            //创建消息的消费者
            messageConsumer = session.createConsumer(destination);

            while(true){
                TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
                if (textMessage != null) {
                    System.err.println("收到的消息:" + textMessage.getText());
                } else {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

6、创建JMSProducer.java,这是一个生产者

package com.activemq.demo.method1;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSProducer {
    //默认连接用户名
    private static final String USERNAME = "admin";//ActiveMQConnection.DEFAULT_USER;
    //默认连接密码
    private static final String PASSWORD = "admin";//ActiveMQConnection.DEFAULT_PASSWORD;
    //默认连接地址,默认端口为61616
    private static final String BROKERURL = "tcp://ip:61616";//ActiveMQConnection.DEFAULT_BROKER_URL;
    //发送的消息数量
    private static final int SENDNUM = 10;
    public static void main(String[] args) {
        //连接工厂
        ConnectionFactory connectionFactory;
        //连接
        Connection connection = null;
        //会话,接收或者发送消息的线程
        Session session;
        //消息的目的地
        Destination destination;
        //消息生产者
        MessageProducer messageProducer;
        //实例化连接工厂
        connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKERURL);
        try {
            //通过连接工厂获取连接
            connection = connectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建session
            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            //创建一个名称为Hello World!的消息队列
            destination = session.createQueue("Hello World");
            //创建消息生产者
            messageProducer = session.createProducer(destination);
            //发送消息
            sendMessage(session,messageProducer);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if(connection != null){
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 发送消息
     * @param session
     * @param messageProducer 消息生产者
     * @throws Exception
     */
    public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
        for (int i = 0; i < JMSProducer.SENDNUM; i++) {
            //创建一条文本消息
            TextMessage message = session.createTextMessage("activemq 发送消息:" + i);
            System.err.println("发送消息:activemq 发送消息:" + i);
            //通过消息生产者发出消息
            messageProducer.send(message);
        }
    }
}

7、运行两个实例,可在web端查看

猜你喜欢

转载自www.cnblogs.com/ywjfx/p/10439039.html