Spring JMS DAO的PersistenceExceptionTranslationPostProcessor
我遇到了Spring的Spring JMS DAO的PersistenceExceptionTranslationPostProcessor,spring,exception-handling,jms,data-access-layer,spring-jms,Spring,Exception Handling,Jms,Data Access Layer,Spring Jms,我遇到了Spring的PersistenceExceptionTranslationPostProcessor,这似乎很完美,因为它将异常抽象到用@Repository注释的DAO中 现在我有了一个使用JMS(ActiveMQ)而不是数据库作为后端的应用程序。我想使用类似于PersistenceExceptionTranslationPostProcessor的东西将jmsceceptions转换为Spring的DataAccessException 在我去重新发明轮子之前,我在网上搜索过这样的
PersistenceExceptionTranslationPostProcessor
,这似乎很完美,因为它将异常抽象到用@Repository
注释的DAO中
现在我有了一个使用JMS(ActiveMQ)而不是数据库作为后端的应用程序。我想使用类似于PersistenceExceptionTranslationPostProcessor
的东西将jmsceception
s转换为Spring的DataAccessException
在我去重新发明轮子之前,我在网上搜索过这样的东西,但没有找到。也许我使用了错误的搜索键,所以作为第二次尝试,有没有人知道这样的东西存在,或者我必须发明这个轮子
更新: 似乎我必须自己创建一个
持久异常Translator
。我已经做了以下工作:
在我的抽象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" />
但是,当抛出
RuntimeException
时,translateExceptionIffailable()
永远不会被命中(使用断点检查)。很明显,我在这里遗漏了一些东西,但是我不知道是什么。虽然它没有转换为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
*/
谢谢你的回答。我已经知道了
JmsUtils
类,但这是拼图中的一个重要部分。我已经更新了我现在的答案。我不确定JMS是否真的适合DAO模型,但为了实现这一点,PersistenceExceptionTranslationPostProcessor需要PersistenceExceptionTranslationAdvisor,它需要PersistenceExceptionTranslationInterceptor,它需要对自定义PersistenceExceptionTranslator的引用。否则…后处理器不知道如何应用您的转换器。
/**
* 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
*/