ActiveMQ部署及使用

2010-03-06 23:37:30 作者:jezz 资深编辑 有2128人浏览 网友评论 1
JMS源于企业应用对于消息中间件的需求,使应用程序可以通过消息进行异步处理而互不影响。Sun公司和它的合作伙伴设计的JMS API定义了一组公共的应用程序接口和相应语法,使得Java程序能够和其他消息组件进行通信。JMS有四个组成部分:JMS服务提供者、消息管理对象、消息的生产者消费者和消息本身。

1)JMS服务提供者实现消息队列和通知,同时实现消息管理的API。JMS已经是J2EE API的一部分,J2EE服务器都提供JMS服务。
2) 消息管理对象提供对消息进行操作的API。JMS API中有两个消息管理对象:创建jms连接使用的工厂(ConnectionFactory)和目的地(Destination),根据消息的消费方式的不同ConnectionFactory可以分为QueueConnectionFactory和TopicConnectionFactory,目的地(Destination)可以分为队列(Queue)和主题(Topic)两种。
3)消息的生产者和消费者。消息的产生由JMS的客户端完成,JMS服务提供者负责管理这些消息,消息的消费者可以接收消息。消息的生产者可以分为――点对点消息发布者(P2P)和主题消息发布者(TopicPublisher)。所以,消息的消费者分为两类:主题消息的订阅者(TopicSubscriber)和点对点消息的接收者(queue receiver)
4)消息。消息是服务提供者和客户端之间传递信息所使用的信息单元。JMS消息由以下三部分组成:
  消息头(header)――JMS消息头包含了许多字段,它们是消息发送后由JMS提供者或消息发送者产生,用来表示消息、设置优先权和失效时间等等,并且为消息确定路由。
  属性(property)――用来添加删除消息头以外的附加信息。
  消息体(body)――JMS中定义了5种消息体:ByteMessage、MapMessage、ObjectMessage、StreamMessage和TextMessage。

2.为什么选用ActiveMQ
1)ActiveMQ是一个开放源码
2)基于Apache 2.0 licenced 发布并实现了JMS 1.1。
3)ActiveMQ现在已经和作为很多项目的异步消息通信核心了
4)在很多中小型项目中采用ActiveMQ+SPRING+TOMCAT开发模式。

3.下载启动ActiveMQ
我这里只讨论在Linux下的部署和操作,在windows里面更简单,操作如下:
tar zxvf activemq-x.x.x.tar.gz
tar zxvf activemq-x.x.x-src.tar.gz
cd [activemq_install_dir]/bin
chmod 755 activemq
cd [activemq_install_dir]
//后台启动activeMQ,远程退出后程序将继续运行,其他方式退出时程序将终止运行
nohup bin/activemq > /tmp/smlog 2>&1 &

这里要说下怎么关掉activeMQ,它没有提供shell脚本,方法稍微麻烦点,方法如下:
netstat -an|find "61616"
kill -9 pid

基本使用
Spring对JMS封装的比较好,使用起来比较方便,所以下面的使用都是基于Spring。

发送消息代码:
package cn.wangmeng.mq;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.JmsTemplate102;
import org.springframework.jms.core.MessageCreator;

public class JmsQueueSender {

	private JmsTemplate jmsTemplate;
	private Queue queue;
	
	public void setQueue(Queue queue) {
		this.queue = queue;
	}
	
	public void setConnectionFactory(ConnectionFactory cf){
		this.jmsTemplate=new JmsTemplate102(cf,false);
	}
	
	public void send(){
		this.jmsTemplate.send(this.queue, new MessageCreator(){
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage("hello");
			}
		});
	}
	
	
}

监听消息代码:
package cn.wangmeng.mq;

import javax.jms.Message;
import javax.jms.MessageListener;

public class JmsListen implements MessageListener {

	public void onMessage(Message message) {
//TODO
	}

}

Spring的配置文件:
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
    </bean>
    <bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="WM.EMAIL"/>
    </bean>
    
    <bean id="sender" class="cn.wangmeng.mq.JmsQueueSender">
    	<property name="connectionFactory" ref="connectionFactory"></property>
    	<property name="queue" ref="queue"></property>
    </bean>
    
    <bean id="jmsListen" class="cn.wangmeng.mq.JmsListen"></bean>
    
    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
	    <property name="connectionFactory" ref="connectionFactory"></property>
    	<property name="destination" ref="queue"></property>
    	<property name="messageListener" ref="jmsListen" />
    	<!--<property name="concurrentConsumers" value="3" />-->
    	<property name="sessionTransacted" value="true"/>
    </bean>

  • 共有 1 位网友发表了评论
  • 1 楼 网友:116.232.*.* 于 2010-03-06 23:43:39 评论道: 引用
  • 不错,听说ACTIVEMQ很强劲,不过大并发量并没有测试过,不过听说消息持久多了,然后重新启动后有点慢
看不清,换一张请输入验证码: (登录注册用户不用填验证码)

热门新闻

  • 没有热门新闻

热评推荐

  • 没有热评推荐
网盟新闻广告