---
title: "セッションエンドバッチ用のカスタムプロセッサを作成します"
slug: "create-a-custom-processor-for-session-end-batching"
abstract: "期限切れのセッションのバッチを処理するカスタムプロセッサの作成方法。"
---
# セッションエンドバッチ用のカスタムプロセッサを作成します

バージョン9.3以降、Sitecoreは一度に1つではなくバッチ単位で処理するようになりました。commitSessionやパイプラインsubmitSessionContextを追加で拡張すると、セッションが期限切れした瞬間に必ずしもxConnectに提出されるわけではありません。したがって、カスタムプロセッサを新しいサービスSitecore.Analytics.XConnect.DataAccess.IXdbRuntimeContextに変更することをお勧めします。これは[xConnect Client API](https://doc.sitecore.com/xp/ja/developers/100/sitecore-experience-platform/xconnect-client-api--c--.html "xConnect Client API (C#)")のインターフェースであるSitecore.XConnect.IXdbContextへのアクセスを提供します。

> !注セッション期限バッチ設定の設定方法については、「 [セッション終了バッチの設定](https://doc.sitecore.com/xp/en/developers/100/platform-administration-and-architecture/configure-session-end-batching.html)」を参照してください。

カスタムプロセッサを作成し、セッションデータをバッチ処理するには:

1.  commitSessionsubmitSessionContext[パイプラインを拡張する:](https://doc.sitecore.com/xp/en/developers/100/platform-administration-and-architecture/configuration-patching.html)
    
    <commitSession> 
     <processor type="AddCustomContactDataOnCommitSession, CustomAssembly" patch:before="processor@type='Sitecore.Analytics.Pipelines.CommitSession.SubmitSession, Sitecore.Analytics'" resolve="true" />
    </commitSession>
    
2.  セッションデータを扱うコードを追加してください。以下は、IXdbRuntimeContextを使ってセッション内の連絡先の名前を変更する方法の例です:
    
    using System.Linq;
    using Sitecore.XConnect.Client;
    using Sitecore.XConnect.Collection.Model;
    
    public class AddCustomContactDataOnCommitSession : Sitecore.Analytics.Pipelines.CommitSession.CommitSessionProcessor
    {
     private readonly Sitecore.Analytics.XConnect.DataAccess.IXdbRuntimeContext \_context;
    
     // Injects IXdbRuntimeContext
     public AddCustomContactDataOnCommitSession(Sitecore.Analytics.XConnect.DataAccess.IXdbRuntimeContext context)
     {
     \_context = context;
     }
    
     public override void Process(Sitecore.Analytics.Pipelines.CommitSession.CommitSessionPipelineArgs args)
     {
     // This provides access to the instance of the IXdbContext that is used for batching
     var xdbContext = \_context.Factory.Create();
    
     // Check whether contact exists
     var identifiedContactReference = new Sitecore.XConnect.IdentifiedContactReference("<IdentifierSource>", "<identifier>");
    
     var xConnectContact = xdbContext.Get(identifiedContactReference, new Sitecore.XConnect.ExpandOptions
     {
     FacetKeys =
     {
     Sitecore.XConnect.Collection.Model.PersonalInformation.DefaultFacetKey
     }
     });
    
     if (xConnectContact == null)
     {
     return;
     }
    
     var personalInfo = xConnectContact.GetFacet<Sitecore.XConnect.Collection.Model.PersonalInformation>() ?? new Sitecore.XConnect.Collection.Model.PersonalInformation();
    
     personalInfo.FirstName = "<FirstName>";
    
     // Check that there facet is not modified twice
     if (xdbContext.DirectOperations.OfType<Sitecore.XConnect.Operations.SetFacetOperation<Sitecore.XConnect.Collection.Model.PersonalInformation>>().All(o => o.Target.Entity.Id != xConnectContact.Id))
     {
     xdbContext.SetPersonal(xConnectContact, personalInfo);
     }
    
     // Do not call xdbContext.Submit() as it is called by batching logic afterward
     }
    }
    

> !注セッションバッチの成功したセーブや失敗したセーブに対してIXdbContextイベントを活用することもできます。