多个MQ侦听器无法正常工作

时间:2015-10-02 13:41:03

标签: c# listener ibm-mq mq

我正在尝试从我的C#代码连接到IBM MQ。

回调适用于一个侦听器(侦听队列)。即当消息进入队列时,正确调用回调方法。

我在同一个QueueManager / Channel上创建了另一个队列#2。使用不同的回调方法在此队列上注册了一个新的侦听器。服务器启动期间未发现任何错误。

当消息到达此队列#2时,将调用与第一个队列关联的回调,而不是我为此队列注册的回调。我尝试创建一个不同的会话,也尝试不同的连接,但行为仍然是相同的。

有人有什么想法吗?

class Program
  {
     static void Main(string[] args)
      {
           string env = "DEV"
    string queueName1= "Queue1"
    string queueName2 = "Queue2"

    new MyListener(CallbackHandler1.onMessage1, env, queueName1).RegisterListener();

    new MyListener(CallbackHandler2.onMessage2, env, queueName2).RegisterListener();
         }



  public class MyListener
  {
public delegate void Handler (IMessage msg)
public static Handler _handler

private string env = "";
private string queue = "";

public MyListner(Handler _handler, string environment, string queueName)
{
     _handler = _handler;
      this.env = environment;
      this.queue = queueName
}

public void RegisterListener()
{

    XMSFactoryFactory xff = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
    IConnectionFactory cf = xff.CreateConnectionFactory();
    cf.SetStringProperty(XMSC.WMQ_HOST_NAME, "10.87.188.156(7111)");
    cf.SetIntProperty(XMSC.WMQ_PORT, 7111);
    cf.SetStringProperty(XMSC.WMQ_CHANNEL, "QMEIGS1.CRM.SVRCONN");
    cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT_UNMANAGED);
    cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "QMEIGS1");

    IConnection conn = cf.CreateConnection();
    Console.WriteLine("connection created");
    ISession sess = conn.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
    IDestination dest = sess.CreateQueue(queue);
    IMessageConsumer consumer = sess.CreateConsumer(dest);
    MessageListener ml = new MessageListener(OnMessage);
    consumer.MessageListener = ml;
    conn.Start();
    Console.WriteLine("Consumer started");

}

private void onMessage(IMessage m)
{
    try {
        _handler(m)
        }
    catch (Exception e ) 
    {
    }
}
}


  //callback 1
  public class CallbackHandler1

  public static void onMessage1(IMessage msg)
     {
   ITextMessage textMessage = IMessage(msg)
  // code to perform onmessage1
     }


  //callback 2
  public class CallbackHandler2

     public static void onMessage2(IMessage msg)
     {
  ITextMessage textMessage = IMessage(msg)
  // code to perform onmessage2
     }

1 个答案:

答案 0 :(得分:0)

我不是一名核心c#程序员,但我认为问题在于static中的public static Handler _handler;关键字。关键字static只能在handler类的所有实例中使用_ MyListener的一个实例。虽然您正在创建两个使用者并分配两个侦听器,但第二次分配消息侦听器的调用将覆盖第一个侦听器。因此,您只从一个队列接收消息。

我修改了你的代码,因为它没有编译,并在删除static关键字后使其正常工作。我使用MQ v8编译程序。但版本对你来说应该不是问题。

namespace multilistener
{
    class Program
    {
        static void Main(string[] args)
        {
            string env = "DEV";
            string queueName1= "SUB.Q";
            string queueName2 = "SUB.Q1";

        new MyListener(CallbackHandler1.onMessage1, env, queueName1).RegisterListener();

        new MyListener(CallbackHandler2.onMessage2, env, queueName2).RegisterListener();
        System.Threading.Thread.Sleep(30000);
        Console.WriteLine("Program ends");
     }
}

public class MyListener
{
    public delegate void Handler (IMessage msg);
    public Handler _handler; // Remove 'static' keyword

    private string env = "";
    private string queue = "";

    public MyListener(Handler _Inhandler, string environment, string queueName)
    {
        _handler = _Inhandler;
        this.env = environment;
        this.queue = queueName;
    }

    public void RegisterListener()
    {
        try
        {

            XMSFactoryFactory xff = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
            IConnectionFactory cf = xff.CreateConnectionFactory();
            cf.SetStringProperty(XMSC.WMQ_HOST_NAME, "localhost");
            cf.SetIntProperty(XMSC.WMQ_PORT, 1414);
            cf.SetStringProperty(XMSC.WMQ_CHANNEL, "MY.SVRCONN");
            cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT);
            cf.SetStringProperty(XMSC.USERID, "userid");
            cf.SetStringProperty(XMSC.PASSWORD, "password");
            cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "QM1");

            IConnection conn = cf.CreateConnection();
            Console.WriteLine("connection created");
            ISession sess = conn.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
            IDestination dest = sess.CreateQueue(queue);
            IMessageConsumer consumer = sess.CreateConsumer(dest);
            MessageListener ml = new MessageListener(onMessage);
            consumer.MessageListener = ml;
            conn.Start();
            Console.WriteLine("Consumer started");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

    private void onMessage(IMessage m)
    {
        try {
            _handler(m);
        }
        catch (Exception e ) 
        {
            Console.Write(e);
        }
    }
}


//callback 1
public class CallbackHandler1
{
    public static void onMessage1(IMessage msg)
    {
        ITextMessage textMessage = (ITextMessage)msg;
        // code to perform onmessage1
        Console.WriteLine("First consumer");
    }
}

//callback 2
public class CallbackHandler2
{
    public static void onMessage2(IMessage msg)
    {
        ITextMessage textMessage = (ITextMessage)msg;
        // code to perform onmessage2
        Console.WriteLine("Second consumer");
    }
}

}