用于JMS DAO的PersistenceExceptionTranslationPostProcessor

时间:2013-01-07 15:37:06

标签: spring exception-handling jms data-access-layer spring-jms

我遇到了Spring的PersistenceExceptionTranslationPostProcessor,这看起来很完美,因为它抽象了用@Repository注释的DAO中抛出的异常。

现在我有一个应用程序使用JMS(ActiveMQ)而不是数据库作为后端。我想使用PersistenceExceptionTranslationPostProcessor之类的内容将JMSException翻译成Spring的DataAccessException

在我去重新发明轮子之前,我在网上搜索了这样的东西,但没有找到它。也许我使用了错误的搜索键,所以作为第二次尝试,有没有人知道现有的东西,或者我是否必须发明这个轮子?


更新

我似乎必须自己创建一个PersistenceExceptionTranslator。我做了以下事情:

在我的抽象JMS DAO上实现了PersistenceExceptionTranslator

public abstract class AbstractJmsDao implements PersistenceExceptionTranslator
{
    public void throwException()
    {
        try
        {
            throw new JMSException("test");
        }
        catch (JMSException ex)
        {
            throw JmsUtils.convertJmsAccessException(ex);
        }
    }

    @Override
    public DataAccessException translateExceptionIfPossible(RuntimeException ex)
    {
        // translate exceptions here.
    }
}

在我的XML配置中添加了PersistenceExceptionTranslationPostProcessor

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

使用@Repository注释我的DAO实现:

@Repository
public class CustomerJmsDao extends AbstractJmsDao implements CustomerDao
{
    public void test()
    {
        throwException();
    }
}

但是当抛出RuntimeException时,translateExceptionIfPossible()永远不会被击中(使用断点检查)。我显然在这里遗漏了一些东西,但是我无法弄清楚是什么。

1 个答案:

答案 0 :(得分:0)

虽然它没有转换为DataAccessException层次结构中的异常,但JmsUtils.convertJmsAccessException()转换为Spring等价物......

/**
 * Convert the specified checked {@link javax.jms.JMSException JMSException} to a
 * Spring runtime {@link org.springframework.jms.JmsException JmsException} equivalent.
 * @param ex the original checked JMSException to convert
 * @return the Spring runtime JmsException wrapping the given exception
 */
相关问题