When I call com.ibm.mq.MQQueue#put(MQMessage,MQPutMessageOptions)
it may hang. How can I set up a timeout for this method?
The same question is for com.ibm.mq.MQQueue#get(MQMessage,MQGetMessageOptions)
问题:
回答1:
With MQ v7.0 and higher using SVRCONN channels with SHARECNV of 1 or higher you can shorten the time that it would take the IBM MQ classes for Java or IBM MQ classes for JMS to return a error on a PUT as a result of a network issue by setting the HBINT of the SVRCONN to a lower value. The TIMEOUT is based on the HBINT using the following formula:
- If HBINT is less than 60: HBINT * 2
- If HBINT is greater than or equal to 60: HBINT + 60
The default HBINT on a SVRCONN channel is 300 seconds, so you end up with a six minute timeout. I like to set this to a lower value such as 15 which gives you a 30 second timeout.
IBM MQ classes for Java and IBM MQ classes for JMS adopt the HBINT set on the queue manager's SVRCONN channel when connection details are specified programmatically or via JNDI.
If you point to a channel table Java/JMS will use the channel table's CLNTCONN HBINT, the final HBINT will be the highest value between CLNTCONN and SVRCONN. For example if the CLNTCONN channel was set to 15 and the SVRCONN was set to the default of 300, the final HBINT would be 300.
回答2:
There is MQGMO_WAIT option and a WaitInterval that can be set to make the Get call to wait for a certain amount of time. For example, the following snippet makes the Get call to wait for 3 seconds.
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.Options = MQConstants.MQGMO_WAIT;
gmo.WaitInterval = 3000;
mqQueue.Get(mqMessage, gmo);
There is no option to set a timeout for the Put call though. The Put call returns with an error if there is any issue.
UPDATE:
When a Put call is invoked, connection to queue manager is already established. If there are any issues with the connection, the Put call gets returns soon as the TCP stack notifies of such issue. As such TCP level issues affect the all applications running on the system, in my opinion, tuning must be done at the system level and not at a per application level. Also I don't think it's possible to set a timeout for a socket.write call.
MQ does provide a way set a timeout for establishing connection to queue manager though. There is connection_timeout parameter in mqclient.ini you can set a timeout.