如何创建active-mq ExceptionListener?

时间:2012-02-02 15:35:10

标签: spring java-ee activemq

我有一个使用Spring-hibernate-activeMq编码的项目

我想知道的是,如果我像下面解释的那样配置activeMq,我应该如何实现它的异常监听器类?我知道你现在不太懂,但请看下面我的样品。

如果我正确实现了异常监听器,请告诉我。如果没有,请举例说明必须如何。提前谢谢。

应用程序上下文:(请注意,除了connectionFactory的属性之外,我没有为异常监听器声明任何bean)

<bean id="connectionFactory"
      class="org.springframework.jms.connection.CachingConnectionFactory"
      depends-on="broker">
    <constructor-arg ref="amqConnectionFactory"/>
    <property name="reconnectOnException" value="true"/>
    <property name="exceptionListener" ref="jmsExceptionListener"/> 
    <property name="sessionCacheSize" value="100"/>
</bean>

Jms异常监听器类:(注意我正在尝试注入ConnectionFactory,我不确定它是否可能..最后一件事,请检查它的构造函数参数,我也不确定..)

@Component("jmsExceptionListener")
public class JMSExceptionListener implements ExceptionListener {

private final static Logger logger = LoggerFactory.getLogger(JMSExceptionListener.class); 

@Autowired
private CachingConnectionFactory connection;

//    private Connection connection = null;

private ExceptionListener exceptionListener = null; 

public JMSExceptionListener() {
}

public JMSExceptionListener(CachingConnectionFactory connection, ExceptionListener exceptionListener) {
    super();
    this.connection = connection;
    this.exceptionListener = exceptionListener; 
}

public void onException(JMSException arg0) {
    logger.error("JMS exception has occured.. ", arg0);

    if(connection != null){
        connection.onException(arg0);
    } 
    if (exceptionListener != null) {
        exceptionListener.onException(arg0);
    } 
} 
}

1 个答案:

答案 0 :(得分:0)

我操作了exceptionListener类,如下所示:

public class JmsExceptionListener implements ExceptionListener {
private final static Logger logger = LoggerFactory.getLogger(JmsExceptionListener.class);

@Autowired
private MailService mailService;

private ExceptionListener exceptionListener = null;

private CachingConnectionFactory cachingConnectionFactory;

public JmsExceptionListener() {
}

public JmsExceptionListener(Connection connection, ExceptionListener exceptionListener) {
    super();
    this.exceptionListener = exceptionListener;
}

public synchronized void onException(JMSException e) {
    logger.error("JMS exception has occurred: ", e);
    sendErrorNotificationMail(e);

    Exception ex = e.getLinkedException();
    if (ex != null) {
        logger.error("JMS Linked exception: ", ex);
    }

    if (exceptionListener != null) {
        exceptionListener.onException(e);
    }
}

public CachingConnectionFactory getCachingConnectionFactory() {
    return cachingConnectionFactory;
}

public void setCachingConnectionFactory(CachingConnectionFactory cachingConnectionFactory) {
    this.cachingConnectionFactory = cachingConnectionFactory;
}

private void sendErrorNotificationMail(Exception e) {
    try {
        mailService.sendJmsExceptionMail(e, ErrorMessageAccessor.get("core.jms.unexpected"));
    } catch (ElekBusinessException e1) {
        logger.error(ErrorMessageAccessor.get("generic.mailService.exp"), e);
    }
}

}