【发布时间】:2019-11-07 15:09:27
【问题描述】:
我需要浏览一个 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
}
【问题讨论】:
-
包括用于浏览的代码......假设初始队列数为 20,当我浏览时我只想查看这 20 条消息,如果有新消息进入队列则忽略
标签: jms jboss-eap-7