• How to use the MQ?


    How to use the MQ?
    ===================================================================================
    Here is an example to use the MQ:

    using System;
    using System.Messaging;
    using System.Drawing;
    using System.IO;

    namespace ConsoleApplication1
    {

    ---------------------------------------------------
    public class Order
    {
    public int orderId;
    public DateTime orderTime;
    };

    ---------------------------------------------------
    public class MyNewQueue
    {

    public static void Main()
    {
    MyNewQueue myNewQueue = new MyNewQueue();
    CreateQueue(".\\myQueue");

    myNewQueue.SendMessage();

    // Receive a message from a queue.
    myNewQueue.ReceiveMessage();

    return;
    }


    public static void CreateQueue(string queuePath)
    {
    try
    {
    if(!MessageQueue.Exists(queuePath))
    {
    MessageQueue.Create(@".\private$\myQueue");
    }
    else
    {
    Console.WriteLine(queuePath + " already exists.");
    }
    }
    catch (MessageQueueException e)
    {
    Console.WriteLine(e.Message);
    }
    }


    public void SendMessage()
    {
    try
    {
    Order sentOrder = new Order();
    sentOrder.orderId = 3;
    sentOrder.orderTime = DateTime.Now;
    MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
    Message myMessage = new Message(sentOrder);
    myQueue.Send(myMessage);
    }
    catch(ArgumentException e)
    {
    Console.WriteLine(e.Message);
    }

    return;
    }


    public void ReceiveMessage()
    {

    MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
    myQueue.Formatter = new XmlMessageFormatter(new Type[]{typeof(ConsoleApplication1.Order)});
    try
    {
    Message myMessage = myQueue.Receive();
    Order myOrder = (Order)myMessage.Body;
    Console.WriteLine("Order ID: " +
    myOrder.orderId.ToString());
    Console.WriteLine("Sent: " +
    myOrder.orderTime.ToString());
    }
    catch (MessageQueueException)
    {
    }
    catch (InvalidOperationException e)
    {
    Console.WriteLine(e.Message);
    }

    return;
    }
    }
    ---------------------------------------------------

    }

  • 相关阅读:
    SSO单点登录机制
    Web应用中Service层获取request对象 | RequestContextHolder的使用
    J2EE中数据字典的使用详解
    Redis高级(事务,持久化,主从复制读写分离,以及安全设置)
    Redis的五种数据类性以及对应的操作命令
    Redis客户端与基本命令
    VMware15安装CentOS8
    用内置的库turtle来画一朵花,python3
    python之经典猜数字
    python,寻找班级里面名字最长的人
  • 原文地址:https://www.cnblogs.com/RobotTech/p/531107.html
Copyright © 2020-2023  润新知