`

ActiveMQ 与Spring结合示例

阅读更多

    公司用过基于websphere上的JMS的应用,主要用的是QUEUE,关于topic的还没有用到过,但是,国内许多公司用的开源的比较多一点,因为成本比较低,现阶段是SSH成风,所以spring的JMS(MQ)的应用是要学习一下,今天学习的是spring上应用activeMQ,都是开源的,呵呵.

   下面列一下步骤:

   1) ActiveMQ的启动.

   启动比较简单,下载ActiveMQ, 进入bin目录,启动activemq.bat.就可以了, 端口是8161.

   2) 可以访问http://localhost:8161/admin进入activeMQ的console,控制台中可以手动添加和删除message, 比较方便.

  3) 服务器配置   

   服务器用的是tomcat,主要配置是设置activeMQ的 factory与queue名称.

  在 x:\tomcat-5.5\conf\context.xml 设制相关jndi

 

<?xml version="1.0" encoding="UTF-8"?>
<!--
  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.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
	
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
<Resource name="jms/ConnectionFactory"    
  auth="Container"      
  type="org.apache.activemq.ActiveMQConnectionFactory"    
  description="JMS Connection Factory"  
  factory="org.apache.activemq.jndi.JNDIReferenceFactory"    
  brokerURL="tcp://localhost:61616"    
  brokerName="LocalActiveMQBroker"/>  
    
<Resource name="jms/Queue"    
auth="Container"    
type="org.apache.activemq.command.ActiveMQQueue"  
description="my Queue"  
factory="org.apache.activemq.jndi.JNDIReferenceFactory"    
physicalName="TOOL.DEFAULT"/>   

</Context>

 这些基本上是比较死的东西, physicalName是指queue的名字,我用的是activeMQ的初始就有的一个。

 

spring中对activeMQ的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:amq="http://activemq.apache.org/schema/core"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core 
  http://activemq.apache.org/schema/core/activemq-core.xsd">

	<bean id="jmsConnectionFactory"
		class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property>
	</bean>

	<bean id="jmsQueue"
		class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="java:comp/env/jms/Queue"></property>
	</bean>

	<bean id="jmsTemplate"
		class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="jmsConnectionFactory"></property>
		<property name="defaultDestination" ref="jmsQueue"></property>
	</bean>

	<bean id="sender" class="message.Sender">
		<property name="jmsTemplate" ref="jmsTemplate"></property>
	</bean>

	<bean id="receive" class="message.Receiver"></bean>
	<bean id="listenerContainer"
		class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="jmsConnectionFactory"></property>
		<property name="destination" ref="jmsQueue"></property>
		<property name="messageListener" ref="receive"></property>
	</bean>


</beans>

 

   sender与receiver的代码

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

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


public class Sender {

	private JmsTemplate jmsTemplate;

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
	
	public void send(final String text){
		System.out.println("---Send:"+text);
		jmsTemplate.send(new MessageCreator(){

			public Message createMessage(Session arg0) throws JMSException {
				// TODO Auto-generated method stub
				return arg0.createTextMessage(text);
			}
			
		});
	}

}

 

package message;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class Receiver implements MessageListener {

	public void onMessage(Message message) {
		if (message instanceof TextMessage) {
			TextMessage text = (TextMessage) message;

			try {
				System.out.println("Receive:" + text.getText());
			} catch (JMSException e) {
				
				e.printStackTrace();
			}

		}
	}
}

 主要测试了对MQ监听的功能,也就是MessageListener ,当程序启动后手动的在console里面添加了几条消息,可以监听到,另外ActiveMQ里的例子里面还有发送与消费的工具,比较好用,ConsumerTool和ProducerTool.

 

附件有个可以跑的例子.

分享到:
评论
1 楼 xuehanxin 2011-08-26  
楼主乃神人也,崇拜中

相关推荐

Global site tag (gtag.js) - Google Analytics