---
title: "Create a custom processor for session end batching"
slug: "create-a-custom-processor-for-session-end-batching"
abstract: "How to create a custom processor to handle a batch of sessions that have expired."
---
# Create a custom processor for session end batching

From version 9.3, Sitecore processes expired sessions in batches instead of one at a time. If you extend the `commitSession` or `submitSessionContext` pipelines with additional processors, sessions are no longer necessarily submitted to xConnect the moment they expire. Therefore, we recommend that you change your custom processor to use the new service `Sitecore.Analytics.XConnect.DataAccess.IXdbRuntimeContext`, which provides access to a factory that creates a `Sitecore.XConnect.IXdbContext`, the interface of the [xConnect Client API](https://doc.sitecore.com/xp/en/developers/105/sitecore-experience-platform/xconnect-client-api--c--.html "xConnect Client API (C#)").

> [!note]
> For information on how to set session expiration batch settings, see [Configure session end batching](https://doc.sitecore.com/xp/en/developers/104/platform-administration-and-architecture/configure-session-end-batching.html).

To create a custom processor and then process session data in batches:

1. [Extend](https://doc.sitecore.com/xp/en/developers/104/platform-administration-and-architecture/configuration-patching.html) the `commitSession` or `submitSessionContext` pipeline using a custom processor:

    ```xml
    <commitSession> 
        <processor type="AddCustomContactDataOnCommitSession, CustomAssembly" patch:before="processor[@type='Sitecore.Analytics.Pipelines.CommitSession.SubmitSession, Sitecore.Analytics']" resolve="true" />
    </commitSession>
    ```

2. Add code to handle the session data. The following is an example of how you can change the first name of the contact in the session using `IXdbRuntimeContext`:

    ```csharp
    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
        }
    }
    ```

> [!note]
> You can also use `IXdbContext` events to react on successful and failed saves of session batches.