C# 将消息放在IBM Websphere MQ上

C# 将消息放在IBM Websphere MQ上,c#,ibm-mq,C#,Ibm Mq,我有一个应用程序,需要将非常简单的消息放入远程队列。 所使用的队列由第三方提供,其名称为IBM WebSphere MQ(版本7.5) 我尝试将amqmdnet.dll与下面的示例代码一起使用,但我知道应该在我的服务器上安装一个MQ客户端来执行此操作 因此,我的问题是: 有没有办法在没有所有这些要求的情况下将消息放入队列?像简单的REST或SOAP客户端吗? 我愿意使用不同的语言来实现这样的组件,我只是不想在服务器上安装第三方应用程序(这是一个托管环境) //==================

我有一个应用程序,需要将非常简单的消息放入远程队列。 所使用的队列由第三方提供,其名称为IBM WebSphere MQ(版本7.5)

我尝试将
amqmdnet.dll
与下面的示例代码一起使用,但我知道应该在我的服务器上安装一个MQ客户端来执行此操作

因此,我的问题是: 有没有办法在没有所有这些要求的情况下将消息放入队列?像简单的REST或SOAP客户端吗? 我愿意使用不同的语言来实现这样的组件,我只是不想在服务器上安装第三方应用程序(这是一个托管环境)

//===========================================================================
//许可材料-IBM的财产
//5724-H72
//(c)版权所有IBM Corp.2003,2005
// ===========================================================================
使用制度;
使用系统集合;
使用IBM.WMQ;
类MQSample
{
//要使用的连接类型,可以是:-
//服务器连接的MQC.TRANSPORT_MQSERIES_绑定。
//用于非XA客户端连接的MQC.TRANSPORT_MQSERIES_客户端
//MQC.TRANSPORT_MQSERIES_XACLIENT用于XA客户端连接
//MQC.TRANSPORT_MQSERIES_为托管客户端连接管理
const String connectionType=MQC.TRANSPORT\u MQSERIES\u CLIENT;
//定义要使用的队列管理器的名称(适用于所有连接)
const String qManager=“您的管理者”;
//定义主机连接的名称(仅适用于客户端连接)
const String hostName=“您的主机名”;
//定义要使用的通道的名称(仅适用于客户端连接)
const String channel=“您的\u channelname”;
/// 
///初始化所请求连接类型的连接属性
/// 
///MQC.TRANSPORT_MQSERIES_uu值之一
静态哈希表初始化(字符串连接类型)
{
Hashtable connectionProperties=新的Hashtable();
//添加连接类型
添加(MQC.TRANSPORT\u属性,connectionType);
//根据
//请求的连接类型
交换机(连接类型)
{
案例MQC.TRANSPORT_MQSERIES_绑定:
打破
案例MQC.TRANSPORT_MQSERIES_客户端:
案例MQC.TRANSPORT_MQSeriesxau客户端:
案例MQC.TRANSPORT_MQSERIES_管理:
添加(MQC.HOST\u NAME\u属性,主机名);
添加(MQC.CHANNEL_属性,CHANNEL);
打破
}
返回connectionProperties;
}
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态int Main(字符串[]args)
{
尝试
{
Hashtable connectionProperties=init(connectionType);
//使用连接创建到队列管理器的连接
//刚定义的属性
MQQueueManager qMgr=新的MQQueueManager(qManager,connectionProperties);
//在要打开的队列上设置选项
int openOptions=MQC.MQOO_INPUT_AS_uq_udef| MQC.MQOO_输出;
//现在指定要打开的队列和打开选项
MQQueue系统\默认\本地\队列=
AccessQueue(“SYSTEM.DEFAULT.LOCAL.QUEUE”,openOptions);
//定义WebSphereMQ消息,以UTF格式写入一些文本
MQMessage hello_world=新MQMessage();
你好,世界。写下(“你好,世界!”);
//指定消息选项
MQPutMessageOptions pmo=new MQPutMessageOptions();//接受默认值,
//与MQPMO_默认值相同
//将消息放入队列
系统\默认\本地\队列.Put(hello \ world,pmo);
//再把信息拿回来
//首先定义一个WebSphereMQ消息缓冲区来接收消息
MQMessage retrievedMessage=新MQMessage();
retrievedMessage.MessageId=hello\u world.MessageId;
//设置获取消息选项
MQGetMessageOptions gmo=new MQGetMessageOptions();//接受默认值
//与MQGMO_默认值相同
//从队列中取出消息
系统\默认\本地\队列.Get(retrievedMessage,gmo);
//通过显示UTF消息文本来证明我们拥有该消息
字符串msgText=retrievedMessage.ReadUTF();
WriteLine(“消息是:{0}”,msgText);
//关闭队列
system_default_local_queue.Close();
//断开与队列管理器的连接
qMgr.Disconnect();
}
//如果上述操作中出现错误,请尝试找出错误所在。
//这是WebSphere MQ错误吗?
捕获(MQException-ex)
{
WriteLine(“发生WebSphere MQ错误:{0}”,例如ToString());
}
catch(System.Exception-ex)
{
WriteLine(“发生系统错误:{0}”,例如ToString());
}
返回0;
}//开始结束
}//样本结束

