1、下载安装ActiveMQ


  ActiveMQ官网下载地址:http://activemq.apache.org/download.html

  ActiveMQ 提供了Windows 和Linux、Unix 等几个版本,楼主这里选择了Linux 版本下进行开发。

  下载完安装包,解压之后的目录:

 

  从它的目录来说,还是很简单的:

    • bin存放的是脚本文件
    • conf存放的是基本配置文件
    • data存放的是日志文件
    • docs存放的是说明文档
    • examples存放的是简单的实例
    • lib存放的是activemq所需jar包
    • webapps用于存放项目的目录

2、启动ActiveMQ 


  进入到ActiveMQ 安装目录的Bin 目录,linux 下输入 ./activemq start 启动activeMQ 服务。

   输入命令之后,会提示我们创建了一个进程IP 号,这时候说明服务已经成功启动了。

  

  ActiveMQ默认启动时,启动了内置的jetty服务器,提供一个用于监控ActiveMQ的admin应用。 
  admin:http://127.0.0.1:8161/admin/

  我们在浏览器打开链接之后输入账号密码(这里和tomcat 服务器类似)

  默认账号:admin

  密码:admin

  

   到这里为止,ActiveMQ 服务端就启动完毕了。

   ActiveMQ 在linux 下的终止命令是 ./activemq stop

