我需要浏览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
}