ActiveMQ入门教程

1.下载ActiveMQ
去官方网站下载:http://activemq.apache.org/

2.运行ActiveMQ
解压缩apache-activemq-5.5.1-bin.zip,然后双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。

启动ActiveMQ以后,登陆:http://localhost:8161/admin/。
ActiveMQ入门教程_第1张图片

3.创建Eclipse项目并运行

ActiveMQ入门教程_第2张图片
Producter类

package com.hbk.test;

import java.util.concurrent.atomic.AtomicInteger;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Producter {
    // ActiveMq 的默认用户名
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    // ActiveMq 的默认登录密码
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    // ActiveMQ 的链接地址
    private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;

    AtomicInteger count = new AtomicInteger(0);
    // 链接工厂
    ConnectionFactory connectionFactory;
    // 链接对象
    Connection connection;
    // 事务管理
    Session session;
    ThreadLocal threadLocal = new ThreadLocal();

    public void init() {
        try {
            // 创建一个链接工厂
            connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEN_URL);
            // 从工厂中创建一个链接
            connection = connectionFactory.createConnection();
            // 开启链接
            connection.start();
            // 创建一个事务(这里通过参数可以设置事务的级别)
            session = connection.createSession(true, Session.SESSION_TRANSACTED);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public void sendMessage(String disname) {
        try {
            // 创建一个消息队列
            Queue queue = session.createQueue(disname);
            // 消息生成者
            MessageProducer messageProducer = null;
            if (threadLocal.get() != null) {
                messageProducer = threadLocal.get();
            } else {
                messageProducer = session.createProducer(queue);
                threadLocal.set(messageProducer);
            }
            while (true) {
                Thread.sleep(1000);
                int num = count.getAndIncrement();
                // 创建一条消息
                TextMessage msg = session.createTextMessage(Thread.currentThread().getName() + "productor:我是大帅哥,我现在正在生产东西!,count:" + num);
                System.out.println(Thread.currentThread().getName() + "productor:我是大帅哥,我现在正在生产东西!,count:" + num);
                // 发送消息
                messageProducer.send(msg);
                // 提交事务
                session.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Consumer类

package com.hbk.test;

import java.util.concurrent.atomic.AtomicInteger;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Consumer {
    // ActiveMq 的默认用户名
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    // ActiveMq 的默认登录密码
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    // ActiveMQ 的链接地址
    private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;
    AtomicInteger count = new AtomicInteger(0);
    // 链接工厂
    ConnectionFactory connectionFactory;
    // 链接对象
    Connection connection;
    // 事务管理
    Session session;
    ThreadLocal threadLocal = new ThreadLocal();

    public void init() {
        try {
            // 创建一个链接工厂
            connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEN_URL);
            // 从工厂中创建一个链接
            connection = connectionFactory.createConnection();
            // 开启链接
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public void getMessage(String disname) {
        try {
            Queue queue = session.createQueue(disname);
            MessageConsumer consumer = null;
            if (threadLocal.get() != null) {
                consumer = threadLocal.get();
            } else {
                consumer = session.createConsumer(queue);
                threadLocal.set(consumer);
            }
            while (true) {
                Thread.sleep(1000);
                TextMessage msg = (TextMessage) consumer.receive();
                if (msg != null) {
                    msg.acknowledge();
                    System.out.println(Thread.currentThread().getName() + ": Consumer:我是消费者,我正在消费Msg" + msg.getText() + "--->" + count.getAndIncrement());
                } else {
                    break;
                }
            }

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

测试生产者生产消息

package com.hbk.test;

public class TestProcucter {
    public static void main(String[] args) {
        Producter producter = new Producter();
        producter.init();
        TestProcucter testMQ = new TestProcucter();
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Thread 1
        new Thread(testMQ.new ProductorMq(producter)).start();
        // Thread 2
        new Thread(testMQ.new ProductorMq(producter)).start();
        // Thread 3
        new Thread(testMQ.new ProductorMq(producter)).start();
        // Thread 4
        new Thread(testMQ.new ProductorMq(producter)).start();
        // Thread 5
        new Thread(testMQ.new ProductorMq(producter)).start();
    }

    private class ProductorMq implements Runnable {
        Producter producter;

        public ProductorMq(Producter producter) {
            this.producter = producter;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    producter.sendMessage("Jaycekon-MQ");
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

执行结果如下:

ActiveMQ入门教程_第3张图片

查看后台:
ActiveMQ入门教程_第4张图片

测试消费者
ActiveMQ入门教程_第5张图片

查看后台:
ActiveMQ入门教程_第6张图片

你可能感兴趣的:(activemq)