0

我需要浏览 JMS 队列并根据存在多少特定条件的消息对其进行过滤。

但是问题出在 JBoss EAP 中,在浏览队列时,如果有新消息出现,也会在浏览时考虑,这使得进程运行时间过长,因为该应用程序不断收到大量消息。

基本上需要了解我是否可以获得队列的静态快照,以便我可以在不考虑新消息和即将到来的消息的情况下扫描消息。

PS:这在 Weblogic 服务器中运行良好。

这是浏览器代码:

Context namingContext = null;

try {
    String userName = System.getProperty("username", DEFAULT_USERNAME);
    String password = System.getProperty("password", DEFAULT_PASSWORD);

    // Set up the namingContext for the JNDI lookup
    final Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
    env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
    env.put(Context.SECURITY_PRINCIPAL, userName);
    env.put(Context.SECURITY_CREDENTIALS, password);
    namingContext = new InitialContext(env);

    // Perform the JNDI lookups
    String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
    ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);


    try (JMSContext context = connectionFactory.createContext(userName, password)) {
        Queue queue = (Queue) namingContext.lookup("jms/ubsexecute");
        QueueBrowser browser = context.createBrowser(queue);
        Enumeration enumeration = browser.getEnumeration();
        int i =1;
        while (enumeration.hasMoreElements()) {
            Object nextElement = enumeration.nextElement();
            System.out.println("Read a message " + i++);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    } catch (NamingException e) {
        log.severe(e.getMessage());
        e.printStackTrace();
    } finally {
        if (namingContext != null) {
            try {
                namingContext.close();
            } catch (NamingException e) {
                log.severe(e.getMessage());
            }
        }
    }
} catch (Exception e) {
    // TODO: handle exception
}
4

1 回答 1

0

JavaDoc 中所述javax.jms.QueueBrowser

扫描完成时,邮件可能会到达并过期。JMS API 不要求枚举的内容是队列内容的静态快照。这些更改是否可见取决于 JMS 提供程序。

JBoss EAP 中 JMS 提供者的队列浏览器提供的枚举内容不是静态的,也没有办法强制它是静态的。

由于 JMS 不保证您正在寻找的行为,我建议您调整您的应用程序,使其不依赖于此类行为。

我想到了几个替代方案:

  • 设置浏览器将检查的消息数量上限。
  • 在创建浏览器之前,使用特定于提供程序的管理调用来获取队列中的消息数量,然后仅浏览该数量的消息。
于 2019-11-07T17:11:52.990 回答