队列管理支持

使用 BizTalk Server MQSeries 适配器,现在可以在 MQSeries 队列管理器上远程创建和删除队列。 支持此操作,因为 BizTalk Server 使用与 MQSeries 队列管理器直接通信的远程 MQSAgent COM+ 对象。 通常,在运行时使用此 MQSAgent 从远程 MQSeries 服务器队列读取信息和向其中写入消息。 此远程服务的客户端可以是多台 BizTalk Server。 此外,队列创建和删除功能由此 MQSAgent 提供,并可以从业务流程或适配器中直接调用。 这样可以实现高度动态的方案:业务流程或适配器创建一个临时队列,然后向其上发送消息,在另一个队列上接收回复,最后删除这个临时队列。

CreateQueue 和 DeleteQueue API

CreateQueue 和 DeleteQueue API 定义如下。

结构定义

typedef enum QueueUsage {  
      Normal       = 0,  
      Transmission = 1  
} QueueUsage;  
  
typedef enum ResultCode {  
      QueueAlreadyExists                     = 0, //  no bits set  
      QueueCreated                           = 1, //  QueueCreated  
      QueueCreatedAndRemoteDefinitionUpdated = 5, //  QueueCreated | RemoteDefinitionUpdated  
      QueueAndRemoteDefinitionCreated        = 7, //  QueueCreated | RemoteDefinitionCreated | RemoteDefinitionUpdated  
      QueueDoesNotExist                      = 8, //  QueueDoesNotExist  
      QueueDeleted                           = 16 //  QueueDeleted  
} ResultCode;  

接口定义

[  
            object,  
            uuid(E90AC1A6-657B-4680-AF6A-89F11113FB8B),  
            dual,  
            nonextensible,  
            helpstring("IMQSAdmin Interface"),  
            pointer_default(unique)  
]  
interface IMQSAdmin2 : IDispatch{  
  
HRESULT CreateQueue (  
[in]BSTR queueManager,  
[in]BSTR newQueueName,  
[in]QueueUsage usage,  
[in]BSTR remoteDefinition,  
[in]BSTR remoteQName,  
[in]BSTR remoteQMgrName,  
[in]BOOL updateExistingRemoteDefinition,  
[out, retval]ResultCode* resultCode);  
  
HRESULT DeleteQueue (  
[in]BSTR queueManager,  
[in]BSTR newQueueName,  
[out, retval]ResultCode* resultCode);  
};  
  
      [  
            uuid(412AF00D-7CA8-4d2a-AFF6-F61CE2E29A0D),  
            helpstring("MQSAdmin Class")  
      ]  
      coclass MQSAdmin  
      {  
            [default] interface IMQSAdmin2;  
      };  
  

示例

完成示例 1 中的步骤,创建可用于创建或删除 MQSeries 服务器队列的 Visual Studio C# 控制台应用程序。

示例 1

创建一个管理 MQSeries Server 队列的 C# 控制台应用程序
  1. 在 Visual Studio 中创建新的名为 MQSeriesQueues 的 Visual C# 控制台应用程序。

  2. 使用以下代码替换生成的 Program.cs 文件中的所有已有代码。

    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using MQSAgentLib;  
    
    namespace MQSeriesQueues  
    {  
        class ManageQueues  
        {  
            public static void Main(string[] args)  
            {  
                // The first argument should be "c" (without quotes)  
                // to create a queue, anything else to delete a queue.  
                // The 2nd and 3rd arguments should be the name of   
                // the MQSeries Queue Manager and the name of   
                // the queue to be created or deleted for example  
                // the following usage will create the local   
                // queue testq for the Queue Manager QM_Test  
                // MQSeriesQueues c QM_Test testq  
                createordeleteQs(args[0], args[1], args[2]);  
            }  
    
            static void createordeleteQs(string Qswitch, string QMgr, string QName)  
            {   
            if ((Qswitch =="c" & (QMgr != null & QName != null)))  
                {  
                    CreateQueue(QMgr, QName);  
                }  
                else if(QMgr != null & QName != null)  
                {  
                    DeleteQueue(QMgr, QName);  
                }  
             }  
    
            static void CreateQueue(string Qmgr, string Qname)  
            {  
                MQSAdmin admin = new MQSAdmin();    
    
                ResultCode resultCode = admin.CreateQueue(Qmgr, Qname, 0, "", "", "", 0);  
    
                if ((resultCode & ResultCode.QueueCreated) == ResultCode.QueueCreated)  
                {  
                    Console.WriteLine("Queue Created.");  
                }  
                else if ((resultCode & ResultCode.QueueAlreadyExists) == ResultCode.QueueAlreadyExists)  
                {  
                    Console.WriteLine("Queue Already Exists.");  
                }  
            }  
    
            static void DeleteQueue(string Qmgr, string Qname)  
            {  
                MQSAdmin admin = new MQSAdmin();  
    
                ResultCode resultCode = admin.DeleteQueue(Qmgr, Qname);  
    
                if ((resultCode & ResultCode.QueueDeleted) == ResultCode.QueueDeleted)  
                {  
                    Console.WriteLine("Queue successfully deleted.");  
                }  
                if ((resultCode & ResultCode.QueueDoesNotExist) == ResultCode.QueueDoesNotExist)  
                {  
                    Console.WriteLine("Queue did not exist anyway!");  
                }  
            }  
    
        }  
    }  
    
  3. 将对此项目的引用添加到 MQSAgent 1.0 类型库MQSAgent 1.0 类型库位于“添加引用”对话框的“COM”选项卡上。

    注意

    运行此控制台应用程序的计算机中必须已经安装了 MQSAgent COM+ 组件。 有关安装 MQSAgent COM+ 组件的详细信息,请参阅 使用 MQSAgent COM+ 配置向导

  4. 生成控制台应用程序。

  5. 在编译的控制台应用程序所在的目录中打开一个命令提示窗口。

  6. 键入编译的控制台应用程序的名称和相应的参数,然后按 Enter。 例如,若要删除队列管理器的队列 testqQM_Test 请在命令提示符处键入以下文本,然后按 Enter:

    MQSeriesQueues d QM_Test testq  
    
  7. 若要创建队列管理器的队列 testqQM_Test 请在命令提示符处键入以下文本,然后按 Enter:

    MQSeriesQueues c QM_Test testq