简体   繁体   中英

MQ C API - how to abort a waiting `MQGET` in a multithreaded environment

I'm using the MQ C API in a multithreaded application, where multiple worker threads are processing incoming messages each from its own queue by calling MQGET in a loop (with MQGMO_WAIT set and a wait interval of MQWI_UNLIMITED ).

It works but I can't find a clean way to shut down the worker threads. If I call MQDISC or MQCLOSE in another thread, it blocks whenever there is an outstanding MQGET call.

For now my best solution is to use a finite wait interval of 5000 instead of MQWI_UNLIMITED , this way MQGET calls return every 5 second, giving the app the chance to shutdown. But this solution is less efficient (worker threads wake up every 5 seconds), and it takes the app up to 5 seconds to shut down.

Terminating the threads using native OS API does not sound like a good solution.

So the question is - is there any way to cleanly abort a waiting MQGET when the wait interval is MQWI_UNLIMITED ?

OS is Windows Server 2012 x64, MQ server version is 7.0.0.19.

使用MQCallBack调用而不是MQGET。

一种选择是,如果您禁用从队列中获取(即ALTER QLOCAL(xxx)GET(DISABLED)),则任何等待中的吸气剂都会由于MQRC_GET_INHIBITED的原因而立即抛出。

The two answers already given are good options, and I have a third to add, so here's the full list.

  • You are on V7 as a mimumum so you can use the callback mechanism, MQCB, and a call-back function, then you can use MQCTL to suspend or stop the connection.
  • Either use an admin command, or on another thread use MQSET call, to change the queue to GET(DISABLED), which will wake up the MQGET-wait with MQRC_GET_INHIBITED.
  • From another thread MQPUT(1) a message to the queue the MQGET is waiting on. Ensure your getting code understands that the format/content of this message means "time to end the app".

All are equally valid, but require varying degrees of change to your current application, and only you can know which will be easiest in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM