Biztalk Server zip unzip pipeline component Development

 

Biztalk Server zip unzip pipeline component Development

最近有个B2B的项目涉及和其他合作伙伴(partner)作数据传输,我们这边使用的开发平台(platform)Biztalk Server 2006,数据传输管道(channel)采用window server 2003MSMQ,但是由于MSMQ本身存在单个消息有4M限制的问题,虽软Biztalk Server 2006自带的MSMQ Adapter已经对大消息(Large Data)的支持,提供了[Support segmentation] if true , message larger than 4095KB(approximately 4MB) will be segmented,说明当发送单个报文实例(Instance)超过4MB的时候可以在发送至MSMQ对列的时候进行分割成几个小的消息发送,这些被分割的消息之间通过Message. CorrelationId进行关联具体的做法可以参考《MSMQ消息大于4MB限制的解决办法》采用对消息进行分割的做法会对接收消息需要作特定的判断,相对于对报文压缩来得比较简单;

下面介绍一下如何通过对Biztalk Pipeline的二次开发实现对报文进行压缩/解压得实现;

功能描述:

1/Biztalk 流程(Orchestration)出来的消息在发送端口通过加载pipeline组件实现将消息以zip的方式进行压缩(zip可以对Xml,txt文档的压缩比达到101

2/将接收的zip文件(支持包含多个文件批处理(batch))进行压缩后进入Biztalk流程(Orchestration)处理;

                    

具体实现:

要实现对Biztalk Pipeline的开发对如下接口[Microsoft.BizTalk.Component.Interop.IComponent,IBaseComponent, IPersistPropertyBag, IComponentUI]做实现,好在现在网上提供pipeline component wizradhttp://www.gotdotnet.com/Workspaces/Workspace.aspx?id=1d4f7d6b-7d27-4f05-a8ee-48cfcd5abf4a 可以下载到pipeline开发向导

实现对文件进行压缩/解压需要的[ICSharpCode.SharpZipLib.dll]目前版本0.85相当稳定;下载地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/ 具体方法请查看版本

通过安装pipeline component wizrad之后就可以在vs.net中创建你的pipeline component组件了。CategoryTypes表示该组件可以加载到pipline的什么位置CategoryTypes.CATID_Any表示任何位置都可以放;

    [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]

    [System.Runtime.InteropServices.Guid("62656b9b-7d69-407d-b71f-d3c0415c82af")]

    [ComponentCategory(CategoryTypes.CATID_DisassemblingParser)]

    public class UnzipDisassemblerComponent : Microsoft.BizTalk.Component.Interop.IDisassemblerComponent, IBaseComponent, IPersistPropertyBag, IComponentUI

    {

 

        private System.Resources.ResourceManager resourceManager = new System.Resources.ResourceManager("Execution.BizTalk.Common.Pipelines.UnzipDisassemblerPipeline", Assembly.GetExecutingAssembly());

 

        #region IBaseComponent members

 

下面是对Biztalk消息进行解压/压缩的代码实现。

       

  1 Biztalk Server zip unzip pipeline component Development /// <summary>解压
  2Biztalk Server zip unzip pipeline component Development
  3Biztalk Server zip unzip pipeline component Development        /// called by the messaging engine when a new message arrives
  4Biztalk Server zip unzip pipeline component Development
  5Biztalk Server zip unzip pipeline component Development        /// </summary>
  6Biztalk Server zip unzip pipeline component Development
  7Biztalk Server zip unzip pipeline component Development        /// <param name="pc">the pipeline context</param>
  8Biztalk Server zip unzip pipeline component Development
  9Biztalk Server zip unzip pipeline component Development        /// <param name="inmsg">the actual message</param>

 10 Biztalk Server zip unzip pipeline component Development
 11 Biztalk Server zip unzip pipeline component Development         public   void  Disassemble(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
 12 Biztalk Server zip unzip pipeline component Development
 13 Biztalk Server zip unzip pipeline component Development         {
 14Biztalk Server zip unzip pipeline component Development
 15Biztalk Server zip unzip pipeline component Development 
 16Biztalk Server zip unzip pipeline component Development
 17Biztalk Server zip unzip pipeline component Development            Stream strmZipFile;
 18Biztalk Server zip unzip pipeline component Development
 19Biztalk Server zip unzip pipeline component Development 
 20Biztalk Server zip unzip pipeline component Development
 21Biztalk Server zip unzip pipeline component Development            IBaseMessagePart msgPart;
 22Biztalk Server zip unzip pipeline component Development
 23Biztalk Server zip unzip pipeline component Development            msgPart = inmsg.BodyPart;
 24Biztalk Server zip unzip pipeline component Development
 25Biztalk Server zip unzip pipeline component Development            strmZipFile = msgPart.GetOriginalDataStream();
 26Biztalk Server zip unzip pipeline component Development
 27Biztalk Server zip unzip pipeline component Development 
 28Biztalk Server zip unzip pipeline component Development
 29Biztalk Server zip unzip pipeline component Development            ZipInputStream oZipStream = new ZipInputStream(strmZipFile);
 30Biztalk Server zip unzip pipeline component Development
 31Biztalk Server zip unzip pipeline component Development            if (!string.IsNullOrEmpty(mPassword))
 32Biztalk Server zip unzip pipeline component Development
 33Biztalk Server zip unzip pipeline component Development                oZipStream.Password = mPassword;
 34Biztalk Server zip unzip pipeline component Development
 35Biztalk Server zip unzip pipeline component Development 
 36Biztalk Server zip unzip pipeline component Development
 37Biztalk Server zip unzip pipeline component Development            try
 38Biztalk Server zip unzip pipeline component Development
 39Biztalk Server zip unzip pipeline component Development            {
 40Biztalk Server zip unzip pipeline component Development
 41Biztalk Server zip unzip pipeline component Development 
 42Biztalk Server zip unzip pipeline component Development
 43Biztalk Server zip unzip pipeline component Development                ZipEntry sEntry = oZipStream.GetNextEntry();
 44Biztalk Server zip unzip pipeline component Development
 45Biztalk Server zip unzip pipeline component Development                while (sEntry != null )
 46Biztalk Server zip unzip pipeline component Development
 47Biztalk Server zip unzip pipeline component Development                {
 48Biztalk Server zip unzip pipeline component Development
 49Biztalk Server zip unzip pipeline component Development                    if (sEntry.IsDirectory)
 50Biztalk Server zip unzip pipeline component Development
 51Biztalk Server zip unzip pipeline component Development                    {
 52Biztalk Server zip unzip pipeline component Development
 53Biztalk Server zip unzip pipeline component Development                        sEntry = oZipStream.GetNextEntry();
 54Biztalk Server zip unzip pipeline component Development
 55Biztalk Server zip unzip pipeline component Development                        continue;
 56Biztalk Server zip unzip pipeline component Development
 57Biztalk Server zip unzip pipeline component Development                    }

 58Biztalk Server zip unzip pipeline component Development
 59Biztalk Server zip unzip pipeline component Development 
 60Biztalk Server zip unzip pipeline component Development
 61Biztalk Server zip unzip pipeline component Development                    MemoryStream strmMem = new MemoryStream();
 62Biztalk Server zip unzip pipeline component Development
 63Biztalk Server zip unzip pipeline component Development                    byte[] buffer = new byte[4096];
 64Biztalk Server zip unzip pipeline component Development
 65Biztalk Server zip unzip pipeline component Development                    int count = 0;
 66Biztalk Server zip unzip pipeline component Development
 67Biztalk Server zip unzip pipeline component Development                    while ((count = oZipStream.Read(buffer, 0, buffer.Length)) != 0)
 68Biztalk Server zip unzip pipeline component Development
 69Biztalk Server zip unzip pipeline component Development                        strmMem.Write(buffer, 0, count);
 70Biztalk Server zip unzip pipeline component Development
 71Biztalk Server zip unzip pipeline component Development 
 72Biztalk Server zip unzip pipeline component Development
 73Biztalk Server zip unzip pipeline component Development                    strmMem.Seek(0, SeekOrigin.Begin);
 74Biztalk Server zip unzip pipeline component Development
 75Biztalk Server zip unzip pipeline component Development                    msgPart.Data = strmMem;
 76Biztalk Server zip unzip pipeline component Development
 77Biztalk Server zip unzip pipeline component Development 
 78Biztalk Server zip unzip pipeline component Development
 79Biztalk Server zip unzip pipeline component Development                    IBaseMessage outMsg;
 80Biztalk Server zip unzip pipeline component Development
 81Biztalk Server zip unzip pipeline component Development 
 82Biztalk Server zip unzip pipeline component Development
 83Biztalk Server zip unzip pipeline component Development                    outMsg = pc.GetMessageFactory().CreateMessage();
 84Biztalk Server zip unzip pipeline component Development
 85Biztalk Server zip unzip pipeline component Development                    outMsg.AddPart("Body", pc.GetMessageFactory().CreateMessagePart(), true);
 86Biztalk Server zip unzip pipeline component Development
 87Biztalk Server zip unzip pipeline component Development                    outMsg.BodyPart.Data = strmMem;
 88Biztalk Server zip unzip pipeline component Development
 89Biztalk Server zip unzip pipeline component Development 
 90Biztalk Server zip unzip pipeline component Development
 91Biztalk Server zip unzip pipeline component Development                    for (int iProp = 0; iProp < inmsg.Context.CountProperties; iProp++)
 92Biztalk Server zip unzip pipeline component Development
 93Biztalk Server zip unzip pipeline component Development                    {
 94Biztalk Server zip unzip pipeline component Development
 95Biztalk Server zip unzip pipeline component Development                        string strName;
 96Biztalk Server zip unzip pipeline component Development
 97Biztalk Server zip unzip pipeline component Development                        string strNSpace;
 98Biztalk Server zip unzip pipeline component Development
 99Biztalk Server zip unzip pipeline component Development 
100Biztalk Server zip unzip pipeline component Development
101Biztalk Server zip unzip pipeline component Development                        object val = inmsg.Context.ReadAt(iProp, out strName, out strNSpace);
102Biztalk Server zip unzip pipeline component Development
103Biztalk Server zip unzip pipeline component Development                        // If the property has been promoted, respect the settings
104Biztalk Server zip unzip pipeline component Development
105Biztalk Server zip unzip pipeline component Development                        if (inmsg.Context.IsPromoted(strName, strNSpace))
106Biztalk Server zip unzip pipeline component Development
107Biztalk Server zip unzip pipeline component Development                            outMsg.Context.Promote(strName, strNSpace, val);
108Biztalk Server zip unzip pipeline component Development
109Biztalk Server zip unzip pipeline component Development                        else
110Biztalk Server zip unzip pipeline component Development
111Biztalk Server zip unzip pipeline component Development                            outMsg.Context.Write(strName, strNSpace, val);
112Biztalk Server zip unzip pipeline component Development
113Biztalk Server zip unzip pipeline component Development                    }

114Biztalk Server zip unzip pipeline component Development
115Biztalk Server zip unzip pipeline component Development 
116Biztalk Server zip unzip pipeline component Development
117Biztalk Server zip unzip pipeline component Development                    if (this.Namespace != null && this.RootElementName != null)
118Biztalk Server zip unzip pipeline component Development
119Biztalk Server zip unzip pipeline component Development                    {
120Biztalk Server zip unzip pipeline component Development
121Biztalk Server zip unzip pipeline component Development                        string messageType = string.Format("{0}#{1}"this.Namespace, this.RootElementName);
122Biztalk Server zip unzip pipeline component Development
123Biztalk Server zip unzip pipeline component Development                        outMsg.Context.Promote("MessageType""http://schemas.microsoft.com/BizTalk/2003/system-properties", messageType);
124Biztalk Server zip unzip pipeline component Development
125Biztalk Server zip unzip pipeline component Development                    }

126Biztalk Server zip unzip pipeline component Development
127Biztalk Server zip unzip pipeline component Development                    _msgs.Enqueue(outMsg);
128Biztalk Server zip unzip pipeline component Development
129Biztalk Server zip unzip pipeline component Development                    sEntry = oZipStream.GetNextEntry();
130Biztalk Server zip unzip pipeline component Development
131Biztalk Server zip unzip pipeline component Development                }

132Biztalk Server zip unzip pipeline component Development
133Biztalk Server zip unzip pipeline component Development            }

134Biztalk Server zip unzip pipeline component Development
135Biztalk Server zip unzip pipeline component Development            catch (Exception ex)
136Biztalk Server zip unzip pipeline component Development
137Biztalk Server zip unzip pipeline component Development            {
138Biztalk Server zip unzip pipeline component Development
139Biztalk Server zip unzip pipeline component Development                Debug.Write(ex.Message + Environment.NewLine + ex.StackTrace);
140Biztalk Server zip unzip pipeline component Development
141Biztalk Server zip unzip pipeline component Development                throw;
142Biztalk Server zip unzip pipeline component Development
143Biztalk Server zip unzip pipeline component Development            }

144Biztalk Server zip unzip pipeline component Development
145Biztalk Server zip unzip pipeline component Development 
146Biztalk Server zip unzip pipeline component Development
147Biztalk Server zip unzip pipeline component Development        }

148 Biztalk Server zip unzip pipeline component Development
149 Biztalk Server zip unzip pipeline component Development 
150 Biztalk Server zip unzip pipeline component Development
151 Biztalk Server zip unzip pipeline component Development // 压缩消息
152 Biztalk Server zip unzip pipeline component Development
153 Biztalk Server zip unzip pipeline component Development /// <summary>
154Biztalk Server zip unzip pipeline component Development
155Biztalk Server zip unzip pipeline component Development        /// Implements IComponent.Execute method.
156Biztalk Server zip unzip pipeline component Development
157Biztalk Server zip unzip pipeline component Development        /// </summary>
158Biztalk Server zip unzip pipeline component Development
159Biztalk Server zip unzip pipeline component Development        /// <param name="pc">Pipeline context</param>
160Biztalk Server zip unzip pipeline component Development
161Biztalk Server zip unzip pipeline component Development        /// <param name="inmsg">Input message</param>
162Biztalk Server zip unzip pipeline component Development
163Biztalk Server zip unzip pipeline component Development        /// <returns>Original input message</returns>
164Biztalk Server zip unzip pipeline component Development
165Biztalk Server zip unzip pipeline component Development        /// <remarks>
166Biztalk Server zip unzip pipeline component Development
167Biztalk Server zip unzip pipeline component Development        /// IComponent.Execute method is used to initiate
168Biztalk Server zip unzip pipeline component Development
169Biztalk Server zip unzip pipeline component Development        /// the processing of the message in this pipeline component.
170Biztalk Server zip unzip pipeline component Development
171Biztalk Server zip unzip pipeline component Development        /// </remarks>

172 Biztalk Server zip unzip pipeline component Development
173 Biztalk Server zip unzip pipeline component Development         public  Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(
174 Biztalk Server zip unzip pipeline component Development
175 Biztalk Server zip unzip pipeline component Development            Microsoft.BizTalk.Component.Interop.IPipelineContext pc, 
176 Biztalk Server zip unzip pipeline component Development
177 Biztalk Server zip unzip pipeline component Development            Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
178 Biztalk Server zip unzip pipeline component Development
179 Biztalk Server zip unzip pipeline component Development         {
180Biztalk Server zip unzip pipeline component Development
181Biztalk Server zip unzip pipeline component Development            // 
182Biztalk Server zip unzip pipeline component Development
183Biztalk Server zip unzip pipeline component Development            // TODO: implement component logic
184Biztalk Server zip unzip pipeline component Development
185Biztalk Server zip unzip pipeline component Development            
186Biztalk Server zip unzip pipeline component Development
187Biztalk Server zip unzip pipeline component Development            //string[] filenames = Directory.GetFiles(args[0]);
188Biztalk Server zip unzip pipeline component Development
189Biztalk Server zip unzip pipeline component Development            byte[] buffer = new byte[4096];
190Biztalk Server zip unzip pipeline component Development
191Biztalk Server zip unzip pipeline component Development            string fileName = string.Empty;
192Biztalk Server zip unzip pipeline component Development
193Biztalk Server zip unzip pipeline component Development            Stream strmZipFile=new MemoryStream();
194Biztalk Server zip unzip pipeline component Development
195Biztalk Server zip unzip pipeline component Development            Stream strmOriginFile;
196Biztalk Server zip unzip pipeline component Development
197Biztalk Server zip unzip pipeline component Development            Stream strmZip; ;
198Biztalk Server zip unzip pipeline component Development
199Biztalk Server zip unzip pipeline component Development            IBaseMessagePart msgPart;
200Biztalk Server zip unzip pipeline component Development
201Biztalk Server zip unzip pipeline component Development            msgPart = inmsg.BodyPart;
202Biztalk Server zip unzip pipeline component Development
203Biztalk Server zip unzip pipeline component Development            strmOriginFile = msgPart.GetOriginalDataStream();
204Biztalk Server zip unzip pipeline component Development
205Biztalk Server zip unzip pipeline component Development            try
206Biztalk Server zip unzip pipeline component Development
207Biztalk Server zip unzip pipeline component Development            {
208Biztalk Server zip unzip pipeline component Development
209Biztalk Server zip unzip pipeline component Development 
210Biztalk Server zip unzip pipeline component Development
211Biztalk Server zip unzip pipeline component Development                using (ZipOutputStream s = new ZipOutputStream(strmZipFile))
212Biztalk Server zip unzip pipeline component Development
213Biztalk Server zip unzip pipeline component Development                {
214Biztalk Server zip unzip pipeline component Development
215Biztalk Server zip unzip pipeline component Development 
216Biztalk Server zip unzip pipeline component Development
217Biztalk Server zip unzip pipeline component Development 
218Biztalk Server zip unzip pipeline component Development
219Biztalk Server zip unzip pipeline component Development                    s.SetLevel(9); // 0 - store only to 9 - means best compression
220Biztalk Server zip unzip pipeline component Development
221Biztalk Server zip unzip pipeline component Development                    //fileName = Guid.NewGuid().ToString() + "." + this.extension;
222Biztalk Server zip unzip pipeline component Development
223Biztalk Server zip unzip pipeline component Development                    fileName = inmsg.Context.Read("ReceivedFileName""http://schemas.microsoft.com/BizTalk/2003/" + messageContext.InboundTransportType.ToLower() + "-properties").ToString();
224Biztalk Server zip unzip pipeline component Development
225Biztalk Server zip unzip pipeline component Development                    s.Password = this.password;
226Biztalk Server zip unzip pipeline component Development
227Biztalk Server zip unzip pipeline component Development                    ZipEntry entry = new ZipEntry(fileName);
228Biztalk Server zip unzip pipeline component Development
229Biztalk Server zip unzip pipeline component Development                    s.PutNextEntry(entry);
230Biztalk Server zip unzip pipeline component Development
231Biztalk Server zip unzip pipeline component Development                    StreamUtils.Copy(strmOriginFile, s, buffer);
232Biztalk Server zip unzip pipeline component Development
233Biztalk Server zip unzip pipeline component Development                    s.Finish();
234Biztalk Server zip unzip pipeline component Development
235Biztalk Server zip unzip pipeline component Development                    strmZip = new MemoryStream();
236Biztalk Server zip unzip pipeline component Development
237Biztalk Server zip unzip pipeline component Development                    strmZipFile.Seek(0, SeekOrigin.Begin);
238Biztalk Server zip unzip pipeline component Development
239Biztalk Server zip unzip pipeline component Development                    StreamUtils.Copy(strmZipFile, strmZip, buffer);
240Biztalk Server zip unzip pipeline component Development
241Biztalk Server zip unzip pipeline component Development                    strmZip.Seek(0, SeekOrigin.Begin);
242Biztalk Server zip unzip pipeline component Development
243Biztalk Server zip unzip pipeline component Development                    s.Close();
244Biztalk Server zip unzip pipeline component Development
245Biztalk Server zip unzip pipeline component Development 
246Biztalk Server zip unzip pipeline component Development
247Biztalk Server zip unzip pipeline component Development 
248Biztalk Server zip unzip pipeline component Development
249Biztalk Server zip unzip pipeline component Development                }

250Biztalk Server zip unzip pipeline component Development
251Biztalk Server zip unzip pipeline component Development 
252Biztalk Server zip unzip pipeline component Development
253Biztalk Server zip unzip pipeline component Development                msgPart.Data = strmZip;
254Biztalk Server zip unzip pipeline component Development
255Biztalk Server zip unzip pipeline component Development                pc.ResourceTracker.AddResource(strmZip);
256Biztalk Server zip unzip pipeline component Development
257Biztalk Server zip unzip pipeline component Development                // this way, it's a passthrough pipeline component
258Biztalk Server zip unzip pipeline component Development
259Biztalk Server zip unzip pipeline component Development 
260Biztalk Server zip unzip pipeline component Development
261Biztalk Server zip unzip pipeline component Development                return inmsg;
262Biztalk Server zip unzip pipeline component Development
263Biztalk Server zip unzip pipeline component Development            }

264Biztalk Server zip unzip pipeline component Development
265Biztalk Server zip unzip pipeline component Development            catch (Exception e)
266Biztalk Server zip unzip pipeline component Development
267Biztalk Server zip unzip pipeline component Development            {
268Biztalk Server zip unzip pipeline component Development
269Biztalk Server zip unzip pipeline component Development                Debug.WriteLine("[zipAssember] " + e.Message);
270Biztalk Server zip unzip pipeline component Development
271Biztalk Server zip unzip pipeline component Development                throw e;
272Biztalk Server zip unzip pipeline component Development
273Biztalk Server zip unzip pipeline component Development            }

274Biztalk Server zip unzip pipeline component Development
275Biztalk Server zip unzip pipeline component Development        }

276 Biztalk Server zip unzip pipeline component Development

你可能感兴趣的:(component)