ubuntu下简单使用activemq

activemq的安装与配置

1.首先官网下载activemq

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

2.然后解压

sudo tar -zxvf 源文件.tar.gz /usr/local/soft

3.更改配置

然后在/usr/local/soft/apache-activemq-5.12.1/bin下有一个env文件,编辑此文件设置对应的jdk目录,要说明的是,新版本的activemq已经不支持jdk1.7以下,亲测不支持6u45,所以还是尽量在jdk1.7版本以上吧,最后一行(JAVACMD="auto")上面添加
JAVA_HOME="/usr/lib/jvm/jdk1.7.0_65"

4.启动activemq

进入到bin目录下,使用sudo ./activemq start
出现如下信息说明启动成功,即可访问管理界面:http://localhost:8161/admin/,默认的用户名与密码一般都是admin


INFO: Loading '/usr/local/soft/apache-activemq-5.12.1//bin/env'
INFO: Using java '/usr/lib/jvm/jdk1.7.0_65/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
INFO: pidfile created : '/usr/local/soft/apache-activemq-5.12.1//data/activemq.pid' (pid '5532')

看到PID(进程号,每次会有所不同)
管理界面:

ubuntu下简单使用activemq_第1张图片

5.简单实例:

5.1 P2P(point-point点对点模式)

5.1.1Sender.java

package cn.example.activemq;


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

import javax.jms.*;

/**
 * @author 
 * @company 
 * @date 
 */
public class Sender {
    private static  final int SEND_NUM=5;

    /**
     * 发送消息
     * @param session
     * @param messageProducer
     */
    public static void sendMessage(Session session,MessageProducer messageProducer) {

        for (int i = 0; i < SEND_NUM; i++) {
            TextMessage message = null;
            try {
                //创建消息
                message = session.createTextMessage("ActiveMQ发送消息:" + i);
                System.out.println("发送消息:" + "ActiveMQ发送消息" + i);
                messageProducer.send(message);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        //ConnectionFactory 连接工厂,JMS用它建立工厂
        ConnectionFactory connectionFactory;
        //Connection是JMS客户端连接Provider
        Connection connection=null;
        //一个接收或者发送消息的线程
        Session session;
        //消息的目的地
        Destination destination;
        //消息发送者
        MessageProducer messageProducer;
        //构造ConllectionFactory的实例对象,使用ActiveMQ
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
        try {
            //构造connection
            connection = connectionFactory.createConnection();
            connection.start();
            //获取连接
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            //通过链接创建队列给消息目的地
            destination = session.createQueue("FirstQueue");
            //创建消息发送者
            messageProducer = session.createProducer(destination);
            //设置不持久化
            messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            sendMessage(session,messageProducer);
            session.commit();
        }catch (JMSException e) {
            e.printStackTrace();
        } finally {
            if(null!=connection){
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

5.1.2Receiver

package cn.example.activemq;

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

import javax.jms.*;
import java.util.logging.Logger;

/**
 * @author 
 * @company 
 * @date
 */
public class Receiver {



    public static void main(String[] args) {
        //ConnectionFactory连接工厂
        ConnectionFactory connectionFactory;
        //是JMS客户端连接JMS Provider:他是用来处理消息路由与传递的消息
        Connection connection=null;
        //创建连接
        Session session;
        //消息目的地
        Destination destination;
        //消息接收者
        MessageConsumer messageConsumer;

        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("FirstQueue");
            messageConsumer = session.createConsumer(destination);
            while(true){
                TextMessage textMessage = (TextMessage) messageConsumer.receive(500000);
                if(null!=textMessage){
                    System.out.println("收到消息:" + textMessage.getText());
                }else{
                    break;
                }
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            if(null!=connection){
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

5.1.3测试

上面的500000代表500秒,当然没必要设置这么长时间,你可以同时启动4台Receiver,然后启动Sender,会出现如下情况,这就是点对点的效果

ubuntu下简单使用activemq_第2张图片

ubuntu下简单使用activemq_第3张图片

ubuntu下简单使用activemq_第4张图片

ubuntu下简单使用activemq_第5张图片

ubuntu下简单使用activemq_第6张图片

同时在web页面你会看到如下效果

ubuntu下简单使用activemq_第7张图片

5.2publisher-Subscriber(发布-订阅)

5.2.1TopicPublisher


package cn.example.activemq.topic;

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

import javax.jms.*;

/**
 * Created by 
 */
public class TopicPublisher {

    public static void main(String[] args) throws InterruptedException {
        //JMS建立工厂
        ActiveMQConnectionFactory connectionFactory;
        //JMS客户端连接Provider
        Connection connection = null;
        //接受发送消息的线程
        Session session = null;
        //主题定义
        Topic topic;
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic("mq.topic");
            MessageProducer producer = session.createProducer(destination);
            //消息的创建以及发送
            for (int i = 1; i <= 20; i++) {
                MapMessage message = session.createMapMessage();
                message.setLong("count", i);
                producer.send(message);
                System.out.println("发布者发送的消息"+i);
            }
            session.commit();
            session.close();
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            if(null!=session){
                try {
                    session.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
            if(null!=connection){
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}

5.2.2 TopicSubscriber

package cn.example.activemq.topic;

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

import javax.jms.*;

/**
 * Created by 
 */
public class TopicSubscriber {

    public static void main(String[] args) {
        ActiveMQConnectionFactory connectionFactory;
        Connection connection=null;
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Topic topic = session.createTopic("mq.topic");
            final MessageConsumer consumer = session.createConsumer(topic);
            while(true){
                consumer.setMessageListener(new MessageListener() {
                    public void onMessage(Message message) {
                        MapMessage mapMessage = null;
                        try {
                            mapMessage = (MapMessage) message;
                            System.out.println("订阅者接收到消息:" + mapMessage.getLong("count"));
                        } catch (JMSException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            if(null!=connection){
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }


    }

}



同时开5个开启TopicSubscriber,使用TopicPublisher发送消息,这时会发现与点对点队列形式之间最大的区别,在发布-订阅的情况下,订阅的每个人都会接收到发布的同样的消息,生产者(发送消息)一对多消费者(接收消息)而队列如果要接收所有消息,只能发给一个消费者,生产者(发送消息)一对一消费者(接收消息)


 
 

你可能感兴趣的:(ubuntu下简单使用activemq)