Biztalk Server zip unzip pipeline component Development

 

Biztalk Server zip unzip pipeline component Development

最近有个B2B的项目涉及和其他合作伙伴(partner)作数据传输,我们这边使用的开发平台(platform)是Biztalk Server 2006,数据传输管道(channel)采用window server 2003的MSMQ,但是由于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文档的压缩比达到10:1)

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 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>解压
  2InBlock.gif
  3InBlock.gif        /// called by the messaging engine when a new message arrives
  4InBlock.gif
  5InBlock.gif        /// </summary>
  6InBlock.gif
  7InBlock.gif        /// <param name="pc">the pipeline context</param>
  8InBlock.gif
  9ExpandedBlockEnd.gif        /// <param name="inmsg">the actual message</param>

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

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

114InBlock.gif
115InBlock.gif 
116InBlock.gif
117InBlock.gif                    if (this.Namespace != null && this.RootElementName != null)
118InBlock.gif
119ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
120InBlock.gif
121InBlock.gif                        string messageType = string.Format("{0}#{1}"this.Namespace, this.RootElementName);
122InBlock.gif
123InBlock.gif                        outMsg.Context.Promote("MessageType""http://schemas.microsoft.com/BizTalk/2003/system-properties", messageType);
124InBlock.gif
125ExpandedSubBlockEnd.gif                    }

126InBlock.gif
127InBlock.gif                    _msgs.Enqueue(outMsg);
128InBlock.gif
129InBlock.gif                    sEntry = oZipStream.GetNextEntry();
130InBlock.gif
131ExpandedSubBlockEnd.gif                }

132InBlock.gif
133ExpandedSubBlockEnd.gif            }

134InBlock.gif
135InBlock.gif            catch (Exception ex)
136InBlock.gif
137ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
138InBlock.gif
139InBlock.gif                Debug.Write(ex.Message + Environment.NewLine + ex.StackTrace);
140InBlock.gif
141InBlock.gif                throw;
142InBlock.gif
143ExpandedSubBlockEnd.gif            }

144InBlock.gif
145InBlock.gif 
146InBlock.gif
147ExpandedBlockEnd.gif        }

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

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

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

264InBlock.gif
265InBlock.gif            catch (Exception e)
266InBlock.gif
267ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
268InBlock.gif
269InBlock.gif                Debug.WriteLine("[zipAssember] " + e.Message);
270InBlock.gif
271InBlock.gif                throw e;
272InBlock.gif
273ExpandedSubBlockEnd.gif            }

274InBlock.gif
275ExpandedBlockEnd.gif        }

276 None.gif

转载于:https://www.cnblogs.com/neozhu/archive/2007/03/13/672885.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值