初见Java消息中间件之ActiveMQ

来源:互联网 发布:unity3d棋牌游戏教程 编辑:程序博客网 时间:2024/05/09 22:14

消息中间件概况

消息队列技术是分布式应用间交换信息的一种技术。消息队列可驻留在内存或磁盘上,队列存储消息直到它们被应用程序读走。通过消息队列,应用程序可独立地执行–它们不需要知道彼此的位置、或在继续执行前不需要等待接收程序接收此消息。 
在分布式计算环境中,为了集成分布式应用,开发者需要对异构网络环境下的分布式应用提供有效的通信手段。为了管理需要共享的信息,对应用提供公共的信息交换机制是重要的。 
设计分布式应用的方法主要有:远程过程调用(PRC)-分布式计算环境(DCE)的基础标准成分之一;对象事务监控(OTM)-基于CORBA的面向对象工业标准与事务处理(TP)监控技术的组合;消息队列(MessageQueue)-构造分布式应用的松耦合方法。 
(a) 分布计算环境/远程过程调用 (DCE/RPC) 
RPC是DCE的成分,是一个由开放软件基金会(OSF)发布的应用集成的软件标准。RPC模仿一个程序用函数引用来引用另一程序的传统程序设计方法,此引用是过程调用的形式,一旦被调用,程序的控制则转向被调用程序。 
在RPC 实现时,被调用过程可在本地或远地的另一系统中驻留并在执行。当被调用程序完成处理输入数据,结果放在过程调用的返回变量中返回到调用程序。RPC完成后程序控制则立即返回到调用程序。因此RPC模仿子程序的调用/返回结构,它仅提供了Client(调用程序)和Server(被调用过程)间的同步数据交换。 
(b) 对象事务监控 (OTM) 
基于CORBA的面向对象工业标准与事务处理(TP)监控技术的组合,在CORBA规范中定义了:使用面向对象技术和方法的体系结构;公共的 Client/Server程序设计接口;多平台间传输和翻译数据的指导方针;开发分布式应用接口的语言(IDL)等,并为构造分布的 Client/Server应用提供了广泛及一致的模式。 
(c) 消息队列 (Message Queue) 
消息队列为构造以同步或异步方式实现的分布式应用提供了松耦合方法。消息队列的API调用被嵌入到新的或现存的应用中,通过消息发送到内存或基于磁盘的队列或从它读出而提供信息交换。消息队列可用在应用中以执行多种功能,比如要求服务、交换信息或异步处理等。 
中间件是一种独立的系统软件或服务程序,分布式应用系统借助这种软件在不同的技术之间共享资源,管理计算资源和网络通讯。它在计算机系统中是一个关键软件,它能实现应用的互连和互操作性,能保证系统的安全、可靠、高效的运行。中间件位于用户应用和操作系统及网络软件之间,它为应用提供了公用的通信手段,并且独立于网络和操作系统。中间件为开发者提供了公用于所有环境的应用程序接口,当应用程序中嵌入其函数调用,它便可利用其运行的特定操作系统和网络环境的功能,为应用执行通信功能。 
如果没有消息中间件完成信息交换,应用开发者为了传输数据,必须要学会如何用网络和操作系统软件的功能,编写相应的应用程序来发送和接收信息,且交换信息没有标准方法,每个应用必须进行特定的编程从而和多平台、不同环境下的一个或多个应用通信。例如,为了实现网络上不同主机系统间的通信,将要求具备在网络上如何交换信息的知识(比如用TCP/IP的socket程序设计);为了实现同一主机内不同进程之间的通讯,将要求具备操作系统的消息队列或命名管道(Pipes)等知识。 
目前中间件的种类很多,如交易管理中间件、面向Java应用的Web应用服务器中间件等,而消息传输中间件(MOM)是其中的一种。它简化了应用之间数据的传输,屏蔽底层异构操作系统和网络平台,提供一致的通讯标准和应用开发,确保分布式计算网络环境下可靠的、跨平台的信息传输和数据交换。它基于消息队列的存储-转发机制,并提供特有的异步传输机制,能够基于消息传输和异步事务处理实现应用整合与数据交换。

####简单点

消息中间件一般两个功能,解耦和异步处理

安装ActiveMQ

下载下来应该是一个安装包,打开自己系统相对应的版本,启动服务。 
这里写图片描述 
这里写图片描述

关于ActiveMQ的运行流程

这里写图片描述

两种模式

1.队列模式,多个消费者同时运行,平分队列里的消息 
2.主题模式,消费者先订阅才可以接收消息,多个消费者同时运行,每个消费者都会接收消息队列里的全部消息

队列模式

APPConsumer.java

