ActiveMQ单机安装和使用

来源:互联网 发布:淘宝图片上传后变大 编辑:程序博客网 时间:2024/05/16 00:35

 

ActiveMQ单机安装使用

1  概述

ActiveMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的、可扩展的、稳定的和安全的企业级消息通信。ActiveMQ使用Apache提供的授权,任何人都可以对其实现代码进行修改。

ActiveMQ的设计目标是提供标准的,面向消息的,能够跨越多语言和多系统的应用集成消息通信中间件。ActiveMQ实现了JMS标准并提供了很多附加的特性。这些附加的特性包括,JMX管理(java Management Extensions,即java管理扩展),主从管理(master/salve,这是集群模式的一种,主要体现在可靠性方面,当主中介(代理)出现故障,那么从代理会替代主代理的位置,不至于使消息系统瘫痪)、消息组通信(同一组的消息,仅会提交给一个客户进行处理)、有序消息管理(确保消息能够按照发送的次序被接受者接收)。消息优先级(优先级高的消息先被投递和处理)、订阅消息的延迟接收(订阅消息在发布时,如果订阅者没有开启连接,那么当订阅者开启连接时,消息中介将会向其提交之前的,其未处理的消息)、接收者处理过慢(可以使用动态负载平衡,将多数消息提交到处理快的接收者,这主要是对PTP消息所说)、虚拟接收者(降低与中介的连接数目)、成熟的消息持久化技术(部分消息需要持久化到数据库或文件系统中,当中介崩溃时,信息不会丢失)、支持游标操作(可以处理大消息)、支持消息的转换、通过使用Apache的Camel可以支持EIP、使用镜像队列的形式轻松的对消息队列进行监控等。

 

 

2  自定义安装

2.1  下载地址

官网下载:
http://activemq.apache.org/download-archives.html

 

2.2  安装步骤

安装步骤如下:

1、解压;

$ tar -zxvf apache-activemq-5.9.0-bin.tar.gz

 

 

2、启动:

执行/bin目录下activemq

$ cd /bin

$ ./activemq start &

运行bin目录下启动mq

注:如果是windows下,直接解压后运行bin目录下activemq.bat即可

 

 

3、查看启动后的日志:

 

如上日志表示启动成功。

 

 

访问activeMQ网页地址并登录:

 

http://192.168.xxx.xxx:8161/

 

用户/密码可以查看/conf/jetty-realm.properties

 

 

3       配置文件说明

进入到activemq_install_dir/config目录,有以下几个重要文件

(1) activemq.xml,在此文件中你可以配置activemq的很多东西,比如将消息持久化到数据库等。(2) credentials.properties,一些密码,多用于生产和消费的密码认证。(3) jetty.xml,activemq内置了jetty应用服务器。(4) jetty-realm.properties,activemq控制台登陆密码。 

 

4       测试验证

4.1     Java客户端连接

在Java项目中使用activemq

在java工程中导入ActiveMQ需要的包

需要如下包:

activemq-core.jaractiveio-core.jarkahadb.jar (ifyou wish to use persistence,如果要使用持久化需要此jar包)slf4j-api.jarJ2EE Jarsgeronimo-spec-jms.jargeronimo-spec-jta.jargeronimo-spec-j2ee-management.jar


也可以使用默认的activemq-all.jar,下载地址:

http://mvnrepository.com/artifact/org.apache.activemq/activemq-all

4.2     生产者测试

public class Producer {     private static final String BROKER_URL = "tcp://localhost:61616";     private static final Boolean NON_TRANSACTED = false;    private static final int NUM_MESSAGES_TO_SEND = 100;    private static final long DELAY = 100;     public static void main(String[] args) {        String url = BROKER_URL;        if (args.length > 0) {            url = args[0].trim();        }        ActiveMQConnectionFactoryconnectionFactory = new ActiveMQConnectionFactory("admin", "admin", url);        Connection connection = null;         try {             connection = connectionFactory.createConnection();            connection.start();             Session session =connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);            Destination destination =session.createQueue("test-queue");            MessageProducer producer =session.createProducer(destination);             for (int i = 0; i < NUM_MESSAGES_TO_SEND; i++) {                TextMessage message =session.createTextMessage("Message#" + i);                System.out.println("Sending message #" + i);                producer.send(message);                Thread.sleep(DELAY);            }             producer.close();            session.close();         } catch (Exception e) {            System.out.println("Caught exception!");        }        finally {            if (connection != null) {                try {                    connection.close();                } catch (JMSException e) {                    System.out.println("Could not close an open connection...");                }            }        }    }}


 

 

 

4.3     消费者测试

public class Consumer {     private static final String BROKER_URL = "tcp://localhost:61616";     private static final Boolean NON_TRANSACTED = false;    private static final long TIMEOUT = 20000;     public static void main(String[] args) {        String url = BROKER_URL;        if (args.length > 0) {            url = args[0].trim();        }        System.out.println("\nWaiting to receive messages... will timeout after " + TIMEOUT / 1000 +"s");        ActiveMQConnectionFactoryconnectionFactory = new ActiveMQConnectionFactory("admin", "admin", url);        Connection connection = null;         try {             connection =connectionFactory.createConnection();            connection.start();             Session session =connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);            Destination destination =session.createQueue("test-queue");            MessageConsumer consumer =session.createConsumer(destination);             int i = 0;            while (true) {                Message message =consumer.receive(TIMEOUT);                 if (message != null) {                    if (message instanceof TextMessage) {                        String text =((TextMessage) message).getText();                        System.out.println("Got " + i++ + ". message: " + text);                    }                } else {                    break;                }            }             consumer.close();            session.close();         } catch (Exception e) {            System.out.println("Caught exception!");        }        finally {            if (connection != null) {                try {                    connection.close();                } catch (JMSException e) {                    System.out.println("Could not close an open connection...");                }            }        }    }}



4.4     数据查看

登录MQ视图界面:

http://192.168.xxx.xxx:8161/


登录后查看ActiveMQ =>localhost=Queue可以查看到生产者发送的数据

如下图:

 


当消费者完成后,查看界面时,队列数据为空;

如下图:


1 0
原创粉丝点击