日期:2014-05-16  浏览次数:20813 次

Apache apusic---ActiveMQ中间件在JMS1.1、J2EE中的使用

ActiveMQ中间件在JMS1.1、J2EE中的使用

Apache的中间件---ActiveMQ对于企业级的应用很广泛。支持JMS1.1、J2EE1.4以上版本,可运行于JVM和大部分web容器中、支持多种语言客户端(Java、C、C++,ActionScript、Ajax等)、支持多种协议(stomp、openWire等)、对spring的支持也很良好、速度很快是JbossMQ的十倍、与openJMS、JbossMQ等开源框架相比,ActiveMQ有Apache的支持可以持续发展。
启动ActiveMQ的后台管理为:http://localhost:8161/admin/(登陆时候默认的user and password is admin/admin)
Queue(Point-to-Point)与Topic(publisher/Subscriber)的比较
1:jms queue执行load balancer语义
一条消息仅仅能够被一个consumer收到。如果在message发送的时候没有可用的consumer,那么它将会被保存到处理该message的consumer可用。如果一个consumer收到一条message后却不响应它,那么这条消息将被转移到另外一个consumer那儿。一个Queue可以有很多的consumer,并且在多个可用的consumer中负载均衡。
2:Topic实现publish和subscribe语义
一条消息被publish时,它将发送到所用感兴趣的订阅者,所以零到多个subscribe将被接受到消息的一个拷贝,但是在消息代理接收到消息的时,只有激活订阅的subscribe能够获得消息的一个拷贝。
下面为程序的实现例子:对java的支持
producer(sender)消息生产对象
package com.clark.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
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  = null;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
//Session:one sender or receiver message singleton thread and provide a transaction context
Session session = null;
/**
* Destination:message destination,sender who
* it include two message domain:Point-to-Point(Destinational call queue)
* and Publisher/Sucscribler Model(Destinational call topic)
* Point-To-Point character:one message can only one consumer
* between message produer and consumer have no relationship.No matter consumer whether is running
* status when producer send message,It can receiver message
* Publisher/Subscribler:one message has many consumer
* producer and consumer have relationship.Subscribler one topic consumer can only