C#中的消息驱动bean [英] Message driven bean in C#

查看:162
本文介绍了C#中的消息驱动bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以建议我用C#.net编写一个消息驱动的bean来监听MQ并进行处理的代码.

Can any one suggest me code to write a message driven bean in C#.net to listen a MQ and process the same.

推荐答案

XMS与JMS非常相似.这是使用XMS的C#中消息侦听器的世界您好"示例.请包括来自Websphere mq安装的参考IBM.XMS.dll.

XMS will be pretty similar to JMS. This is a "hello, world" example of a message listener in C# using XMS. Please include the reference IBM.XMS.dll from your websphere mq installation.

在我的Windows安装(32位)上,它是

On my windows installation, 32bit, it was

c:\Program Files\IBM\WebSphere MQ\bin\IBM.XMS.dll

此示例假定了一些硬编码设置,没有错误处理,但我想您明白了.

This sample assumes a few hardcoded settings and have no error handling, but I think you get the idea.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBM.XMS;

namespace XMSTest
{
    class MyXmsApp
    {
        static void Main(string[] args)
        {
            MyXmsApp app = new MyXmsApp();
            app.Setup();
            Console.ReadLine();
        }

        public void Setup()
        {
            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, "CLIENT");
            cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT);
            cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "QM_LOCAL");
            cf.SetIntProperty(XMSC.WMQ_BROKER_VERSION, XMSC.WMQ_BROKER_V1);

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

        private void OnMessage(IMessage msg)
        {
            ITextMessage textMsg = (ITextMessage)msg;
            Console.Write("Got a message: ");
            Console.WriteLine(textMsg.Text);
        }
    }
}

这篇关于C#中的消息驱动bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