我想按順序讀取來自Websphere MQ的10000封消息,我使用下面的代碼來執行相同的操作,但讀取所有消息需要很長時間。即使我試圖使用多線程概念,但有時2個線程正在消耗相同的組和競態條件。以下是代碼片段。 我想用3個線程從MQ中順序讀取10000條消息,但是我的兩個線程在同一時間訪問同一個組。如何避免這種情況?順序讀取大量消息的最佳方法是什麼?我的要求是我想順序閱讀10000條消息。請幫忙。如何從Websphere讀取大量消息MQ
MQConnectionFactory factory = new MQConnectionFactory();
factory.setQueueManager("QM_host")
MQQueue destination = new MQQueue("default");
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
MessageConsumer lastMessageConsumer =
session.createConsumer(destination, "JMS_IBM_Last_Msg_In_Group=TRUE");
TextMessage lastMessage = (TextMessage) lastMessageConsumer.receiveNoWait();
lastMessageConsumer.close();
if (lastMessage != null) {
int groupSize = lastMessage.getIntProperty("JMSXGroupSeq");
String groupId = lastMessage.getStringProperty("JMSXGroupID");
boolean failed = false;
for (int i = 1; (i < groupSize) && !failed; i++) {
MessageConsumer consumer = session.createConsumer(destination,
"JMSXGroupID='" + groupId + "'AND JMSXGroupSeq=" + i);
TextMessage message = (TextMessage)consumer.receiveNoWait();
if (message != null) {
System.out.println(message.getText());
} else {
failed = true;
}
consumer.close();
}
if (failed) {
session.rollback();
} else {
System.out.println(lastMessage.getText());
session.commit();
}
}
connection.close();