前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MSMQ消息大于4MB限制的解决办法

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

作者头像
阿新
发布2018-04-13 10:03:06
1.2K0
发布2018-04-13 10:03:06
举报
文章被收录于专栏:c#开发者c#开发者c#开发者

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();
   }
  }
 }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2007-02-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • MSMQ 消息发送大于 4 导致 System.Messaging.MessageQueueException Mb。
    • 概要
      • 症状
        • 解决方案
          • 更多信息
          相关产品与服务
          机器翻译
          机器翻译(Tencent Machine Translation,TMT)结合了神经机器翻译和统计机器翻译的优点,从大规模双语语料库自动学习翻译知识,实现从源语言文本到目标语言文本的自动翻译,目前可支持十余种语言的互译。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档