簡體   English   中英

從異步方法讀取/寫入Websphere隊列

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

我有一個異步方法,該方法只負責連接到隊列並從隊列中讀取。 在嘗試讀取時,我從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);
    }

我認為IBM MQ的異步消息傳遞模式與.NET Framework的異步編程概念之間存在一些混淆。

IBM MQ通過排隊以異步方式啟用應用程序到應用程序的連接。 在典型的客戶端-服務器模式中,客戶端和服務器應用程序都需要始終啟動並運行以進行數據交換。 這是同步模式。 在異步模式, 不需要客戶端應用程序和服務器應用程序中始終處於運行狀態。 客戶端可以向MQ發送一條消息,然后消失。 該消息將駐留在MQ隊列中。 如果服務器應用程序正在運行,則該消息將傳遞到服務器應用程序。 服務器將處理該消息,並將回復消息放入MQ隊列中,然后消失。 客戶端應用程序可以在一段時間后返回並讀取回復消息。 如您所見,客戶端和服務器應用程序不是同時存在的,但是它們仍然能夠通過MQ以異步方式進行通信。

我不是較新的.NET Framework概念的專家。 .NET Framework中的async編程概念用於卸載CPU可以獨立執行的短任務,而不會阻塞線程。 例如,當主線程忙於顯示網頁的內容時,可以將連接數據庫的工作轉移到.NET任務中,以便用戶能夠與網頁進行交互。

IBM MQ .NET(amqmdnet和XMS)使用多個線程與隊列管理器進行通信。 當涉及多個線程時,我不確定“異步”編程技術是否合適。 這可能是“線程被中止”錯誤的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM