简体   繁体   English

从异步方法读取/写入Websphere队列

[英]Reading/Writing a websphere queue from an Async method

I have a asynchronous method that is responsible for only connecting to the queue and reading from the queue. 我有一个异步方法,该方法只负责连接到队列并从队列中读取。 While trying to read I get an error from the WebSphere dll 'amqmdnet.dll' that says "Thread was being aborted". 在尝试读取时,我从WebSphere dll'amqmdnet.dll'中得到一个错误,提示“线程正在中止”。 I get this error every time I try to modify the queue in any way, and only when I try to modify from an asynchronous method. 每当我尝试以任何方式修改队列,并且仅当我尝试从异步方法进行修改时,都会出现此错误。 I've also tried implementing the IBM.XMS.net dll as this was said to be used for asynchronous messaging, although I get the same error. 我也曾尝试实现IBM.XMS.net dll,因为据说它用于异步消息传递,尽管我遇到了同样的错误。 Is it possible to read a queue from inside an async method? 是否可以从异步方法内部读取队列? If so, is the implementation for reading/writing different for synchronous and asynchronous when it comes to modifying the queue itself? 如果是这样,那么在修改队列本身时,同步和异步的读/写实现是否有所不同? I can connect to the queue manager just fine, its modifying the queue that's giving me issues. 我可以很好地连接到队列管理器,它可以修改给我带来问题的队列。

Main: 主要:

private async Task ReadAsync()
{
   await MqMessanger.ConnectAsync(); // connects fine
   await MqMessanger.StartReadingAsync(); // errors out
}

MqMessanger: (IBM.XMS.net dll) 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) 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);
    }

I think there is some confusion here between Asynchronous messaging pattern of IBM MQ and Asynchronous programming concept of .NET Framework. 我认为IBM MQ的异步消息传递模式与.NET Framework的异步编程概念之间存在一些混淆。

IBM MQ enables application to application connectivity in an asynchronous way through queuing. IBM MQ通过排队以异步方式启用应用程序到应用程序的连接。 In a typical client-server pattern, both client and server applications need to be up and running always for data exchange. 在典型的客户端-服务器模式中,客户端和服务器应用程序都需要始终启动并运行以进行数据交换。 This is synchronous pattern. 这是同步模式。 In asynchronous pattern, client application and server application are not required to be running always. 在异步模式, 不需要客户端应用程序和服务器应用程序中始终处于运行状态。 Client can send a message to MQ and go away. 客户端可以向MQ发送一条消息,然后消失。 The message will be residing in a MQ queue. 该消息将驻留在MQ队列中。 If the server application is running, that message will be delivered to server application. 如果服务器应用程序正在运行,则该消息将传递到服务器应用程序。 The server will process that message and put a reply message into MQ queue and go away. 服务器将处理该消息,并将回复消息放入MQ队列中,然后消失。 The client application can come back after some time and read the reply message. 客户端应用程序可以在一段时间后返回并读取回复消息。 As you can see client and server applications are not simultaneously but still they are able to communicate in a asynchronous way via MQ. 如您所见,客户端和服务器应用程序不是同时存在的,但是它们仍然能够通过MQ以异步方式进行通信。

I am not an expert in the newer .NET Framework concepts. 我不是较新的.NET Framework概念的专家。 The async programming concept in .NET Framework is for offloading short tasks that can be independently by the CPU without blocking a thread. .NET Framework中的async编程概念用于卸载CPU可以独立执行的短任务,而不会阻塞线程。 For example when the main thread is busy displaying contents of a web page, the job of connecting to a database could be offloaded to a .NET task so that the user is able to interact with the web page. 例如,当主线程忙于显示网页的内容时,可以将连接数据库的工作转移到.NET任务中,以便用户能够与网页进行交互。

IBM MQ .NET (amqmdnet and XMS) use multiple threads for communicating with a queue manager. IBM MQ .NET(amqmdnet和XMS)使用多个线程与队列管理器进行通信。 I am unsure if 'async' programming technique is suitable when multiple threads are involved. 当涉及多个线程时,我不确定“异步”编程技术是否合适。 This could be the reason for "Thread was being aborted" error. 这可能是“线程被中止”错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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