我正在创建一个侦听队列的 JMS 侦听器应用程序。我正在使用 TIBCO JMS 实现并面临一个问题,即间歇性地多个我的侦听器线程接收相同的消息并导致重复处理。
这是我创建连接的方式:…………
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.PROVIDER_URL, url);
env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactoryClass);
env.put(Context.SECURITY_PRINCIPAL, userName);
String decryptedPass = null;
//Decryption logic
try {
// Look up queue connection factory from naming context.
if (log.isEnabledFor(Level.DEBUG)) {
log.debug("Attempting to lookup queue connection factory at '" +
this.url + "' as user '" + userName + "'.");
}
Context ctx = new InitialContext(env);
QueueConnectionFactory factory =
(QueueConnectionFactory) ctx.lookup(connectionFactoryName);
// Create JMS connection using the factory we just looked up.
if (log.isEnabledFor(Level.DEBUG)) {
log.debug("Creating queue connection as user '" + userName + "'.");
}
connection = factory.createQueueConnection(userName, decryptedPass);
...
..
.
然后在这里我使用上面创建的相同连接创建侦听器线程
//This is called in a loop.
// Create a JMS session that is non-transacted, but in client
// acknowledge mode. This will allow us to control when
// messages are acknowledged.
QueueSession session =
getQueueConnection().createQueueSession(
false, Tibjms.EXPLICIT_CLIENT_ACKNOWLEDGE);
// Create a receiver for the queue we are interested in. Then
// set the message listener defined on the outer class. Messages
// will be delivered on a dispatcher thread created by the
// JMS provider.
Queue queue = session.createQueue(getQueueName());
session.createReceiver(queue).setMessageListener(getListener());
...
..
.
现在让我们假设,创建了 5 个侦听器线程,它们作为接收者在队列中侦听。我看到一种行为,有时不止一个侦听器线程/接收器接收相同的消息,我最终得到重复处理?我如何通过 JMS 配置来处理它?甚至可能吗?还是我不得不求助于一些程序化解决方案?任何建议将不胜感激。谢谢。