package com.ysk.jms.queque;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * Created by Y.S.K on 2017/8/25 in jmstest. * 队列模式 */public class AppConsumer {    //下面ip需要替换为自己的ip,端口不需要变.    private static final String url = "tcp://192.168.51.2:61616";    private static final String queueName = "queue-test";    public static void main(String[] args) throws Exception {        //1.创建我们的连接工厂        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);        //2.创建connection        Connection connection = connectionFactory.createConnection();        //3.启动连接        connection.start();        //4.创建回话        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);        //5.创建目标        Destination destination = session.createQueue(queueName);        //6.创建一个消费者         MessageConsumer consumer=session.createConsumer(destination);         //7.创建一个监听器        consumer.setMessageListener(new MessageListener() {            public void onMessage(Message message) {                TextMessage textMessage=(TextMessage)message;                try {                    System.out.println("接收消息"+textMessage.getText());                }catch (Exception e){                    e.printStackTrace();                }            }        });        //9.关闭连接        //connection.close();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

AppProducer.java

package com.ysk.jms.queque;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * Created by Y.S.K on 2017/8/25 in jmstest. */public class AppProducer {    private static final String url = "tcp://192.168.51.2:61616";    private static final String queueName = "queue-test";    public static void main(String[] args) throws Exception {        //1.创建我们的连接工厂        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);        //2.创建connection        Connection connection = connectionFactory.createConnection();        //3.启动连接        connection.start();        //4.创建回话        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);        //5.创建目标        Destination destination = session.createQueue(queueName);        //6.创建一个生产者        MessageProducer producer = session.createProducer(destination);        for (int i = 0; i < 100; i++) {            //7.创建消息            TextMessage textMessage = session.createTextMessage("test" + i);            //8.发布消息            producer.send(textMessage);            System.out.println("发送消息"+textMessage.getText());        }        //9.关闭连接        connection.close();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

运行结果 
启动两个消费者。 
这里写图片描述 
运行生产者。

生产者控制台输出 
发送消息test0 
发送消息test1 
发送消息test2 
发送消息test3 
发送消息test4 
发送消息test5 
发送消息test6 
发送消息test7 
发送消息test8 
发送消息test9 
发送消息test10 
发送消息test11 
发送消息test12 
发送消息test13 
发送消息test14 
发送消息test15 
发送消息test16 
发送消息test17 
发送消息test18 
发送消息test19 
发送消息test20 
发送消息test21 
发送消息test22 
发送消息test23 
发送消息test24 
发送消息test25 
发送消息test26 
发送消息test27 
发送消息test28 
发送消息test29 
发送消息test30 
发送消息test31 
发送消息test32 
发送消息test33 
发送消息test34 
发送消息test35 
发送消息test36 
发送消息test37 
发送消息test38 
发送消息test39 
发送消息test40 
发送消息test41 
发送消息test42 
发送消息test43 
发送消息test44 
发送消息test45 
发送消息test46 
发送消息test47 
发送消息test48 
发送消息test49 
发送消息test50 
发送消息test51 
发送消息test52 
发送消息test53 
发送消息test54 
发送消息test55 
发送消息test56 
发送消息test57 
发送消息test58 
发送消息test59 
发送消息test60 
发送消息test61 
发送消息test62 
发送消息test63 
发送消息test64 
发送消息test65 
发送消息test66 
发送消息test67 
发送消息test68 
发送消息test69 
发送消息test70 
发送消息test71 
发送消息test72 
发送消息test73 
发送消息test74 
发送消息test75 
发送消息test76 
发送消息test77 
发送消息test78 
发送消息test79 
发送消息test80 
发送消息test81 
发送消息test82 
发送消息test83 
发送消息test84 
发送消息test85 
发送消息test86 
发送消息test87 
发送消息test88 
发送消息test89 
发送消息test90 
发送消息test91 
发送消息test92 
发送消息test93 
发送消息test94 
发送消息test95 
发送消息test96 
发送消息test97 
发送消息test98 
发送消息test99


消费者控制台1输出

接收消息test0 
接收消息test2 
接收消息test4 
接收消息test6 
接收消息test8 
接收消息test10 
接收消息test12 
接收消息test14 
接收消息test16 
接收消息test18 
接收消息test20 
接收消息test22 
接收消息test24 
接收消息test26 
接收消息test28 
接收消息test30 
接收消息test32 
接收消息test34 
接收消息test36 
接收消息test38 
接收消息test40 
接收消息test42 
接收消息test44 
接收消息test46 
接收消息test48 
接收消息test50 
接收消息test52 
接收消息test54 
接收消息test56 
接收消息test58 
接收消息test60 
接收消息test62 
接收消息test64 
接收消息test66 
接收消息test68 
接收消息test70 
接收消息test72 
接收消息test74 
接收消息test76 
接收消息test78 
接收消息test80 
接收消息test82 
接收消息test84 
接收消息test86 
接收消息test88 
接收消息test90 
接收消息test92 
接收消息test94 
接收消息test96 
接收消息test98


消费者控制台2输出

接收消息test1 
接收消息test3 
接收消息test5 
接收消息test7 
接收消息test9 
接收消息test11 
接收消息test13 
接收消息test15 
接收消息test17 
接收消息test19 
接收消息test21 
接收消息test23 
接收消息test25 
接收消息test27 
接收消息test29 
接收消息test31 
接收消息test33 
接收消息test35 
接收消息test37 
接收消息test39 
接收消息test41 
接收消息test43 
接收消息test45 
接收消息test47 
接收消息test49 
接收消息test51 
接收消息test53 
接收消息test55 
接收消息test57 
接收消息test59 
接收消息test61 
接收消息test63 
接收消息test65 
接收消息test67 
接收消息test69 
接收消息test71 
接收消息test73 
接收消息test75 
接收消息test77 
接收消息test79 
接收消息test81 
接收消息test83 
接收消息test85 
接收消息test87 
接收消息test89 
接收消息test91 
接收消息test93 
接收消息test95 
接收消息test97 
接收消息test99

主题模式

只需要修改为creatTopic,生产者和消费者都需要改 
这里写图片描述 
先启动两个消费者,再启动生产者(如果先启动生产者,会因为消费者没有订阅而接收不到任何消息) 
运行结果

消费者1的控制台输出

接收消息test5 
接收消息test6 
接收消息test7 
接收消息test8 
接收消息test9 
接收消息test10 
接收消息test11 
接收消息test12 
接收消息test13 
接收消息test14 
接收消息test15 
接收消息test16 
接收消息test17 
接收消息test18 
接收消息test19 
接收消息test20 
接收消息test21 
接收消息test22 
接收消息test23 
接收消息test24 
接收消息test25 
接收消息test26 
接收消息test27 
接收消息test28 
接收消息test29 
接收消息test30 
接收消息test31 
接收消息test32 
接收消息test33 
接收消息test34 
接收消息test35 
接收消息test36 
接收消息test37 
接收消息test38 
接收消息test39 
接收消息test40 
接收消息test41 
接收消息test42 
接收消息test43 
接收消息test44 
接收消息test45 
接收消息test46 
接收消息test47 
接收消息test48 
接收消息test49 
接收消息test50 
接收消息test51 
接收消息test52 
接收消息test53 
接收消息test54 
接收消息test55 
接收消息test56 
接收消息test57 
接收消息test58 
接收消息test59 
接收消息test60 
接收消息test61 
接收消息test62 
接收消息test63 
接收消息test64 
接收消息test65 
接收消息test66 
接收消息test67 
接收消息test68 
接收消息test69 
接收消息test70 
接收消息test71 
接收消息test72 
接收消息test73 
接收消息test74 
接收消息test75 
接收消息test76 
接收消息test77 
接收消息test78 
接收消息test79 
接收消息test80 
接收消息test81 
接收消息test82 
接收消息test83 
接收消息test84 
接收消息test85 
接收消息test86 
接收消息test87 
接收消息test88 
接收消息test89 
接收消息test90 
接收消息test91 
接收消息test92 
接收消息test93 
接收消息test94 
接收消息test95 
接收消息test96 
接收消息test97 
接收消息test98 
接收消息test99


消费者2的控制台输出

接收消息test5 
接收消息test6 
接收消息test7 
接收消息test8 
接收消息test9 
接收消息test10 
接收消息test11 
接收消息test12 
接收消息test13 
接收消息test14 
接收消息test15 
接收消息test16 
接收消息test17 
接收消息test18 
接收消息test19 
接收消息test20 
接收消息test21 
接收消息test22 
接收消息test23 
接收消息test24 
接收消息test25 
接收消息test26 
接收消息test27 
接收消息test28 
接收消息test29 
接收消息test30 
接收消息test31 
接收消息test32 
接收消息test33 
接收消息test34 
接收消息test35 
接收消息test36 
接收消息test37 
接收消息test38 
接收消息test39 
接收消息test40 
接收消息test41 
接收消息test42 
接收消息test43 
接收消息test44 
接收消息test45 
接收消息test46 
接收消息test47 
接收消息test48 
接收消息test49 
接收消息test50 
接收消息test51 
接收消息test52 
接收消息test53 
接收消息test54 
接收消息test55 
接收消息test56 
接收消息test57 
接收消息test58 
接收消息test59 
接收消息test60 
接收消息test61 
接收消息test62 
接收消息test63 
接收消息test64 
接收消息test65 
接收消息test66 
接收消息test67 
接收消息test68 
接收消息test69 
接收消息test70 
接收消息test71 
接收消息test72 
接收消息test73 
接收消息test74 
接收消息test75 
接收消息test76 
接收消息test77 
接收消息test78 
接收消息test79 
接收消息test80 
接收消息test81 
接收消息test82 
接收消息test83 
接收消息test84 
接收消息test85 
接收消息test86 
接收消息test87 
接收消息test88 
接收消息test89 
接收消息test90 
接收消息test91 
接收消息test92 
接收消息test93 
接收消息test94 
接收消息test95 
接收消息test96 
接收消息test97 
接收消息test98 
接收消息test99