This package contains sObjects and related Apex logic in order to receive JSON payloads.
This package contains the KafkaMessage__c sObject and related Apex logic in
order to receive JSON payloads representing changes from the Kafka CDC pipeline.
A trigger on the KafkaMessage__c sObject will enqueue asynchronous processing
requests through the asynchronous processing framework that is part of the
crm-platform-base package.
The framework depends on two custom metadata objects in order to dynamically instruct the application how to handle the message payload.
Binding between the asynchronous processing request (AsyncRequest__c) type
created by this package and the KafkaMessageAsyncJob class in order to
instruct the asynchronous processing framework to call the
KafkaMessageAsyncJob class in order to handle requests originating from this
package.
Binding between the KafkaMessage__c.Topic__c field and an Apex handler class
for a given Topic in order to instruct the application on how to handle a
message payload related to a specific Kafka topic.
- An external application inserts a record or batch or records into the
KafkaMessage__csObject - A trigger on the
KafkaMessage__cobject insert one record into theAsyncRequest__cobject for each batch of up to 200KafkaMessage__crecords created in a single transaction, representing a request for asynchronous processing of the new messages. - When the asynchronous processing framework processes the request, the custom
metadata binding
AsyncRequestHandlerBinding__mdtinstructs the application to handle the request using theKafkaMessageAsyncJobApex class. - If noAsyncRequestHandlerBinding__mdtrecord is found corresponding to the "Kafka Message"AsyncRequestType__cvalue, theAsyncRequest__crecord is updated with an error. - The
KafkaMessageAsyncJobqueries for the relevantKafkaMessage__crecords by the Ids stored in the async processing request and queries theKafkaMessageHandlerBinding__mdtcustom metadata object for registered bindings betweenKafkaMessage__c.Topic__cvalues and corresponding Apex classes to handle payloads corresponding toTopic__cvalues. - If noKafkaMessageHandlerBinding__mdtrecord is found corresponding to theTopic__cvalue, the relevantKafkaMessage__crecord is updated with an error. The message can then be retried after the error has been addressed. i. IfKafkaMessageHandlerBinding__mdt.SandboxOverrideTopic__cexists, it is its value which will correspond withKafkaMessage__c.Topic__cin scratch orgs and sandboxes.KafkaMessageHandlerBinding__mdt.Topic__cwill in this case remain unused. - The Apex class registered by the
KafkaMessageHandlerBinding__mdtbinding executes the business logic corresponding to theTopic__cvalue. If an exception occurs, the relevantKafkaMessage__crecord is updated with an error. The message can then be retried after the error has been addressed.
To process incoming kafka messages in a synchronous context the following pattern should be followed:
- Definition of a separate platform event with the exact data model as i.e. defined here.
- Create a trigger and separate trigger handler to process the incoming events.
- The processing itself should be implemented using the
IKafkaMessageConsumerinterface such that error handling can be performed easily storing failed events asKafkaMessage__crecords. An example of this can be viewed here where doEventTransform performs the transformation from the custom event to theKafkaMessage__cmodel and the failed events are stored asKafkaMessage__crecords in an error status.
KodeverkCalloutService contains methods for fetching codes from Felles
kodeverk for ad hoc use or for storing them as Common_Code__c records. For
details, see kodeverk.