使用activeMQ实现jms
一:jms介绍
jms说白了就是java message service,是J2EE规范的一部分,跟jdbc差不多,sun只提供了接口,由各个厂商(provider)来进行具体的实现,然后使用者使用他们的jar包进行开发使用即可。
另外在jms的API中,jms传递消息有两种方式,一种是点对点的Queue,还有一个是发布订阅的Topic方式。区别在于:
对于Queue模式,一个发布者发布消息,下面的接收者按队列顺序接收,比如发布了10个消息,两个接收者A,B那就是A,B总共会收到10条消息,不重复。
对于Topic模式,一个发布者发布消息,有两个接收者A,B来订阅,那么发布了10条消息,A,B各收到10条消息。
关于api的简单基础可以看下:http://www.javaeye.com/topic/64707,简单的参考!
二:ActiveMQ介绍
activeMQ是apache下的一个开源jms产品,具体参见 apache官方网站;
Apache ActiveMQ is fast, supports many Cross Language Clients and Protocols , comes with easy to use Enterprise Integration Patterns and many advanced features while fully supporting JMS 1.1 and J2EE 1.4. Apache ActiveMQ is released under the Apache 2.0 License
三:开始实现代码
1:使用activeMQ来完成jms的发送,必须要下载activeMQ,然后再本机安装,并且启动activeMQ的服务才行。在官网下载完成之后,运行bin目录下面的activemq.bat,将activeMQ成功启动。
启动成功之后可以运行:http://localhost:8161/admin/index.jsp 查看一下。
2:发送端,sender
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; public class Sender {
private static final int SEND_NUMBER = 5; public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// MessageProducer:消息发送者
MessageProducer producer;
// TextMessage message;
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("test-queue");
// 得到消息生成者【发送者】
producer = session.createProducer(destination);
// 设置不持久化,可以更改
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 构造消息
sendMessage(session, producer);
session.commit(); } catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
} }
public static void sendMessage(Session session, MessageProducer producer)
throws Exception {
for (int i = 1; i <=SEND_NUMBER; i++) {
TextMessage message = session
.createTextMessage("ActiveMq 发送的消息" + i);
// 发送消息到目的地方
System.out.println("发送消息:" + i);
producer.send(message);
}
}
}
:接收端,receive
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.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; public class Receiver {
public static void main(String[] args) { // ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
MessageConsumer consumer; connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
//test-queue跟sender的保持一致,一个创建一个来接收
destination = session.createQueue("test-queue");
consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message arg0) {
System.out.println("==================");
try {
System.out.println("RECEIVE1第一个获得者:"
+ ((TextMessage) arg0).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}); MessageConsumer consumer1 = session.createConsumer(destination);
consumer1.setMessageListener(new MessageListener() {
public void onMessage(Message arg0) {
System.out.println("+++++++++++++++++++");
try {
System.out.println("RECEIVE1第二个获得者:"
+ ((TextMessage) arg0).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
});
} catch (Exception e) {
e.printStackTrace();
}
//在eclipse里运行的时候,这里不要关闭,这样就可以一直等待服务器发送了,不然就直接结束了。
// } finally {
// try {
// if (null != connection)
// connection.close();
// } catch (Throwable ignore) {
// }
// } }
}
:发送端,sender 上面的是用Queue的方式来创建,下面再用topic的方式实现同样的功能。
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic; public class TopicTest {
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
"tcp://localhost:61616"); Connection connection = factory.createConnection();
connection.start(); // 创建一个Topic
Topic topic = new ActiveMQTopic("testTopic");
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE); //if the first argument is Boolean.TRUE,can't receive jms // 注册消费者1
MessageConsumer comsumer1 = session.createConsumer(topic);
comsumer1.setMessageListener(new MessageListener() {
public void onMessage(Message m) {
try {
System.out.println("Consumer1 get "
+ ((TextMessage) m).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}); // 注册消费者2
MessageConsumer comsumer2 = session.createConsumer(topic);
comsumer2.setMessageListener(new MessageListener() {
public void onMessage(Message m) {
try {
System.out.println("Consumer2 get "
+ ((TextMessage) m).getText());
} catch (JMSException e) {
e.printStackTrace();
}
} }); // 创建一个生产者,然后发送多个消息。
MessageProducer producer = session.createProducer(topic);
for (int i = 0; i < 10; i++) {
System.out.println("producer begin produce=======");
producer.send(session.createTextMessage("Message:" + i));
}
} }
http://blog.sina.com.cn/s/blog_4b5bc0110100kb8d.html
http://blog.sina.com.cn/s/blog_4b5bc0110100kboh.html
http://blog.sina.com.cn/s/blog_4b5bc0110100kbxa.html
activeMQ密码配置(默认用户名/密码是admin/admin)
http://blog.163.com/czg_e/blog/static/4610456120133109443755/
http://blog.csdn.net/stevenprime/article/details/7091224
ActiveMQ使用的是jetty服务器, 打开conf/jetty.xml文件,找到
<bean id="securityConstraint" class="org.eclipse.jetty.http.security.Constraint">
<property name="name" value="BASIC" />
<property name="roles" value="admin" />
<property name="authenticate" value="false" />
</bean>
将property name为authenticate的属性value="false" 改为"true",
控制台的登录用户名密码保存在conf/jetty-realm.properties文件中,内容如下:
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
值得注意的是 用户名和密码的格式是
用户名 : 密码 ,角色名
使用activeMQ实现jms的更多相关文章
- ActiveMQ基本详解与总结& 消息队列-推/拉模式学习 & ActiveMQ及JMS学习
转自:https://www.cnblogs.com/Survivalist/p/8094069.html ActiveMQ基本详解与总结 基本使用可以参考https://www.cnblogs.co ...
- ActiveMQ:JMS开源框架入门介绍
介绍基本的JMS概念与开源的JMS框架ActiveMQ应用,内容涵盖一下几点: 基本的JMS概念 JMS的消息模式 介绍ActiveMQ 一个基于ActiveMQ的JMS例子程序 一:JMS基本概念 ...
- activemq和jms是种什么关系
JMS是一个用于提供消息服务的技术规范,它制定了在整个消息服务提供过程中的所有数据结构和交互流程. 而activemq则是消息队列服务,是面向消息中间件(MOM)的最终实现,是真正的服务提供者. jm ...
- 基于Tomcat + JNDI + ActiveMQ实现JMS的点对点消息传送
前言 写了一个简单的JMS例子,之所以使用JNDI 是出于通用性考虑,该例子使用JMS规范提供的通用接口,没有使用具体JMS提供者的接口,这样可以保证我们编写的程序适用于任何一种JMS实现(Activ ...
- 消息队列-推/拉模式学习 & ActiveMQ及JMS学习
一种分类是推和拉 . 还有一种分类是 Queue 和 Pub/Sub . 先看的这一篇:http://blog.csdn.net/heyutao007/article/details/50131089 ...
- ActiveMQ中JMS的可靠性机制
全文用到的生产者代码: package cn.qlq.activemq; import javax.jms.Connection; import javax.jms.ConnectionFactory ...
- 使用 ActiveMQ 实现JMS 异步调用
目录 简介 启动 ActiveMQ 服务器 查看控制台 ActiveMQ 的消息通道 Queue Topic 比较 开发生产者和消费者 开发服务端(消费者) 开发客户端(生产者) 参考 简介 服务之间 ...
- ActiveMQ基于JMS的pub/sub传播机制
原文地址:[ActiveMQ实战]基于JMS的pub/sub传播机制 发布订阅模型 就像订阅报纸,我们可以选择一份或者多份报纸.比如:北京日报.人民日报.这些报纸就相当于发布订阅模型中的topic.如 ...
- ActiveMQ (二) JMS入门
JMS入门 前提:安装好了ActiveMQ ActiveMQ安装 Demo结构: 首先pom.xml引入依赖: <dependency> <groupId>org.apach ...
随机推荐
- VS2008 ctrl+shift+F热键冲突
今天发现有时VS 热键好使,有时,怎么按都没有反应. 后来发现是和输入法有冲突,把输入法快捷键改了就好了.
- 第一章 工欲善其事 必先利其器—Android SDK工具(3)
1.3没有真机一样开发--Android模拟器 有些时候,我们手头上可能并没有符合要求的Android设备.那么这时候我们是不是对调试或者开发就一筹莫展了呢?当然不是.由于我们有Android模拟器. ...
- 使用EMOJI表情
因为IOS系统支持日文中的字块编码,所以在UILable,UITextField,UIAlertView等控件中使用emoji表情编码(emoji就是表情符号:词义来自日语(えもじ,e-moji,mo ...
- Linux应用程序打包
原文地址:http://blog.solrex.cn/articles/packaging-1-src.html1. 应用程序打包技术之一(源代码篇) 相信很多朋友都曾经为方便做某件事写过自己的小 ...
- 在top命令下kill和renice进程
For common process management tasks, top is so great because it gives an overview of the most active ...
- nyoj 756 重建二叉树
重建二叉树主要是给你一颗二叉树的前序遍历的结果和中序遍历的结果或者后序遍历的结果或者中序遍历的结果,让你求出其中的后序遍历的结果或者前序遍历的结果,这里知道其中的两个就能求出第三个,但是知道的两个必须 ...
- python面对对象编程------4:类基本的特殊方法__str__,__repr__,__hash__,__new__,__bool__,6大比较方法
一:string相关:__str__(),__repr__(),__format__() str方法更面向人类阅读,print()使用的就是str repr方法更面对python,目标是希望生成一个放 ...
- HTML <center> 标签
浏览器支持 定义和用法所有浏览器都支持 <center> 标签. 对其所包括的文本进行水平居中. HTML 与 XHTML 之间的差异 在 HTML 4.01 中,center 元素不被赞 ...
- Jquery.Sorttable 桌面拖拽自定义
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 使用VS2012的C++生成dll
1,首先,通过File->New Project的方式新建一个工程,打开"New Project"对话框. 2,选择Visual C++语言下的 Win32->Win3 ...