• MSMQ消息大于4MB限制的解决办法


    MSMQ 消息发送大于 4 导致 System.Messaging.MessageQueueException Mb。

    察看本文应用于的产品

    社区解决方案免责声明

    作者 Carlos Walzer MVP

    注意:这篇文章是由无人工介入的自动的机器翻译系统翻译完成。这些文章是微软为不懂英语的用户提供的, 以使他们能够理解这些文章的内容。微软不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的使用所引起的任何直接的, 或间接的可能的问题负责。

    文章编号
    :
    555319

    最后修改
    :
    2005年5月24日

    修订
    :
    1.0

    概要

    本文介绍如何发送大于 4 文件使用 System.Messaging Mb。

    回到顶端

    症状

    当试图发送邮件包含超过 4 Mb 。 的数据是获得以下异常:
    : 不足资源无法执行操作不足资源无法执行操作 System.Messaging.MessageQueueException System.Messaging.MessageQueueException

    回到顶端

    原因

    MSMQ 消息有 不超过 4 MB 的数据 。 此限制是由于对内存映射文件由消息队列用来存储邮件数据。 这些内存映射文件存储队列所在计算机上 MSMQ\Storage 文件夹中。

    回到顶端

    解决方案

    为了解决这个限制您应该 brake 信息到小数据块 ; 并 多部分邮件 中发送。 多部分消息序列是流的几个邮件中发送数据。 如 MSMQ 协议不支持多部分邮件, 您需要建立自己算法来发送者和接收者之间交换它们。 许多技术能应用于解决此问题。 需要考虑中具有以下任何机制可能生成:
    1), 接收者需要知道多部分消息机制 两发送者。
    2) 组是 MSMQ 消息包含一个多部分消息需要他人标识。
    注意: 注意: 意向是本文是不能涵盖所有可用技术来解决此问题 ; 它们可能异通信业务模型
    A 普通示例, 如何发送文件大于 4 Mb 说明在下节。

    回到顶端

    更多信息

    以下示例显示如何发送大于 4 文件 使用 System.Messaging Mb。
    注意, 文件是中断到块的 4194000 字节, 因此是二进制数据使用 Message.BodyStream , 邮件正文中保存格式。

    using System;
    using System.Messaging;
    using System.IO;
    namespace FilesInMsmq
    {
     /// <summary>
     /// This example shows how to send and receive Files larger than 4 Mb. using MSMQ Messages.
     /// How to run the example:
     /// FilesInMsmq [send/receive] [File Name] [Queue Path]
     /// </summary>
     class MsmqFileExample
     {
      //break the mesage into 4 Mb chunks
      static int chunkSize = 4194000;
      [STAThread]
      static void Main(string[] args)
      {
       string fileName;
       string queuePath;
       if (args.Length > 0)
       {
        //get parameters from command prompt
        fileName = args[1];
        queuePath = args[2];
        switch(args[0])
        {
         case "send":
          SendFile(fileName, queuePath);
          break;
         case "receive":
          ReceiveFile(fileName, queuePath);
          break;
        }
       }
      }
      static void SendFile(string fileName, string queuePath)
      {
       int i;
       int count = 0;
       int msgNumber = 0;
       
       //Open an existing queue
       MessageQueue queue = new MessageQueue(queuePath);
       Message msg = new Message();
       try
       {
        //Open the file for reading
        using (FileStream fs = File.OpenRead(fileName))
        {
          // while there are bytes
         while((i = fs.ReadByte()) != -1)
         {
           // if count has reached size, send message
          if(count >= chunkSize)
          {
           msgNumber++;
           msg.AppSpecific = msgNumber;
           //Send the messsage
           queue.Send(msg);
           string nextMsgId = msg.Id;
           count = 0;
           //Create a new message
           msg = new Message();
           msg.CorrelationId = nextMsgId;
          }
          msg.BodyStream.WriteByte((byte)i);
          count++;            // from the original file
         }
         msgNumber++;
         msg.AppSpecific = msgNumber;
         //Send the last message
         queue.Send(msg);
        }
       }
       catch(Exception ex)
       {
        Console.WriteLine(ex);
       }
       finally
       {
        //release queue resources
        queue.Close();
       }
      }
      static void ReceiveFile(string fileName, string queuePath)
      {
       byte[] data;
       int length;
       
       //Open an existing queue
       MessageQueue queue = new MessageQueue(queuePath);
       try
       {
        //Open file for writing
        using (FileStream fs = File.OpenWrite(fileName))
        {
         //Receive the first message
         Message msg = queue.Receive(new TimeSpan(0,0,0,1));
         while (msg != null)
         {
          //Get the Lenght of the message body stream
          length = Convert.ToInt32(msg.BodyStream.Length);
          //Create a buffer to hold the stream in memory
          data = new byte[length];
          //Read the body stream
          msg.BodyStream.Read(data, 0, length);
          //Write the buffer into the file
          fs.Write(data, 0, length);
          //Receive following message
          msg = queue.Receive(new TimeSpan(0,0,0,1));
         }
        }
       }
       catch(Exception ex)
       {
        Console.WriteLine(ex);
       }
       finally
       {
        //release queue resources
        queue.Close();
       }
      }
     }
    }
  • 相关阅读:
    实验四——多分支结构及本章总结
    实验五——循环结构学习总结
    实验12——指针的基础应用 2
    实验11——指针的基础应用
    实验十——一维数组的定义及引用
    实验九——基本数据类型存储及应用总结
    实验八——函数定义及调用总结
    实验七——函数定义及调用总结
    实验六——循环结构程序练习总结
    实验五——循环结构学习总结
  • 原文地址:https://www.cnblogs.com/neozhu/p/643753.html
Copyright © 2020-2023  润新知