activemq 简单用法实例

栏目: java

1.从官网下载安装包,http://activemq.apache.org/download.html
2.解压安装 并执行  home/kzs/apache-activemq-5.15.2/bin/activemq start
3.测试进程 jps
[root@node1 bin]# jps
8153 activemq.jar
4.页面中打开地址: http://192.168.7.165:8161/admin/  
pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>

        <artifactId>kezesong-test</artifactId>

        <groupId>com.kezesong</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>


    <artifactId>activemq-test</artifactId>

<dependencies>

    <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-core -->

    <dependency>

        <groupId>org.apache.activemq</groupId>

        <artifactId>activemq-core</artifactId>

        <version>5.7.0</version>

    </dependency>


</dependencies>



生产者类Producer.java

/**
 * 文件名:Producer
 * 版权:Copyright by www.huluhome.com
 * 描述:
 * 修改人:kezs
 * 修改时间:2017/11/8
 * 修改内容:
 */
package com.kezesong.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;


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.Session;
import java.util.Date;

/**
 * 〈一句话功能简述〉
 *
 * @author kezs
 * @version 2017/11/8
 * @see Producer
 */
public class Producer {
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin";
    private static final String BORKEN_URL = "tcp://192.168.7.165:61616";

    public Connection createConnection() {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BORKEN_URL);
        try {
            Connection connection = connectionFactory.createConnection();
            return connection;
        } catch (JMSException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void excute(Connection connection) {
        try {
            Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            Destination destination = session.createTopic("test-topic");
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            Mybean mybean = new Mybean();
            for (int i=0;i<10;i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mybean.setName(new Date().toString());
                producer.send(session.createObjectMessage(mybean));
                System.out.println("======已经发送:" + i);
            }
            producer.close();

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



消费类:Consumer .java



/**
 * 文件名:Consumer
 * 版权:Copyright by www.huluhome.com
 * 描述:
 * 修改人:kezs
 * 修改时间:2017/11/8
 * 修改内容:
 */
package com.kezesong.activemq;


import org.apache.activemq.ActiveMQConnectionFactory;

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.ObjectMessage;
import javax.jms.Session;

/**
 * 〈一句话功能简述〉
 *
 * @author kezs
 * @version 2017/11/8
 * @see Consumer
 */
public class Consumer {
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin";
    private static final String BORKEN_URL = "tcp://192.168.7.165:61616";

    public Connection createConnection() {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BORKEN_URL);
        try {
            Connection connection = connectionFactory.createConnection();
            return connection;
        } catch (JMSException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void readMessage(Connection connection) {
        try {
            Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic("test-topic");
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message message) {
                    try {
                        Mybean bean = (Mybean) ((ObjectMessage)message).getObject();
                        System.out.println("收到消息:"+bean.getName());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }

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


调用示例:
/**
 * 文件名:start
 * 版权:Copyright by www.huluhome.com
 * 描述:
 * 修改人:kezs
 * 修改时间:2017/11/8
 * 修改内容:
 */
package com.kezesong.activemq;

import javax.jms.Connection;
import javax.jms.JMSException;

/**
 * 〈一句话功能简述〉
 *
 * @author kezs
 * @version 2017/11/8
 * @see start
 */
public class start {
    public static void main(String[] args) {
       new Thread(new Runnable() {
           @Override
           public void run() {
               produceMessage();
           }
       }).start();

        consumerMessage();

    }

    private static void produceMessage() {
        Producer producer = new Producer();
        Connection connection = producer.createConnection();
        try {
            connection.start();
        } catch (JMSException e) {
            e.printStackTrace();
        }
        producer.excute(connection);
        try {
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    private static void consumerMessage() {
        Consumer consumer = new Consumer();
        Connection connection = consumer.createConnection();
        try {
            connection.start();
        } catch (JMSException e) {
            e.printStackTrace();
        }
        consumer.readMessage(connection);
//        try {
////            connection.close();
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
    }
}


参考:http://www.cnblogs.com/luochengqiuse/p/4678020.html





曾梦想仗剑走天涯 看一看世界的繁华 年少的心总有些轻狂 如今你四海为家 曾让你心疼的姑娘 如今已悄然无踪影 爱情总让你渴望又感到烦恼 曾让你遍体鳞伤 Dilililidilililidenda Dilililidilililidada Dilililidilililidada 走在勇往直前的路上 Dilililidilililidenda Dilililidilililidada Dilililidilililidada 有难过也有精彩 每一次难过的时候 就独自看一看大海 总想起身边走在路上的朋友 有多少正在疗伤 Dilililidilililidenda Dilililidilililidada Dilililidilililidada 不知多少孤独的夜晚 Dilililidilililidenda Dilililidilililidada Dilililidilililidada 从昨夜酒醉醒来 每一次难过的时候 就独自看一看大海 总想起身边走在路上的朋友 有多少正在醒来 让我们干了这杯酒 好男儿胸怀像大海 经历了人生百态世间的冷暖 这笑容温暖纯真 每一次难过的时候 就独自看一看大海 总想起身边走在路上的朋友 有多少正在醒来 让我们干了这杯酒 好男儿胸怀像大海 经历了人生百态世间的冷暖 这笑容温暖纯真

关键字

最近访客