JMS队列静态快照

我需要浏览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
}
b946241223 回答:JMS队列静态快照

the JavaDoc for javax.jms.QueueBrowser中所述:

  

扫描完成后,消息可能正在到达和到期。 JMS API不需要将枚举的内容作为队列内容的静态快照。这些更改是否可见取决于JMS提供程序。

队列浏览器从JBoss EAP中的JMS提供程序提供的枚举的内容是不是静态的,没有方法可以强制这样做。

由于JMS不能保证您寻找的行为,因此建议您调整应用程序,使其不依赖于这种行为。

有两种选择:

  • 设置浏览器将检查的消息数的上限。
  • 在创建浏览器之前,使用提供程序特定的管理调用获取队列中的消息数量,然后仅浏览该数量的消息。
本文链接:https://www.f2er.com/3143724.html

大家都在问