Spring JMS-延迟处理MessageListener类中的消息

时间:2020-02-24 06:15:18

标签: spring jms ibm-mq spring-jms websphere-8

我的应用程序在WAS 8.5上运行,并使用JMS MessageListener消耗来自IBM MQ 9的消息。当我的应用程序最初启动时,还会通过访问DB中的数据来形成一个缓存。但是,如果完成了JVM的任何重新启动或停止/启动,由于高速缓存未完全加载并且我的应用程序进入了空指针状态,并且onMessage方法开始从MQ提取消息。 Spring将缓存用于依赖项注入。在这种情况下,如何停止或MessageListener类如何等待直到缓存完全加载。下面是我的MessageListener以及LoadCache类的代码片段。

JMS邮件监听器

public class MQMessageConsumer implements MessageListener {
    private static Log log = LogFactory.getLog(MQMessageConsumer.class);

    ExecutorService executor = Executors.newFixedThreadPool(500);

    @SuppressWarnings("unchecked")
    public void onMessage(Message message) {
        log.debug("There is a message in the Queue");
        String inputXML = null;

        if (message instanceof TextMessage) {
            try {
                inputXML = ((TextMessage) message).getText();

                MQMessageProcessor mqp = new MQMessageProcessor(inputXML);
                executor.submit(mqp);
            } catch (Exception e) {
                log.error("Error in processing the message - " + e.getMessage());
            }
        } else
            log.error("Invalid message format, please resend with correct MQ message header as MQSTR");

    }
}

LoadCache

public class LoadCache implements ServletContextListener{

    private Cache cache;
    protected static Log log = LogFactory.getLog(LoadCache.class);
    private ApplicationService service = new ApplicationService();

    public void contextInitialized(ServletContextEvent event){
        cache = Cache.getCache();
        try {
            System.out.println("Cache building started during strtup");
            /*log.debug("Cahe building started during strtup");*/
            long startTime = System.currentTimeMillis();
            loadProperties(cache);
            loadDataTable(cache);
            loadOP(cache);
            loadUCM(cache);
            long endTime = System.currentTimeMillis() ;
            System.out.println("Total Cache building time is :"+(endTime - startTime));
            /*log.debug("Total Cache building time is :"+(endTime - startTime));*/
        } catch (DataAccessException e) {
            e.printStackTrace();
            log.debug("Error While communicating to database:"+e.getMessage());
        }
    }

    public void contextDestroyed(ServletContextEvent event){
        System.out.println("Context Destroyed-LoadCache Gone");
    }

}

如何让我的应用程序侦听器等待缓存加载完毕?我还尝试在MessageConsumer类中创建默认构造函数,并添加了20秒的线程睡眠,但这并没有帮助。关于如何实现这一目标的任何解决方法?

0 个答案:

没有答案