zoukankan      html  css  js  c++  java
  • ActiveMQ搭建

    下载

    到ActiveMQ官网,找到下载点。

    目前,

    官网为http://activemq.apache.org/

    Linux版本下载点之一为:http://apache.fayea.com/activemq/5.11.1/apache-activemq-5.11.1-bin.tar.gz

    启动

    下载到本机,并解压

    wget http://apache.fayea.com/activemq/5.11.1/apache-activemq-5.11.1-bin.tar.gz
    tar -xf ./apache-activemq-5.11.1-bin.tar.gz

    wget http://apache.fayea.com/activemq/5.11.1/apache-activemq-5.11.1-bin.tar.gz
    tar -xf ./apache-activemq-5.11.1-bin.tar.gz

    启动(当然,由于依赖于JAVA,如果你没有安装JAVA,它会提醒你的,哈哈)

    cd ./apache-activemq-5.11.1-bin/bin
    ./activemq start

    cd ./apache-activemq-5.11.1-bin/bin
    ./activemq start

    测试启动成功与否

    ActiveMQ默认监听61616端口,查此端口看看是否成功启动

    netstat -an|grep 61616

    netstat -an|grep 61616

    如果一切顺利,会看到如下日志

    [nicchagil@localhost bin]$ ./activemq start
    INFO: Loading '/home/nicchagil/app/apache-activemq-5.11.1/bin/env'
    INFO: Using java '/home/nicchagil/app/jdk1.7.0_71//bin/java'
    INFO: Starting - inspect logfiles specified in logging.properties and log4j.prop                                                                                                                                                             erties to get details
    INFO: pidfile created : '/home/nicchagil/app/apache-activemq-5.11.1/data/activem                                                                                                                                                             q.pid' (pid '4858')
    [nicchagil@localhost bin]$
    [nicchagil@localhost bin]$
    [nicchagil@localhost bin]$
    [nicchagil@localhost bin]$ netstat -an | grep 61616
    tcp        0      0 :::61616                    :::*                        LIST                                                                                                                                                             EN
    [nicchagil@localhost bin]$

    复制代码
    [nicchagil@localhost bin]$ ./activemq start
    INFO: Loading '/home/nicchagil/app/apache-activemq-5.11.1/bin/env'
    INFO: Using java '/home/nicchagil/app/jdk1.7.0_71//bin/java'
    INFO: Starting - inspect logfiles specified in logging.properties and log4j.prop                                                                                                                                                             erties to get details
    INFO: pidfile created : '/home/nicchagil/app/apache-activemq-5.11.1/data/activem                                                                                                                                                             q.pid' (pid '4858')
    [nicchagil@localhost bin]$
    [nicchagil@localhost bin]$
    [nicchagil@localhost bin]$
    [nicchagil@localhost bin]$ netstat -an | grep 61616
    tcp        0      0 :::61616                    :::*                        LIST                                                                                                                                                             EN
    [nicchagil@localhost bin]$
    复制代码

    顺便,登录下管理员页面,看看有木有问题:

    URL : http://10.0.0.109:8161/admin/

    ACC/PWD : admin/admin

    尝试基本消息功能

    接下来,用简单的点对点测试消息发送、消息接收。

    引入包:

    • activemq-client-5.11.1.jar
    • geronimo-j2ee-management_1.1_spec-1.0.1.jar
    • geronimo-jms_1.1_spec-1.1.1.jar
    • hawtbuf-1.11.jar
    • slf4j-api-1.7.10.jar

    消息发送

    package com.nicchagil.activemq.study.No001点对点;
    
    
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.DeliveryMode;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.MessageProducer;
    import javax.jms.ObjectMessage;
    import javax.jms.Session;
    
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    
    public class Sender {
        
        public static void main(String[] args) throws JMSException {
            ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://10.0.0.109:61616");
            Connection connection = factory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("TestQueue");
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            ObjectMessage message = session.createObjectMessage("hello world...");
            producer.send(message);
            session.commit();
            System.out.println("sent...");
        }
    
    }

    复制代码
    package com.nicchagil.activemq.study.No001点对点;
    
    
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.DeliveryMode;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.MessageProducer;
    import javax.jms.ObjectMessage;
    import javax.jms.Session;
    
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    
    public class Sender {
        
        public static void main(String[] args) throws JMSException {
            ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://10.0.0.109:61616");
            Connection connection = factory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("TestQueue");
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            ObjectMessage message = session.createObjectMessage("hello world...");
            producer.send(message);
            session.commit();
            System.out.println("sent...");
        }
    
    }
    复制代码

    消息接收

    package com.nicchagil.activemq.study.No001点对点;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.MessageConsumer;
    import javax.jms.ObjectMessage;
    import javax.jms.Session;
    
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class Receiver {
    
        public static void main(String[] args) throws JMSException {
            ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, 
                    "tcp://10.0.0.109:61616");
            Connection connection = factory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("TestQueue");
            
            MessageConsumer consumer = session.createConsumer(destination);
            ObjectMessage message = (ObjectMessage)consumer.receive();
            if (message != null) {
                String messageString = (String)message.getObject();
                System.out.println("Receive : " + messageString);
            }
        }
    
    }

    复制代码
    package com.nicchagil.activemq.study.No001点对点;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.MessageConsumer;
    import javax.jms.ObjectMessage;
    import javax.jms.Session;
    
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class Receiver {
    
        public static void main(String[] args) throws JMSException {
            ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, 
                    "tcp://10.0.0.109:61616");
            Connection connection = factory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("TestQueue");
            
            MessageConsumer consumer = session.createConsumer(destination);
            ObjectMessage message = (ObjectMessage)consumer.receive();
            if (message != null) {
                String messageString = (String)message.getObject();
                System.out.println("Receive : " + messageString);
            }
        }
    
    }
    复制代码

    看到console打印出:Receive : hello world...,可知接收到消息了,内流满面啊啊啊啊。。。

    关闭

    查询进程id(pid),禁止其进程:

    ps -ef | grep activemq
    kill -9 pid

    ps -ef | grep activemq
    kill -9 pid

    再运行Sender,她就无法连接了,哈哈哈哈哈

    Exception in thread "main" javax.jms.JMSException: Could not connect to broker URL: tcp://10.0.0.109:61616. Reason: java.net.ConnectException: Connection refused: connect

    Exception in thread "main" javax.jms.JMSException: Could not connect to broker URL: tcp://10.0.0.109:61616. Reason: java.net.ConnectException: Connection refused: connect

    好了,基本的搭建告一段落。

    荆棘

    过程中,遇到一个小问题,就是我一开始是用JDK1.6去跑的,报出常见的Unsupported major.minor version 51.0

    针对这个问题,这个帖子有很好的参考意义:

    http://www.cnblogs.com/chinafine/articles/1935748.html

    找出jar中的一个class,执行以下命令,可查出minor version、major version:

    javap -verbose yourClassName

    javap -verbose yourClassName

    或直接查看jar中的META-INFMANIFEST.MF。

    然后对照帖子中的JDK版本,换成JDK1.7就OK了。

  • 相关阅读:
    Canvas 3D球形文字云动画特效
    CSS3实现各种表情
    使用html+css+js实现弹球游戏
    CSS3实现图片木桶布局
    JQ实现弹幕效果
    css实现导航切换
    使用JS实现俄罗斯方块游戏
    JS实现文本中查找并替换字符
    Qt笔记之 01主函数和配置文件详解
    C++学习笔记之 异常
  • 原文地址:https://www.cnblogs.com/shijiaoyun/p/4672459.html
Copyright © 2011-2022 走看看