Thursday, March 14, 2019

Event Queue in Sitecore 9.1

Event queue

The event queue is a very useful mechanism in the Sitecore architecture. John Rappel wrote a very nice blogpost on it a while ago (I really do suggest you to read that). But since that day Sitecore came with version 9.1.

In one of our projects we used the event queue to send events to the ContentDelivery servers. The default/system Event Queue was used and it worked fine.  You can find the configuration on these event queues in the <eventing> section in Sitecore.config. The configuration includes a systemDatabase (which is deprecated now) and also a defaultEventQueue.

The configuration and code behind it seem to indicate that Sitecore is slowly refactoring this part and moving towards the <eventqueueprovider>  configuration instead of the <providers>.

Sitecore 9.1

After upgrading to Sitecore 9.1 however, we noticed there is a slight change of plans.. The release notes of 9.1 mention:
The Content Delivery server role does not require the Core database when the Sitecore security membership provider is not used.​​
The documentation on the core database still mentions the ContentDelivery role as one of the roles that refers to the core database, but in this case of the eventQueue you can (should) consider it gone...

Configuration

Let's have a look at the configuration in a Sitecore 9.1:
<eventing defaultProvider="sitecore">
  <providers>
    <clear />
    <!--
      Event provider attributes:
 systemDatabaseName: Deprecated. Name of the database which will hold the system event queue. 
   System event queue receives all the events that are not related to a specific database.
    -->
    <add name="sitecore" type="Sitecore.Eventing.EventProvider, Sitecore.Kernel" systemDatabaseName="core" />
    <add name="sitecore" role:require="ContentDelivery">
   <patch:attribute name="systemDatabaseName">web</patch:attribute>
    </add>
  </providers>
  <!-- EVENT QUEUE -->
  <eventQueueProvider defaultEventQueue="core">
    <eventQueue name="web" type="Sitecore.Data.Eventing.$(database)EventQueue, Sitecore.Kernel">...</eventQueue>
    <eventQueue name="master" role:require="!ContentDelivery" type="Sitecore.Data.Eventing.$(database)EventQueue, Sitecore.Kernel">...</eventQueue>
    <eventQueue name="core" role:require="!ContentDelivery" type="Sitecore.Data.Eventing.$(database)EventQueue, Sitecore.Kernel">...</eventQueue>
  </eventQueueProvider>
  <eventQueueProvider role:require="ContentDelivery">
    <patch:attribute name="defaultEventQueue">web</patch:attribute>
  </eventQueueProvider>
</eventing>

A few things have changed since the previous version here. Note the patches for the ContentDelivery role:

  • systemDatabaseName is set to web instead of core
  • defaultEventQueue is set to web instead of core
  • core and master queues are removed from the eventQueueProvider

As this is only done on the ContentDelivery and not on the other environments, one line in the (still excellent) blog post from John doesn't seem to fit anymore: "Sitecore can no longer assume all web servers in a cluster to have the same database set as the systemDatabase".

Sitecore will handle this nicely (assumption) but in our code we had to make a few small changes.

Coding for a web event queue

Subscribing to the event queue doesn't change as you subscribe to all event queues available with the EventManager.  

Queuing an event however did slightly change as we now push the event to a specific queue. Note that our events are pushed from the ContentManagent role which still has core as the default eventQueue to a ContentDelivery role that has no access to the core queue anymore. So, we need to push to the queue related to the database we want to reach:
var database = Sitecore.Configuration.Factory.GetDatabase(databaseName);
var eventQueue = database.RemoteEvents.EventQueue;
eventQueue.QueueEvent(...);

And we're good to go queue...

No comments:

Post a Comment