利用Spring对消息驱动Bean的支持开发EJB2的消息驱动Bean

第1步
开发基于POJI的POJO的业务实现,最基本的我们经常那么干的

第2步
使用Spring的xml解耦合这些POJO实现
产生applicationContext-datasource.xml 等

第3步
消息驱动Bean开发继承自org.springframework.ejb.support.AbstractMessageDrivenBean
给消息驱动Bean注入第二步的Beanfactory,在onMessage(Message message)中调用beanFactory中的
POJO实现业务处理,消息驱动Bean是Spring工厂的消费类。

第4步
部署消息驱动Bean,消息驱动Bean是JMS的消息处理Bean

第5步
书写特定容器的配置文件绑定JNDI

附开发实例文件如下

weblogic-ejb-jar.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE weblogic-ejb-jar PUBLIC “-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN” “http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd”>
<weblogic-ejb-jar>
<description>Test</description>
<weblogic-enterprise-bean>
<ejb-name>MyMessageDrivenBean</ejb-name>
<message-driven-descriptor>
<pool>
<max-beans-in-free-pool>8</max-beans-in-free-pool>
<initial-beans-in-free-pool>1</initial-beans-in-free-pool>
</pool>
<destination-jndi-name>jms/DestMyMessageQueue</destination-jndi-name>
<initial-context-factory>weblogic.jndi.WLInitialContextFactory</initial-context-factory>
<provider-url>t3://localhost:7001</provider-url>
<!– 如果JMS和EJB容器不在一台机器上指定该JMS provider-url–>
<connection-factory-jndi-name>jms/MyMessageDrivenBean</connection-factory-jndi-name>
<jms-polling-interval-seconds>10</jms-polling-interval-seconds>
<!–jms-client-id>myMessageDrivenBean</jms-client-id–>
</message-driven-descriptor>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>

META-INF/ejb-jar.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE ejb-jar PUBLIC “-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN” “http://java.sun.com/dtd/ejb-jar_2_0.dtd”>
<ejb-jar id=”ejb-jar_1″>
<description>ejb</description>
<display-name>ejb</display-name>
<enterprise-beans>
<message-driven id=”MessageDriven_1″>
<description>MyMessageDrivenBean</description>
<display-name>Name for MyMessageDrivenBean</display-name>
<ejb-name>MyMessageDrivenBean</ejb-name>
<ejb-class>service.MyMessageDrivenBean</ejb-class>
<transaction-type>Bean</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
</message-driven>
</enterprise-beans>
<assembly-descriptor id=”AssemblyDescriptor_1″>
</assembly-descriptor>
</ejb-jar>

消息驱动Bean加载所需要的POJObeanFactory的Bean资源
beanRefContext.xml

<bean id=”businessBeanFactory”
>
<constructor-arg>
<list>
<value>applicationContext-datasource.xml</value>
<value>applicationContext.xml</value>
<value>applicationContext-service.xml</value>
</list>
</constructor-arg>
</bean>

消息驱动Bean
MyMessageDrivenBean.java

package service;

import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
import org.springframework.ejb.support.AbstractMessageDrivenBean;

public class MyMessageDrivenBean extends AbstractMessageDrivenBean implements
MessageDrivenBean, MessageListener {

private static final long serialVersionUID = 7819604050800044554L;

private static Log log = LogFactory.getLog(MyMessageDrivenBean.class); // log

/**
* for spring inject this property
*/
private MessageHandelService messageHandelService;

public MessageHandelService getMessageHandelService() {
return messageHandelService;
}

public void setMessageHandelService(
MessageHandelService messageHandelService) {
this.messageHandelService = messageHandelService;
}

protected void onEjbCreate() {
this.setMessageHandelService((MessageHandelService) getBeanFactory()
.getBean(“messageHandelService”));
/**
* 从spring 工厂bean中得到需要的业务实现,业务实现是基于POJI的POJO实现
* 本身消息驱动Bean只负责暴露接口JNDI来接受JMS的Queue并调用POJI的POJO实现处理业务逻辑
* 就是真正的业务逻辑是写在POJO中实现的 消息驱动Bean相对于Spring来说是Bean消费者
*/
}

public void onMessage(Message message) {

if (message instanceof ObjectMessage) { // 如果接受的消息是序列化后的对象,直接强制转换
ObjectMessage objMessage = (ObjectMessage) message;
MyDomain model = null;
try {
model = (MyDomain) objMessage.getObject();
} catch (JMSException e) {
e.printStackTrace();
}
}
// 业务处理调用接口
this.getMessageHandelService().doFoo();

if (message instanceof TextMessage) {

}

}

public void setMessageDrivenContext(
MessageDrivenContext messageDrivenContext) {
super.setMessageDrivenContext(messageDrivenContext);
setBeanFactoryLocator(ContextSingletonBeanFactoryLocator.getInstance());// 指定默认的beanRefContext.xml
setBeanFactoryLocatorKey(“businessBeanFactory”);
}

}

原创文章,转载请注明: 转载自海波无痕

本文链接地址: 利用Spring对消息驱动Bean的支持开发EJB2的消息驱动Bean

文章的脚注信息由WordPress的wp-posturl插件自动生成

此条目发表在javaee, spring, weblogic分类目录,贴了, , , , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论链接可以 移除 nofollow.