ActiveMq详解(一)

ActiveMq详解

Apache ActiveMQ是一款高效流行的消息中间件。支持多种语言跨平台,遵循JMS1.1和J2EE1.4标准。
企业级开发中,用于服务解耦合,以及高并发处理等……

  • Activemq下载安装
  • Activemq启动
  • Activemq简单使用
  • session消息确认
  • 简单配置
  • 小结

    Activemq下载安装

    ActiveMq下载,选择linux版本即可。
    将下载到的apache-activemq-5.11.1-bin.tar.gz程序放入安装目录解压即可,下载好的activemq无需编译便可使用。

    Activemq启动

这里是ubuntu64为系统,启动如下(需要java环境):

death@ubuntu:~/activemq/apache-activemq-5.11.1/bin/linux-x86-64$ ./activemq start

启动完成后,访问http://192.168.182.129:8161/admin/可以正常打开并提示输入用户名密码的话,启动正常。用户名,密码默认均为admin
这里写图片描述
当然启动失败,需要查看日志分析。
这里写图片描述
在data目录下查询日志分析。

Activemq简单使用

队列样例(o2o),生成者:

package com.ccycc.activemq.project.producer;

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 org.apache.activemq.ActiveMQConnectionFactory;


public class JMSProducer {

    public static void main(String[] args) {

        JMSProducer jmsProducer = new JMSProducer();
        jmsProducer.producer();
    }
    private void producer() {

        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageProducer producer = null;
        try {

            connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.182.129:61616");
            //创建连接
            connection = connectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建session
            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            //创建消息队列
            destination = session.createQueue("jms_q");
            //对于该队列创建生成者
            producer = session.createProducer(destination);
            //生成者发生消息
            producer.send(session.createTextMessage("hello"));
            session.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {

            try {
                if(session != null) {

                    session.close();
                }
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {

                try {
                    if(connection != null) {

                        connection.close();
                    }
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

消费者:

package com.ccycc.activemq.project.producer.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSConsumer {

    public static void main(String[] args) {

        JMSConsumer consumer = new JMSConsumer();
        consumer.consumer();
    }
    public void consumer() {

        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageConsumer consumer = null;

        try {

            connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.182.129:61616");
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("jms_q");
            consumer = session.createConsumer(destination);
            consumer.setMessageListener(new MessageListener() {

                @Override
                public void onMessage(Message message) {
                    // TODO Auto-generated method stub
                    if(message != null) {

                        if(message instanceof TextMessage) {

                            TextMessage textMessage = (TextMessage) message;
                            try {
                                System.out.println(textMessage.getText());
                            } catch (JMSException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }
            });
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

上面代码可见,使用起来是非常方便的。

session消息确认

AUTO_ACKNOWLEDGE:当客户端成功接收后返回,会话自动确认客户端收到消息。
CLIENT_ACKNOWLEDGE:客户端通过消息的acknowledge()方法消息确认,确认是在会话层进行的;确认一个被消费的消息,将自动确认所有已被消费的消息。
DUPS_OK_ACKNOWLEDGE:会话迟钝确认消息提交。如果JMS Provider失败,那么可能会导致一些重复消息。如果消息重复,那么JMS Provider必须把消息头中的JMSRedelivered字段修改为true。

简单配置

连接用户名密码配置:
config/activemq.xml中在shutdownHooks标签后增加如下内容,设置连接用户名密码。

<plugins>  
            <simpleAuthenticationPlugin>  
                <users>  
                    <authenticationUser username="admin" password="admin" groups="users,admins"/>  
                </users>  
            </simpleAuthenticationPlugin>  
        </plugins>

连接端口配置:
config/activemq.xml中,修改uri的端口即可。

<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>

小结

消息队列的应用场景非常广泛。

猜你喜欢

转载自blog.csdn.net/ccycc88/article/details/80582622