ibmmq的SVRCONN通道如何接收多个队列的消息
时间: 2023-08-14 19:03:17 浏览: 189
在IBM MQ中,SVRCONN通道是一种常用的通道类型,它可以用于连接到队列管理器并进行消息发送和接收。如果你想要接收多个队列的消息,可以使用IBM MQ的多线程机制。
以下是一个简单的Java示例,它演示了如何使用SVRCONN通道接收多个队列的消息,并将它们打印到控制台:
```java
import com.ibm.mq.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MQReceiver {
private static final String QMGR_NAME = "QMGR";
private static final String CHANNEL_NAME = "CHANNEL";
private static final String HOST_NAME = "localhost";
private static final int PORT = 1414;
private static final String USER_NAME = "username";
private static final String PASSWORD = "password";
private static final String QUEUE_NAME_1 = "QUEUE1";
private static final String QUEUE_NAME_2 = "QUEUE2";
public static void main(String[] args) throws MQException {
MQEnvironment.hostname = HOST_NAME;
MQEnvironment.port = PORT;
MQEnvironment.channel = CHANNEL_NAME;
MQEnvironment.userID = USER_NAME;
MQEnvironment.password = PASSWORD;
MQQueueManager qmgr = new MQQueueManager(QMGR_NAME);
MQQueue queue1 = qmgr.accessQueue(QUEUE_NAME_1, MQC.MQOO_INPUT_AS_Q_DEF);
MQQueue queue2 = qmgr.accessQueue(QUEUE_NAME_2, MQC.MQOO_INPUT_AS_Q_DEF);
MQReceiverThread thread1 = new MQReceiverThread(queue1);
MQReceiverThread thread2 = new MQReceiverThread(queue2);
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.execute(thread1);
executor.execute(thread2);
executor.shutdown();
}
private static class MQReceiverThread implements Runnable {
private MQQueue queue;
public MQReceiverThread(MQQueue queue) {
this.queue = queue;
}
public void run() {
MQMessage msg = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options |= MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING;
gmo.waitInterval = MQC.MQWI_UNLIMITED;
while (true) {
try {
queue.get(msg, gmo);
System.out.println(msg.readString());
} catch (MQException e) {
if (e.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE) {
continue;
}
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
```
在这个示例中,我们创建了两个MQQueue对象,分别用于接收QUEUE1和QUEUE2队列中的消息。然后,我们创建了两个MQReceiverThread线程对象,每个线程都从一个队列中接收消息。最后,我们使用ExecutorService来启动并管理这两个线程。
在每个线程中,我们使用一个无限循环来接收消息。当队列中没有可用的消息时,我们使用MQException.MQRC_NO_MSG_AVAILABLE异常来继续等待下一条消息。如果发生其他异常,我们将它们打印到控制台。
通过类似这个示例的方法,你可以在SVRCONN通道中接收多个队列的消息。
阅读全文
相关推荐


















