【发布时间】:2016-02-10 09:01:13
【问题描述】:
我有一个异步方法,它只负责连接到队列并从队列中读取。在尝试读取时,我从 WebSphere dll 'amqmdnet.dll' 中得到一个错误,上面写着“线程被中止”。每次我尝试以任何方式修改队列时都会出现此错误,并且仅当我尝试从异步方法进行修改时。我也尝试过实现 IBM.XMS.net dll,因为据说它用于异步消息传递,尽管我得到了同样的错误。是否可以从异步方法中读取队列?如果是这样,在修改队列本身时,同步和异步的读/写实现是否不同?我可以很好地连接到队列管理器,它修改了给我带来问题的队列。
主要:
private async Task ReadAsync()
{
await MqMessanger.ConnectAsync(); // connects fine
await MqMessanger.StartReadingAsync(); // errors out
}
MqMessanger:(IBM.XMS.net dll)
private XMSFactoryFactory factoryFactory; //used for connection
private IConnectionFactory cf; //used for connection
private ISession sessionWMQ;
private IDestination destination;
private IMessageConsumer consumerAsync;
private MessageListener messageListener;
public IConnection QueueManager { get; set; }
//QueueManager has been connected prior to this
private void StartReadingAsync()
{
try
{
//Creates a session where an Ack is sent to the server to delete the message as soon the message is received.
sessionWMQ = QueueManager.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
destination = sessionWMQ.CreateQueue(queueName);
// Create consumer object to read messages from the queue
consumerAsync = sessionWMQ.CreateConsumer(destination);
// Create a message listener to fire when a message is put on the queue and assign it to consumer
messageListener = new MessageListener(OnMessageCallback);
consumerAsync.MessageListener = messageListener;
}
catch (Exception ex)
{
throw new Exception($"Error reading from '{destination.Name}'.", ex);
}
}
MqMessanger: (amqmdnet dll)
//QueueManager has been connected prior to this
private void StartReadingAsync()
{
try
{
queue = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
queueMessage = new MQMessage();
queueMessage.Format = MQC.MQFMT_STRING;
queueGetMessageOptions = new MQGetMessageOptions();
queue.Get(queueMessage, queueGetMessageOptions);
string message = queueMessage.ReadString(queueMessage.MessageLength);
//Do something with this message
}
catch (Exception ex)
{
throw new Exception($"Error reading from '{destination.Name}'.", ex);
}
【问题讨论】:
标签: c# asynchronous ibm-mq