I'm trying to implement a destructive reading from WebSphere, i.e. I read message that should be then immediately deleted from the queue.
I written the code that worked fine until I started to mess with messages. For example, the last one was "add one message, read it, wait on empty queue, then add two messages". In my scenario this program should read the first message, wait until something appears, and then read it too.
However, the problem is that I've got a situation where I'm stuck. I have a message in the queue but I can't read if with BROWSE nor with CURSOR. Here is my code:
MQEnvironment.UserId = _queueSettings.UserName;
MQEnvironment.Password = _queueSettings.Password;
var manager = new MQQueueManager(_queueSettings.QueueManagerName, _queueSettings.ChannelName, _queueSettings.ConnectionName);
var queue = manager.AccessQueue(_queueSettings.QueueName, MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INPUT_SHARED | MQC.MQOO_BROWSE);
var browseFirstOptions = new MQGetMessageOptions { Options = MQC.MQGMO_BROWSE_FIRST };
var cursorOptions = new MQGetMessageOptions { Options = MQC.MQGMO_MSG_UNDER_CURSOR };
var currentOptions = browseFirstOptions;
while (!cancellationToken.IsCancellationRequested)
{
var logger = _contextlessLogger.ForContext("requestId", Guid.NewGuid());
try
{
var msg = new MQMessage();
queue.Get(msg, currentOptions);
if (currentOptions == browseFirstOptions)
{
currentOptions = cursorOptions;
continue;
}
string messageText = msg.ReadString(msg.MessageLength);
RunProcessingTask(logger, messageText);
}
catch (MQException ex) when (IsNoMessagesException(ex) && currentOptions != browseFirstOptions)
{
currentOptions = browseFirstOptions;
}
catch (MQException ex) when (IsNoMessagesException(ex))
{
const int sleepIntervalMs = 5000;
_contextlessLogger.Information("No messages in the queue. Sleeping for {sleepIntervalMs}ms", sleepIntervalMs);
await Task.Delay(sleepIntervalMs);
}
catch (Exception ex)
{
logger.Error(ex, "Unexpected error occured");
}
}
private static bool IsNoMessagesException(MQException exception) =>
exception.ReasonCode == MQC.MQRC_NO_MSG_AVAILABLE
|| exception.ReasonCode == MQC.MQRC_NO_MSG_UNDER_CURSOR;
I just get 2033
or 2034
while I can see in UI that there is a message.
How could it be done? Maybe I'm doing the wrong thing?
I added Java tag as well because their code doesn't differ at all, for example I used this code as reference.