消息中间件 ,ActiveMQ,RabbitMQ,Kafaka这三个框架,现在是用得比较多的,先来个ActiveMq的入门案例,老爽了,通过ActiveMq可以简单的完成消息的传递.



ActiveMQ入门案例-yellowcong_apache


下载地址

#官网下载地址
http://activemq.apache.org/

#这是官网的5.11.1版本
wget http://archive.apache.org/dist/activemq/5.11.1/apache-activemq-5.11.1-bin.tar.gz

#本人七牛云的下载地址
http://yellowcong.qiniudn.com/apache-activemq-5.11.1-bin.tar.gz

客户端安装

#下载
wget http://yellowcong.qiniudn.com/apache-activemq-5.11.1-bin.tar.gz

#解压
tar -zxvf apache-activemq-5.11.1-bin.tar.gz

#启动服务器
./apache-activemq-5.11.1/bin/activemq-admin start

#停止服务器
./apache-activemq-5.11.1/bin/activemq-admin stop

启动的时候,设定activemq的环境

ActiveMQ入门案例-yellowcong_apache_02

目录结构

ActiveMQ入门案例-yellowcong_activemq_03

web访问地址

管控台,直接是依赖于jetty的,内嵌jetty服务器

#访问activeMq的管控台
http://192.168.66.100:8161/admin/

ActiveMQ入门案例-yellowcong_java_04

管控台情况

ActiveMQ入门案例-yellowcong_apache_05

端口设定

#修改jetty.xml配置
vim /usr/local/activemq/apache-activemq-5.11.1/conf/jetty.xml

#找到103行左右,可以修改默认端口号8161
 <property name="port" value="8161"/>

ActiveMQ入门案例-yellowcong_apache_06

conf目录下面的jetty.xml配置文件

ActiveMQ入门案例-yellowcong_apache_07

管控台用户修改

用户名和密码是默认admin的

#修改jetty-realm.properties
vim /usr/local/activemq/apache-activemq-5.11.1/conf/jetty-realm.properties

#用户名:密码,角色
username:password ,admin

ActiveMQ入门案例-yellowcong_apache_08

里面的配置

ActiveMQ入门案例-yellowcong_xml_09

端口配置

#端口默认配置即可,tcp默认tcp://0.0.0.0:61616
 vim activemq.xml

 <transportConnectors>
            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>

案例-生产者

package com.yellowcong.provider;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;

/**
 *
 * 作者:yellowcong <br/>
 * 日期:2017/12/08 <br/>
 * 時間:15:10:39 <br/>
 * 描述:
 */
public class DemoProvider {

    private static final String ACTIVEMQ_HOST = "tcp://192.168.66.100:61616";
    public static void main(String[] args) throws Exception {
        //1.建立ConnectionFactory
        ConnectionFactory  fs= new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,  //默认用户名 null
                ActiveMQConnectionFactory.DEFAULT_PASSWORD, //默认密码 null
                ACTIVEMQ_HOST) ; //连接地址

        //2.创建连接
        Connection  conn = fs.createConnection();
        //开启连接
        conn.start();

        //3.创建session
        //paramBoolean 是否启用事物 Boolean 对象
        //paramInt 接收模式  4种模式
        //Session.AUTO_ACKNOWLEDGE = 1; //自动签收
        //Session.CLIENT_ACKNOWLEDGE = 2; //客户端,手动签收,通过 执行  TextMessage.acknowledge(); 来表示签收
        //Session.DUPS_OK_ACKNOWLEDGE = 3;//不必确保用户是否签收
        //Session.SESSION_TRANSACTED = 0;//事物

        Session session = conn.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);

        //4.通过session来创建Destination
        Destination dest = session.createQueue("demo");  //队列的名称

        //5.创建发送和接收对象
        MessageProducer produc = session.createProducer(dest);
        //非持久化的方式
        produc.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        //6.发送5条数据
        for(int i=0;i<5;i++) {
            //创建Message
            Message msg = new ActiveMQTextMessage();
            msg.setStringProperty("username", "你是yellowcong"+i+"号");

            //发送Message
            produc.send(msg);
        }

        //关闭连接
        conn.close();
    }
}

产生的数据

ActiveMQ入门案例-yellowcong_java_10

案例-消费者

package com.yellowcong.provider;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 *
 * 作者:yellowcong <br/>
 * 日期:2017/12/08 <br/>
 * 時間:15:39:22 <br/>
 * 描述:
 */
public class DemoCustomer {
    //ActiveMq的主机地址
    private static final String ACTIVEMQ_HOST = "tcp://192.168.66.100:61616";


    public static void main(String[] args) throws Exception {

        //1、创建连接的ConnectionFactory
        ConnectionFactory fs = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER, 
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                ACTIVEMQ_HOST);

        //2.获取连接
        Connection conn = fs.createConnection();
        conn.start();

        //3.获取session
        Session session = conn.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);

        //4.获取目的地
        Destination desc = session.createQueue("demo");

        //5.获取消费者
        MessageConsumer cus = session.createConsumer(desc);

        //6.获取所有数据
        while(true) {
            //1 receive():Message
            //获取下一个消息。这个调用将导致无限期的阻塞,直到有新的消息产生。
            //2 receive(long timeout):Message
            //获取下一个消息。这个调用可能导致一段时间的阻塞,直到超时或者有新的消息产生。超时则返回null。
            //3 receiveNoWait():Message
            //获取下一个消息。这个调用不会导致阻塞,如果没有下一个消息,直接返回null。

            TextMessage msg = (TextMessage) cus.receive();
            if(msg == null) break;
            System.out.println(msg.getText());
        }

        //7 关闭连接
        conn.close();
    }
}

ActiveMQ入门案例-yellowcong_apache_11

管控台的数据处理结果

ActiveMQ入门案例-yellowcong_apache_12

目录结构

ActiveMQ入门案例-yellowcong_java_13

pom.xml

<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>yellowcong</groupId>
    <artifactId>day12_08</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>day12_08</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.11.1</version>
        </dependency>
    </dependencies>
</project>