apache ActiveMQ之初体验
版权声明: https://blog.csdn.net/zdp072/article/details/27237549
一. 开篇语
继上一篇weblogic中使用jms发送和接受消息的文章后, 本文使用apache的一个开源组件ActiveMQ接着探讨JMS的话题, 本篇仅仅是ActiveMQ的一个入门的样例,
希望对您有所帮助.
二. ActiveMQ
1. ActiveMQ简单介绍:
ActiveMQ是Apache的一个能力强劲的开源消息总线, 它全然支持JMS1.1和JavaEE1.4规范的JMS Provider实现.
2. ActiveMQ特性:
1.) 多种语言和协议编写client。语言: 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能够非常easy内嵌到使用Spring的系统里面去,并且也支持Spring2.0的特性
4.) 通过了常见J2EE服务器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的測试,当中通过JCA 1.5 resourceadaptors的配置,
能够让ActiveMQ能够自己主动的部署到不论什么兼容J2EE1.4商业服务器上
5.) 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
6.) 支持通过JDBC和journal提供快速的消息持久化
7.) 从设计上保证了高性能的集群,client-服务器,点对点
8.) 支持Ajax
9.) 支持与Axis的整合
10.) 能够非常easy得调用内嵌JMS provider,进行測试
3. 环境准备:
1.) 下载ActiveMQ:
http://activemq.apache.org/download.html, 我下载的是apache-activemq-5.2.0
2.) 执行ActiveMQ server
解压缩下载好的文件, 双击bin/activemq.bat 启动server, ActiveMQ内置了jetty服务器, 默认使用TCP连接port为61616.
ActiveMQ提供一个用于监控ActiveMQ的admin应用: http://127.0.0.1:8161/admin
3.) 在Eclipse中建立Javaproject, 并导入activemq-all-5.2.0.jar包
4.) 新建两个Java类: 消息生产者MsgSender和消息消费者MsgReceiver
4. 代码測试(P2P):
1.) 消息生产者: MsgSender
/**
* Message Provider
*/
public class MsgSender {
// ConnectionFactory: use to create JMS connection
private static ConnectionFactory connectionFactory;
// Connection: connect message provider and JMS server
private static Connection connection;
// Session: a message send or receive thread
private static Session session;
// Destination: use to sign the message type
private static Destination destination;
// MessageProducer:sender
private static MessageProducer messageProducer;
/**
* init the JMS object
*/
public static void init() throws Exception {
// use ActiveMQ to to create connection factory.
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
// get the connection from connection factory
connection = connectionFactory.createConnection();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("myQueue");
messageProducer = session.createProducer(destination);
messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
connection.start();
}
/**
* send activeMq message
*/
public static void sendMessage() throws Exception {
for (int i = 1; i <= 5; i++) {
TextMessage message = session.createTextMessage("ActiveMq message " + i);
System.out.println("send:" + "ActiveMq message " + i);
messageProducer.send(message);
}
session.commit();
}
/**
* release resource
*/
public static void release() throws Exception {
messageProducer.close();
session.close();
connection.close();
}
/**
* main method
*/
public static void main(String[] args) throws Exception {
init();
sendMessage();
release();
}
}
2.) 消息消费者: MsgReceiver
/**
* Message Consumer
*/
public class MsgReceiver {
// ConnectionFactory: use to create JMS connection
private static ConnectionFactory connectionFactory;
// Connection: connect message provider and JMS server
private static Connection connection;
// Session: a message send or receive thread
private static Session session;
// use to sign the message type
private static Destination destination;
// MessageConsumer: receiver
private static MessageConsumer messageConsumer;
/**
* init the JMS object
*/
public static void init() throws Exception {
// use ActiveMQ to to create connection factory.
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
// get the connection from connection factory
connection = connectionFactory.createConnection();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("myQueue");
messageConsumer = session.createConsumer(destination);
connection.start();
}
/**
* receive activeMq message
*/
public static void receiveMessage() throws Exception {
while (true) {
TextMessage message = (TextMessage) messageConsumer.receive();
if (message != null) {
System.out.println("receive: " + message.getText());
} else {
break;
}
}
}
/**
* release resource
*/
public static void release() throws Exception {
messageConsumer.close();
session.close();
connection.close();
}
/**
* main method
*/
public static void main(String[] args) throws Exception {
init();
receiveMessage();
release();
}
}
3.) 源代码下载地址: http://download.csdn.net/detail/zdp072/7422401
apache ActiveMQ之初体验的更多相关文章
- JMS服务器ActiveMQ的初体验并持久化消息到MySQL数据库中
JMS服务器ActiveMQ的初体验并持久化消息到MySQL数据库中 一.JMS的理解JMS(Java Message Service)是jcp组织02-03年定义了jsr914规范(http://j ...
- Apache Beam入门及Java SDK开发初体验
1 什么是Apache Beam Apache Beam是一个开源的统一的大数据编程模型,它本身并不提供执行引擎,而是支持各种平台如GCP Dataflow.Spark.Flink等.通过Apache ...
- Spring之初体验
Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...
- Flume日志采集系统——初体验(Logstash对比版)
这两天看了一下Flume的开发文档,并且体验了下Flume的使用. 本文就从如下的几个方面讲述下我的使用心得: 初体验--与Logstash的对比 安装部署 启动教程 参数与实例分析 Flume初体验 ...
- Flume 实战(1) -- 初体验
前言: Flume-ng是数据收集/聚合/传输的组件, Flume-ng抛弃了Flume OG原本繁重的zookeeper和Master, Collector, 其整体的架构更加的简洁和明了. 其基础 ...
- (一)SpringBoot基础篇- 介绍及HelloWorld初体验
1.SpringBoot介绍: 根据官方SpringBoot文档描述,BUILD ANYTHING WITH SPRING BOOT (用SPRING BOOT构建任何东西,很牛X呀!),下面是官方文 ...
- JAVA 11初体验
JAVA 11初体验 随着JAVA没半年发布一次新版本,前几天JAVA 11隆重登场.在JAVA 11中,增加了一些新的特性和api, 同时也删除了一些特性和api,还有一些性能和垃圾回收的改进. 作 ...
- spring cloud 初体验
spring cloud分为注册端.客户端以及消费端 初体验的理解就是: 注册端就是将之前所有的应用在这边进行注册,然后给每个应用都生成自己的标识,这些应用就是来自于客户端,消费端则通过调用注册端(有 ...
- 【Spark深入学习 -15】Spark Streaming前奏-Kafka初体验
----本节内容------- 1.Kafka基础概念 1.1 出世背景 1.2 基本原理 1.2.1.前置知识 1.2.2.架构和原理 1.2.3.基本概念 1.2.4.kafka特点 2.Kafk ...
随机推荐
- Node.js学习笔记(八) --- Node.js的路由模块封装
1 .模块化的方式封装 整理中… 2 .封装仿照 express 的路由整理中…
- .net core 2.2 部署CentOS7(4)CentOS7下载并安装.NET SDK(软件开发工具包)
目录: .net core 2.2 部署CentOS7(1)安装虚拟机 .net core 2.2 部署CentOS7(2)给虚拟机安装CentOS7 .net core 2.2 部署CentOS7( ...
- Linux的虚拟内存管理-如何分配和释放内存,以提高服务器在高并发情况下的性能,从而降低了系统的负载
Linux的虚拟内存管理有几个关键概念: Linux 虚拟地址空间如何分布?malloc和free是如何分配和释放内存?如何查看堆内内存的碎片情况?既然堆内内存brk和sbrk不能直接释放,为什么不全 ...
- 山东第四届省赛C题: A^X mod P
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3232 Problem C:A^X mod P Time Limit: 5 Sec Memor ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- element ui 时间控件 多个日期
前言:工作中用到 vue+element ui 的前端框架,需要使用时间控件来选择多个日期,已月日的形式,且有默认值,所以记录一下.转载请注明出处:https://www.cnblogs.com/yu ...
- Linux安装配置mysql
1.假设已经有mysql-5.5.10.tar.gz以及cmake-2.8.4.tar.gz两个源文件 (1)先安装cmake(mysql5.5以后是通过cmake来编译的) [root@ rhel5 ...
- HTTPS 常见部署问题及解决方案
在最近几年里,我写了很多有关 HTTPS 和 HTTP/2 的文章,涵盖了证书申请.Nginx 编译及配置.性能优化等方方面面.在这些文章的评论中,不少读者提出了各种各样的问题,我的邮箱也经常收到类似 ...
- angr 学习笔记
前言 angr 是一个基于 符号执行 和 模拟执行 的二进制框架,可以用在很多的场景,比如逆向分析,漏洞挖掘等.本文对他的学习做一个总结. 安装 这里介绍 ubuntu 下的安装,其他平台可以看 官方 ...
- ubuntu 命令、linux环境变量设置
解压与压缩: tar.gz格式tar -xzvf xxx jar格式jar -xvf xxx.jar zip格式unzip xxx.zip zip -r xxx.zip xxx unarunar -e ...