Pickup 폴더내 파일의 복사본을 유지하는 방법

BizTalk Server 용 응용프로그램을 만들때 일반적으로 File Receive Location(또는 FTP Receive Location)을 이용하게 됩니다.

일단 메시지 파일을 Receive Location에서 설정된 Pickup 폴더에 복사하고 이파일이 없어지는 것을 보고 일단 메시지가 전달된 것으로 생각할 수 있습니다.

 

문제는 이 파일의 복사본을 유지하고 싶은 경우 현재 BizTalk Server가 지원하지 않는다는 것 입니다. 관련 기능을 원하시는 경우 아래의 CodePlex에서 제공되는 Message Archive Pipeline Component를 사용해 보시기 바랍니다.

 

대신 소스 코드가 같이 제공이 되고 있기 때문에 관련 Component에 기능추가 역시 가능 합니다. 물론 기능사용 전에 라이센스 역시 주의 해서 읽어 보시기 바랍니다.

 

BizTalk 2006 Message Archiving Pipeline Component

https://www.codeplex.com/btsmsgarchcomp

 

위의 URL에서 다운로드 가능한 컴퍼넌트를 BizTalk Server에 설치 하신 후 Receive/Send Pipeline의 Decode stage에 넣어주신 후 관련 컴퍼넌트의 Properties를 설정해 주시면 됩니다.

사용법은 다음과 같습니다.

 

1. 웹사이트에서 복사하신 파일의 압축을 푸신 후 Modhul.BizTalk.Pipelines.ArchiveMessages.dll 파일을 BizTalk 서버의 설치 폴더내 Pipeline Component 폴더 (일반적으로 C:\Program Files\Microsoft BizTalk Server 2006\Pipeline Components) 에 복사 하세요.

 

clip_image002

 

2. Message File의 Archive 기능이 필요한 BizTalk Application Project에서 Receive Pipeline 을 추가하시고 (이미 있는 경우는 다음으로 진행하세요)

 

3. 파이프 라인 파일을 여신 후 BizTalk Pipeline Components Toolxbox 에서 오른쪽 마우스 버튼을 클릭하셔서 “아이템 선택”을 클릭하시면 아래와 같은 다이얼로그 박스가 나타납니다.

 

4. 아래 그림과 같이 “BizTalk Pipeline Components” 탭을 선택 하시고 Browser 버튼을 선택하셔서 1번에서 복사한 폴더로 이동 하신 후 컴퍼넌트를 선택하세요.

 

clip_image004

 

5. 컴퍼넌트의 선택이 정상적으로 완료되면 아래와 같이 Toolbox 내에 “Archive Messages”라는 컴퍼넌트가 추가 됩니다.

 

clip_image006

 

6. 추가된 컴퍼넌트를 선택하신 후 Drag 하셔서 “Decode” stage내의 추가 하시기 바랍니다.

 

clip_image008

 

7. 선택된 컴퍼넌트의 설정가능한 속성은 아래와 같습니다. 저장될 폴더 위치와 파일 이름등이 제공되고 있습니다. 기존에 사용 중인 MacroDefinition 파일이 있으시면 File이나 FTP 관련 부분을 추가 하신 후 MacroDefinitionFile Property에 위치를 적어주셔도 됩니다.

 

clip_image010

 

좀더 자세한 기능은 Readme.txt파일을 확인하시기 바랍니다.

 

실제 파일을 저장하는 부분은 “ArchiveMessages.cs” 파일내의 StreamOnReadEvent EnentHandler에서 아래와 같이 코딩되어 있습니다.

 

private void StreamOnReadEvent(object src, EventArgs args)
{
ReadEventArgs rargs = args as ReadEventArgs;

    if (rargs != null)
{
try
{
            using (
FileStream FileArchiveStream =
new FileStream(_archiveFilePath, FileMode.Append, FileAccess.Write))
{
using (BinaryWriter FileBinaryWriter = new BinaryWriter(FileArchiveStream))
{
FileBinaryWriter.Write(rargs.buffer, 0, rargs.bytesRead);

                    // Close the file writer.
FileBinaryWriter.Flush();
FileBinaryWriter.Close();
}
}
}
catch (IOException Ex)
{
...
}
}
else
{
....

    }
}

 

파일이 저장되도록 하기 위한 Event Handler의 등록은 IComponent Interface의 Execute Method를 Implement하는 코드에 나타납니다.

 

IComponent.Execute Method

https://msdn.microsoft.com/en-us/library/microsoft.biztalk.component.interop.icomponent.execute.aspx

 

public IBaseMessage Execute(IPipelineContext Context, IBaseMessage InMsg)
{
// Validate the Macro Definition configuration file.
if (ValidationHelper.ValidateMacroDefinitionsXmlConfiguration(_macroDefinitionsFile))
{
// Validate number of macro identifiers.
if ((ValidationHelper.ValidateMacros(InMsg.MessageID, "Archive Directory", _archiveDirectoryMacroPath) &&
(ValidationHelper.ValidateMacros(InMsg.MessageID, "Archive Filename", _archiveFilenameMacroPath))))
{
            if (InMsg.BodyPart != null)
{
// Expand macros and build the archive file path.
_archiveFilePath = BuildArchiveFilePath(InMsg);

                // Wrap original stream in a Forward Only Event Stream (FOES) and
// configure the read-event.
Stream dataStream = InMsg.BodyPart.GetOriginalDataStream();
CForwardOnlyEventingReadStream eventingStream = new CForwardOnlyEventingReadStream(dataStream);
eventingStream.ReadEvent += new ReadEventHandler(StreamOnReadEvent);

                // Assign FOES wrapper back to the BodyPart.
InMsg.BodyPart.Data = eventingStream;
}
}
}

    return (InMsg);
}