您可以选择
MQC.TRANSPORT\u MQSERIES\u MANAGED
以使
amqmdnet.dll
在非托管模式下运行并依赖于其他非.NET dll,而不是选择
MQC.TRANSPORT\u MQSERIES\u MANAGED
以使其在托管模式下运行,这意味着它不需要任何其他dll来运行

在MQ v7.5中,即使在托管模式下,您也不能单独使用
amqmdnet.dll
,在该版本中,IBM不支持此配置。在IBM MQ v8和更高版本中,IBM支持单独使用
amqmdnet.dll
。您可以从下面的链接之一下载MQ v8或MQ v9可再发行客户端。只需从zip文件中找到
amqmdnet.dll
,然后使用它

MQ是完全向后兼容的,从更高版本的客户端连接到MQ 7.5队列管理器应该不是问题。还请注意,MQ v7.5将于2018年4月30日停止提供支持,为了保持IBM的支持,第三方将需要upg或
// ===========================================================================
// Licensed Materials - Property of IBM
// 5724-H72
// (c) Copyright IBM Corp. 2003, 2005
// ===========================================================================
using System;
using System.Collections;

using IBM.WMQ;

class MQSample
{
  // The type of connection to use, this can be:-
  // MQC.TRANSPORT_MQSERIES_BINDINGS for a server connection.
  // MQC.TRANSPORT_MQSERIES_CLIENT for a non-XA client connection
  // MQC.TRANSPORT_MQSERIES_XACLIENT for an XA client connection
  // MQC.TRANSPORT_MQSERIES_MANAGED for a managed client connection
  const String connectionType = MQC.TRANSPORT_MQSERIES_CLIENT;

  // Define the name of the queue manager to use (applies to all connections)
  const String qManager = "your_Q_manager";

  // Define the name of your host connection (applies to client connections only)
  const String hostName = "your_hostname";

  // Define the name of the channel to use (applies to client connections only)
  const String channel = "your_channelname";



  /// <summary>
  /// Initialise the connection properties for the connection type requested
  /// </summary>
  /// <param name="connectionType">One of the MQC.TRANSPORT_MQSERIES_ values</param>
  static Hashtable init(String connectionType)
  {
    Hashtable connectionProperties = new Hashtable();

    // Add the connection type
    connectionProperties.Add(MQC.TRANSPORT_PROPERTY, connectionType);

    // Set up the rest of the connection properties, based on the
    // connection type requested
    switch(connectionType)
    {
      case MQC.TRANSPORT_MQSERIES_BINDINGS:
        break;
      case MQC.TRANSPORT_MQSERIES_CLIENT:
      case MQC.TRANSPORT_MQSERIES_XACLIENT:
      case MQC.TRANSPORT_MQSERIES_MANAGED:
        connectionProperties.Add(MQC.HOST_NAME_PROPERTY, hostName);
        connectionProperties.Add(MQC.CHANNEL_PROPERTY, channel);
        break;
    }

    return connectionProperties;
  }
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static int Main(string[] args)
  {
    try
    {
      Hashtable connectionProperties = init(connectionType);

      // Create a connection to the queue manager using the connection
      // properties just defined
      MQQueueManager qMgr = new MQQueueManager(qManager, connectionProperties);

      // Set up the options on the queue we want to open
      int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;

      // Now specify the queue that we want to open,and the open options
      MQQueue system_default_local_queue =
        qMgr.AccessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE", openOptions);

      // Define a WebSphere MQ message, writing some text in UTF format
      MQMessage hello_world = new MQMessage();
      hello_world.WriteUTF("Hello World!");

      // Specify the message options
      MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,
                                                           // same as MQPMO_DEFAULT

      // Put the message on the queue
      system_default_local_queue.Put(hello_world, pmo);



      // Get the message back again

      // First define a WebSphere MQ message buffer to receive the message
      MQMessage retrievedMessage =new MQMessage();
      retrievedMessage.MessageId =hello_world.MessageId;

      // Set the get message options
      MQGetMessageOptions gmo =new MQGetMessageOptions(); //accept the defaults
                                                          //same as MQGMO_DEFAULT

      // Get the message off the queue
      system_default_local_queue.Get(retrievedMessage,gmo);

      // Prove we have the message by displaying the UTF message text
      String msgText = retrievedMessage.ReadUTF();
      Console.WriteLine("The message is: {0}", msgText);

      // Close the queue
      system_default_local_queue.Close();

      // Disconnect from the queue manager
      qMgr.Disconnect();
    }

    //If an error has occurred in the above,try to identify what went wrong.

    //Was it a WebSphere MQ error?
    catch (MQException ex)
    {
      Console.WriteLine("A WebSphere MQ error occurred: {0}", ex.ToString());
    }

    catch (System.Exception ex)
    {
      Console.WriteLine("A System error occurred: {0}", ex.ToString());
    }

    return 0;
  }//end of start
}//end of sample