将WebLogic MDB转换为Spring消息驱动的POJO

时间:2014-08-19 18:21:22

标签: spring ejb weblogic11g mdp

我有一个使用MDB构建在WebLogic 11b(10.3.4)上的应用程序。我正在尝试将这些转换为Spring MDP。以下是有问题的MDB:

@MessageDriven(activationConfig = {
        @ActivationConfigProperty(propertyName = "ejbName", propertyValue = "RecipientEventRouterBean"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable") })
public class RecipientEventRouterBean extends TraxMessageRouter {

    @Resource
    private int appInstance;

    @Resource
    private String appName;

    @Resource(mappedName = "jms/TraxErrorQ")
    private Queue errorQueue;

    @Resource(name = "TraxCF")
    private ConnectionFactory errorQueueConnectionFactory;

    @EJB
    private Logger loggingService;

    @Resource
    private int maxJMSXDeliveryCount;

    @Resource
    private boolean validate;

    @EJB
    protected RecipientRegistrationService recService;

    @Override
    @PostConstruct
    public void init() {

        if (appName == null) {
            throw new EJBException("appName is null");
        }

        try {
            super.traxFactory = TraxFactory.getInstance(Application.fromValue(appName));
            super.processor = new ObjectFactory().createApplicationInstance();
            super.processor.setName(appName);
            super.processor.setInstance(this.appInstance);
            super.errorQueueConnectionFactory = this.errorQueueConnectionFactory;
            super.errorQueue = this.errorQueue;
            super.maxJMSXDeliveryCount = this.maxJMSXDeliveryCount;
            super.validate = this.validate;
            super.loggingService = this.loggingService;
        } catch (Exception e) {
            throw new EJBException(e);
        }

        super.init();
    }

    @Override
    protected TraxMessageService getTraxMessageService() {

        return recService;
    }

}

MDB的父级TraxMessageRouter实现了MessageListener接口。

我在jms-applicationContext.xml中创建了MDP bean:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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-3.0.xsd">

    <!-- Spring JMS Destination Resolver -->
    <bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.JndiDestinationResolver">
        <property name="cache" value="true" />
    </bean>


    <!-- Spring JMS Queue Connection Factory leveraging a single connection -->
    <!-- SingleConnectionFactory will return the same Connection on all createConnection calls and ignore calls to close -->
    <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jms/TraxCF" />
    </bean>

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destinationResolver" ref="jmsDestinationResolver" />
    </bean>

    <bean id="jmsSender" class="com.mycompany.web.service.jms.JMSSender">
        <property name="jmsTemplate" ref="jmsTemplate" />
    </bean>

    <bean id="jmsReceiver" class="com.mycompany.web.service.jms.JMSReceiver">
        <property name="jmsTemplate" ref="jmsTemplate" />
    </bean> 

    <bean id="jmsQueue" class="javax.jms.Topic" />  

    <!-- this is the Message Driven POJO (MDP) -->
    <bean id="messageListener" class="com.mycompany.messaging.RecipientEventRouterBean" />

    <!-- and this is the message listener container -->
    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destination" ref="jmsQueue"/>
        <property name="messageListener" ref="messageListener" />
    </bean>
</beans>

如何将@MessageDriven注释转换为正确的Spring语法?我现在可以评论一下吗?

我还有几个EventRouterBeans。他们都在jms-ApplicationContext.xml中获取bean声明,还是可以将父TraxMessageRouter添加到xml?

需要对配置文件进行哪些其他更改?有关如何从MDB过渡到MDP的良好指南吗?有关如何从EJB 3过渡到Spring的良好指南吗?

1 个答案:

答案 0 :(得分:1)

对我来说几乎没问题。以下是一些提示:

  • 是的,您可以删除@MessageDriven注释,因为您已经配置了容器
  • 你的bean jmsQueue错了。当您使用weblogic时,您应该使用JNDI找到队列(就像您为连接工厂所做的那样),或者您应该使用destinationName属性(接受{{1)来提供队列的(jndi)名称。而不是String

每个要部署的消息侦听器需要一个声明。也许您应该使用xml命名空间路由来共享这些设置?

javax.jms.Destination

这将创建两个侦听器容器(即每个<jms:listener-container connectionFactory="connectionFactory" destinationResolver="jmsDestinationResolver"> <jms:listener ... /> <jms:listener ... /> </jms:listener-container> 条目一个容器)。

另请注意,Spring 4.1将提供更好的声明性方法,您可以在其中注释任何Spring托管bean的方法。有关详细信息,请查看this blog post。 4.1.0.RC2已经发布,所以你现在可以尝试一下。