【问题标题】:How to create an active-mq ExceptionListener?如何创建一个active-mq ExceptionListener?
【发布时间】:2012-02-25 06:28:36
【问题描述】:

我有一个使用 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);
    } 
} 
}

【问题讨论】:

    标签: spring jakarta-ee activemq


    【解决方案1】:

    我已经像下面这样操作了 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);
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多