3、创建一个ActiveMQ工程


  项目目录结构:

  

  上述在官网下载ActiveMq 的时候,我们可以在目录下看到一个jar包:

  

  这个jar 包就是我们需要在项目中进行开发中使用到的相关依赖。

  3.1 创建生产者

  1. public class Producter {
  2.  
  3. //ActiveMq 的默认用户名
  4. private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
  5. //ActiveMq 的默认登录密码
  6. private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
  7. //ActiveMQ 的链接地址
  8. private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;
  9.  
  10. AtomicInteger count = new AtomicInteger(0);
  11. //链接工厂
  12. ConnectionFactory connectionFactory;
  13. //链接对象
  14. Connection connection;
  15. //事务管理
  16. Session session;
  17. ThreadLocal<MessageProducer> threadLocal = new ThreadLocal<>();
  18.  
  19. public void init(){
  20. try {
  21. //创建一个链接工厂
  22. connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL);
  23. //从工厂中创建一个链接
  24. connection = connectionFactory.createConnection();
  25. //开启链接
  26. connection.start();
  27. //创建一个事务(这里通过参数可以设置事务的级别)
  28. session = connection.createSession(true,Session.SESSION_TRANSACTED);
  29. } catch (JMSException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33.  
  34. public void sendMessage(String disname){
  35. try {
  36. //创建一个消息队列
  37. Queue queue = session.createQueue(disname);
  38. //消息生产者
  39. MessageProducer messageProducer = null;
  40. if(threadLocal.get()!=null){
  41. messageProducer = threadLocal.get();
  42. }else{
  43. messageProducer = session.createProducer(queue);
  44. threadLocal.set(messageProducer);
  45. }
  46. while(true){
  47. Thread.sleep(1000);
  48. int num = count.getAndIncrement();
  49. //创建一条消息
  50. TextMessage msg = session.createTextMessage(Thread.currentThread().getName()+
  51. "productor:我是大帅哥,我现在正在生产东西!,count:"+num);
  52. System.out.println(Thread.currentThread().getName()+
  53. "productor:我是大帅哥,我现在正在生产东西!,count:"+num);
  54. //发送消息
  55. messageProducer.send(msg);
  56. //提交事务
  57. session.commit();
  58. }
  59. } catch (JMSException e) {
  60. e.printStackTrace();
  61. } catch (InterruptedException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }

    

  3.2 创建消费者

  1. public class Comsumer {
  2.  
  3. private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
  4.  
  5. private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
  6.  
  7. private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;
  8.  
  9. ConnectionFactory connectionFactory;
  10.  
  11. Connection connection;
  12.  
  13. Session session;
  14.  
  15. ThreadLocal<MessageConsumer> threadLocal = new ThreadLocal<>();
  16. AtomicInteger count = new AtomicInteger();
  17.  
  18. public void init(){
  19. try {
  20. connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL);
  21. connection = connectionFactory.createConnection();
  22. connection.start();
  23. session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
  24. } catch (JMSException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28.  
  29. public void getMessage(String disname){
  30. try {
  31. Queue queue = session.createQueue(disname);
  32. MessageConsumer consumer = null;
  33.  
  34. if(threadLocal.get()!=null){
  35. consumer = threadLocal.get();
  36. }else{
  37. consumer = session.createConsumer(queue);
  38. threadLocal.set(consumer);
  39. }
  40. while(true){
  41. Thread.sleep(1000);
  42. TextMessage msg = (TextMessage) consumer.receive();
  43. if(msg!=null) {
  44. msg.acknowledge();
  45. System.out.println(Thread.currentThread().getName()+": Consumer:我是消费者,我正在消费Msg"+msg.getText()+"--->"+count.getAndIncrement());
  46. }else {
  47. break;
  48. }
  49. }
  50. } catch (JMSException e) {
  51. e.printStackTrace();
  52. } catch (InterruptedException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }

4、运行ActiveMQ项目


  4.1 生产者开始生产消息

  1. public class TestMq {
  2. public static void main(String[] args){
  3. Producter producter = new Producter();
  4. producter.init();
  5. TestMq testMq = new TestMq();
  6. try {
  7. Thread.sleep(1000);
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();
  10. }
  11. //Thread 1
  12. new Thread(testMq.new ProductorMq(producter)).start();
  13. //Thread 2
  14. new Thread(testMq.new ProductorMq(producter)).start();
  15. //Thread 3
  16. new Thread(testMq.new ProductorMq(producter)).start();
  17. //Thread 4
  18. new Thread(testMq.new ProductorMq(producter)).start();
  19. //Thread 5
  20. new Thread(testMq.new ProductorMq(producter)).start();
  21. }
  22.  
  23. private class ProductorMq implements Runnable{
  24. Producter producter;
  25. public ProductorMq(Producter producter){
  26. this.producter = producter;
  27. }
  28.  
  29. @Override
  30. public void run() {
  31. while(true){
  32. try {
  33. producter.sendMessage("Jaycekon-MQ");
  34. Thread.sleep(10000);
  35. } catch (InterruptedException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. }
  41. }

   运行结果:

  1. INFO | Successfully connected to tcp://localhost:61616
  2. Thread-6productor:我是大帅哥,我现在正在生产东西!,count:0
  3. Thread-4productor:我是大帅哥,我现在正在生产东西!,count:1
  4. Thread-2productor:我是大帅哥,我现在正在生产东西!,count:3
  5. Thread-5productor:我是大帅哥,我现在正在生产东西!,count:2
  6. Thread-3productor:我是大帅哥,我现在正在生产东西!,count:4
  7. Thread-6productor:我是大帅哥,我现在正在生产东西!,count:5
  8. Thread-3productor:我是大帅哥,我现在正在生产东西!,count:6
  9. Thread-5productor:我是大帅哥,我现在正在生产东西!,count:7
  10. Thread-2productor:我是大帅哥,我现在正在生产东西!,count:8
  11. Thread-4productor:我是大帅哥,我现在正在生产东西!,count:9
  12. Thread-6productor:我是大帅哥,我现在正在生产东西!,count:10
  13. Thread-3productor:我是大帅哥,我现在正在生产东西!,count:11
  14. Thread-5productor:我是大帅哥,我现在正在生产东西!,count:12
  15. Thread-2productor:我是大帅哥,我现在正在生产东西!,count:13
  16. Thread-4productor:我是大帅哥,我现在正在生产东西!,count:14
  17. Thread-6productor:我是大帅哥,我现在正在生产东西!,count:15
  18. Thread-3productor:我是大帅哥,我现在正在生产东西!,count:16
  19. Thread-5productor:我是大帅哥,我现在正在生产东西!,count:17
  20. Thread-2productor:我是大帅哥,我现在正在生产东西!,count:18
  21. Thread-4productor:我是大帅哥,我现在正在生产东西!,count:19

  

  4.2 消费者开始消费消息

  1. public class TestConsumer {
  2. public static void main(String[] args){
  3. Comsumer comsumer = new Comsumer();
  4. comsumer.init();
  5. TestConsumer testConsumer = new TestConsumer();
  6. new Thread(testConsumer.new ConsumerMq(comsumer)).start();
  7. new Thread(testConsumer.new ConsumerMq(comsumer)).start();
  8. new Thread(testConsumer.new ConsumerMq(comsumer)).start();
  9. new Thread(testConsumer.new ConsumerMq(comsumer)).start();
  10. }
  11.  
  12. private class ConsumerMq implements Runnable{
  13. Comsumer comsumer;
  14. public ConsumerMq(Comsumer comsumer){
  15. this.comsumer = comsumer;
  16. }
  17.  
  18. @Override
  19. public void run() {
  20. while(true){
  21. try {
  22. comsumer.getMessage("Jaycekon-MQ");
  23. Thread.sleep(10000);
  24. } catch (InterruptedException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. }
  30. }

  运行结果:

  1. INFO | Successfully connected to tcp://localhost:61616
  2. Thread-2: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:4--->0
  3. Thread-3: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:36--->1
  4. Thread-4: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:38--->2
  5. Thread-5: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:37--->3
  6. Thread-2: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:2--->4
  7. Thread-3: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:40--->5
  8. Thread-4: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:42--->6
  9. Thread-5: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:41--->7
  10. Thread-2: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:1--->8
  11. Thread-3: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:44--->9
  12. Thread-4: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:46--->10
  13. Thread-5: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:45--->11
  14. Thread-2: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:3--->12
  15. Thread-3: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:48--->13
  16. Thread-4: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:50--->14
  17. Thread-5: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:49--->15
  18. Thread-4: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:54--->16
  19. Thread-2: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:6--->17
  20. Thread-3: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:52--->18
  21. Thread-5: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:53--->19
  22. Thread-4: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:58--->20

  

  查看运行结果,我们可以做ActiveMQ 服务端:http://127.0.0.1:8161/admin/ 里面的Queues 中查看我们生产的消息。

5、ActiveMQ的特性


 5.1 ActiveMq 的特性 

  1. 多种语言和协议编写客户端。语言: Java, C, C++, C#, Ruby, Perl, Python, PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
  2. 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务)
  3. 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性
  4. 通过了常见J2EE服务器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上
  5. 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
  6. 支持通过JDBC和journal提供高速的消息持久化
  7. 从设计上保证了高性能的集群,客户端-服务器,点对点
  8. 支持Ajax
  9. 支持与Axis的整合
  10. 可以很容易得调用内嵌JMS provider,进行测试

 

 5.2 什么情况下使用ActiveMQ?

  1. 多个项目之间集成 
    (1) 跨平台 
    (2) 多语言 
    (3) 多项目
  2. 降低系统间模块的耦合度,解耦 
    (1) 软件扩展性
  3. 系统前后端隔离 
    (1) 前后端隔离,屏蔽高安全区

关于JMS(Java 消息服务) 的一些概述可以参考我的上一篇博客:http://www.cnblogs.com/jaycekon/p/6220200.html

Java消息队列--ActiveMq 初体验的更多相关文章

  1. Java消息队列--ActiveMq 实战

    1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...

  2. java消息队列--ActiveMQ

    1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...

  3. activemq学习总结 (转)Java消息队列--ActiveMq 实战

    转:https://www.cnblogs.com/jaycekon/p/6225058.html 感谢作者 ActiveMQ官网下载地址:http://activemq.apache.org/dow ...

  4. Java消息队列ActiveMQ (一)--JMS基本概念

    摘要:The Java Message Service (JMS) API is a messaging standard that allows application components bas ...

  5. Java消息队列-Spring整合ActiveMq

    1.概述 首先和大家一起回顾一下Java 消息服务,在我之前的博客<Java消息队列-JMS概述>中,我为大家分析了: 消息服务:一个中间件,用于解决两个活多个程序之间的耦合,底层由Jav ...

  6. 【阿里云产品公测】消息队列服务MQS java SDK 机器人应用初体验

    [阿里云产品公测]消息队列服务MQS java SDK 机器人应用初体验 作者:阿里云用户啊里新人   初体验 之 测评环境 由于MQS支持外网访问,因此我在本地做了一些简单测试(可能有些业余),之后 ...

  7. 消息队列ActiveMQ的使用详解

    通过上一篇文章 <消息队列深入解析>,我们已经消息队列是什么.使用消息队列的好处以及常见消息队列的简单介绍. 这一篇文章,主要带大家详细了解一下消息队列ActiveMQ的使用. 学习消息队 ...

  8. 高性能无锁队列 Disruptor 初体验

    原文地址: haifeiWu和他朋友们的博客 博客地址:www.hchstudio.cn 欢迎转载,转载请注明作者及出处,谢谢! 最近一直在研究队列的一些问题,今天楼主要分享一个高性能的队列 Disr ...

  9. java activemq初体验

    1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...

随机推荐

  1. Linux+Shell常用命令总结

    因为自己不经常使用linux的命令行工具,但是mac的终端还是经常使用的,有些命令总是要想一会或者百度一下才知道怎么用,抽时间整理了一下常用的命令,作为笔记. 常用命令 查看文件操作: ls :列出当 ...

  2. shell 脚本中执行sql

    #!/bin/bashsql='show databases' result=`mysql -uroot -pengine2user -h127.0.0.1 -e "${sql}" ...

  3. JSP 学习总结 03 核心组件 Servlet

    1 Servlet 简绍 Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生 ...

  4. EasyUI datagrid formatter 属性

    easyui的formatter属性可以帮助我们更加灵活的显示数据库中的数据. 比如,我有一个启用禁用字段,使用数字表示,1表示启用,2表示禁用,展示给客户的时候我当然希望是中文的形式. 只需要写这么 ...

  5. python print 在windows上 出现 Bad file descriptor error

    先说一下情况,一个python写的采集程序,做成windows服务在windows上运行. 这个问题出现的挺奇特,本来一套采集程序,一个采集文件的时候没问题,两个采集文件的时候也没问题,当三个采集文件 ...

  6. 用es6类封装的图片预加载技术!

    1.HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  7. SpringCloud-Eureka服务注册与发现(二)

    SpringCloud-Eureka服务注册与发现(二) https://www.cnblogs.com/qdhxhz/p/9357502.html https://blog.csdn.net/wei ...

  8. IntentService+BroadcastReceiver 实现定时任务

    效果图: AlramIntentService package com.example.admin.water; import android.app.AlarmManager;import andr ...

  9. 部署alinode监控线上应用

    参考: https://segmentfault.com/a/1190000013089124

  10. [模板] 次短路 | bzoj1726-[Usaco2006Nov]Roadblocks第二短路

    简介 所谓次短路, 顾名思义, 就是第二短路. :P 1到n的次短路长度必然产生于:1到x的最短路 + edge(x,y) + y到n的最短路 简单证明一下: 设 \(dis(i,j)\) 表示 \( ...