spring3.0和JBoss5.1的JMS集成

参考 http://blog.csdn.net/liuzhenwen/article/details/3768766

注意:Entity中有个Transient标注的属性无法序列化到queue中,可以用一个DTO重新组装对象传送。

定我们的Queue文件adServerLog-service.xml

<?xml version="1.0" encoding="UTF-8"?>  
<server>  
    <mbean code="org.jboss.mq.server.jmx.Queue"  
           name="jboss.mq.destination:service=Queue,name=adServerQueue">  
        <attribute name="JNDIName">queue/adServerQueue</attribute>  
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>      
    </mbean>  
</server>  
 

applicationContext.xml的配置

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

	<!-- This will automatically locate any and all property files you have 
		within your classpath, provided they fall under the META-INF/spring directory. 
		The located property files are parsed and their values can then be used within 
		application context files in the form of ${propertyKey}. -->
	<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
	<context:spring-configured />

	<context:component-scan base-package="com.mogenesis">
		<context:exclude-filter expression="org.springframework.stereotype.Controller"
			type="annotation" />
	</context:component-scan>

	<bean class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close" id="dataSource">
		<property name="driverClassName" value="${database.driverClassName}" />
		<property name="url" value="${database.url}" />
		<property name="username" value="${database.username}" />
		<property name="password" value="${database.password}" />
	</bean>

	<bean class="org.springframework.orm.jpa.JpaTransactionManager"
		id="transactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>

	<tx:annotation-driven transaction-manager="transactionManager" />
	<bean
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
		id="entityManagerFactory">
		<property name="dataSource" ref="dataSource" />
		<property name="persistenceXmlLocation" value="classpath*:/META-INF/persistence-spring.xml" />
		<property name="loadTimeWeaver">
			<bean
				class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
		</property>
	</bean>

	<!-- JMS configure -->
	<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
		<property name="environment">
			<props>
				<prop key="java.naming.factory.initial">
					org.jnp.interfaces.NamingContextFactory
				</prop>
				<prop key="java.naming.provider.url">localhost:1099</prop>
				<prop key="java.naming.factory.url.pkgs">
					org.jnp.interfaces:org.jboss.naming
	           </prop>
			</props>
		</property>
	</bean>

	<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiTemplate">
			<ref bean="jndiTemplate" />
		</property>
		<property name="jndiName">
			<value>XAConnectionFactory</value>
		</property>
	</bean>

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

		<!-- true for Topic false for Queue -->
		<property name="pubSubDomain">
			<value>false</value>
		</property>
		<!-- message waiting time(ms) -->
		<property name="receiveTimeout">
			<value>30000</value>
		</property>
	</bean>
	<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiTemplate">
			<ref bean="jndiTemplate" />
		</property>
		<property name="jndiName">
			<value>queue/adServerQueue</value>
		</property>
	</bean>

	<!-- and this is the message listener container -->
	<bean id="jmsContainer"
		class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="jmsConnectionFactory" />
		<property name="destination" ref="destination" />
		<!-- 监听队列的bean声明在注解之中 -->
		<property name="messageListener" ref="adServerJmsQueueListener" />
	</bean>
</beans>
 

服务端监听类AdServerJmsQueueListener.java

扫描二维码关注公众号,回复: 777973 查看本文章
package com.mogenesis.mobileadplatform.adserver.log;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mogenesis.mobileadplatform.domain.AdRequest;
import com.mogenesis.mobileadplatform.service.adserver.AdMetricsService;

@Service("adServerJmsQueueListener")
public class AdServerJmsQueueListener implements MessageListener {

	@Resource
	private JmsTemplate jmsTemplate;

	@Resource
	private AdMetricsService adMetricsService;

	public void onMessage(Message message) {
		ObjectMessage objMessage = (ObjectMessage) message;
		try {
			AdRequest adRequest = (AdRequest) objMessage.getObject();
			adMetricsService.processAdMetrics(adRequest);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
 

客户端操作类AdServerJmsQueueSender.java

package com.mogenesis.mobileadplatform.adserver.log;
import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

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

import com.mogenesis.mobileadplatform.domain.AdRequest;

@Component
public class AdServerJmsQueueSender {

	@Resource
	private JmsTemplate jmsTemplate;

	public void sendMessage(final AdRequest adRequest) {
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				ObjectMessage objMessage = session.createObjectMessage();
				objMessage.setObject(adRequest);
				return objMessage;
			}
		});
	}
}

  在我们需要的组件类中添加

@Resource private AdServerJmsQueueSender adServerJmsQueueSender;

就可以使用了。

其中项目jms.jar包和jboss的jms.jar包冲突,所以我把项目中pom.xml文件为,编译的时候使用,次jar包部署的时候不用打包进项目

<dependency>
			<groupId>jms</groupId>
			<artifactId>jms</artifactId>
			<version>1.1</version>
			<scope>provided</scope>
		</dependency>

猜你喜欢

转载自xiaofancn.iteye.com/blog/1183363