Publish Events
Using the Business Event Support extension, additional code for easily publishing events will be generated.
For creating the event and setting the payload the event builder can be used. The event producer service and the built-in event publishing offers capabilities to publish the event to the Kafka topic which was specified at the event while modelling.
Event builder
The generated SDK provides Builders for Events which can be easily used to construct events. It allows to set the payload of the event using the setPayload
method. Depending on what has been defined as payload, it allows to use a schema from the schema registry for defining a payload or another entity (deprecated, see Events 1.0).
// Importing overall Event Builder import de.knowis.cards.sdk.domain.facade.DomainEventBuilder; // Declare and Inject Domain Event Builder @Autowired private DomainEventBuilder eventBuilder; // Create a new instance of the event that should be published CardCreatedEvent event = this.eventBuilder.getCc().getCardCreatedEvent().build(); // Create the payload for the event based on a schema and set it SuccessSchema payloadSchema = new SuccessSchema(); payloadSchema.successful(true); event.setPayload(payloadSchema); /** * Event 1.0 with entity payload * @deprecated use schemas from the schema registry instead */ SuccessEventPayload payloadEntity = entityBuilder.getCc().successEventPayload().build(); event.setPayload(payloadEntity);
DomainEventBuilder is also accessible in project generated implementation files (services, commands, agents) derived from their base classes.
Event producer service
The autogenerated EventProducerService
allows to easily publish business events to Kafka topics. The topic which is used is the one, that has been defined for the event during modelling. When using it, the connection to the Kafka will be automatically set up and does not require further code.
When publishing the event, it is possible to set custom headers and a custom message key (optional).
// Declare Event Producer Service @Autowired private EventProducerService eventProdcuer; // Create business event SuccessEvent event = eventBuilder.getCc().successEvent().build(); // set event payload // ... // Publish business event eventProducer.publish(event); // Alternative 1: publish the event with custom messageKey eventProducer.publish(event, "customMessageKey"); // Alternative 2: publish the event with messageHeaders HashMap<String, Object> map = new HashMap(); map.put("headerKey", "headerValue"); MessageHeaders headers = new MessageHeaders(map); eventProducer.publish(event, headers); // Alternative 3: publish the event with messageKey and messageHeaders eventProducer.publish(event, "customMessageKey", headers);
Custom message keys will be ignored in case of publishing event 1.0 (Entity payload).
Built-in event publishing
If the event is assigned to a command, service or agent, while modelling the generated base class of those will additionally provide a method to publish the event easily. Also here it is possible to customize the publishing by setting a custom message key and custom headers
// Create business event SuccessEvent event = eventBuilder.getCc().successEvent().build(); // set event payload // ... // Publish business event this.publishSuccessEvent(event); // Alternative 1: publish the event with custom messageKey this.publishSuccessEvent(event, "customMessageKey"); // Alternative 2: publish the event with messageHeaders HashMap<String, Object> map = new HashMap(); map.put("headerKey", "headerValue"); MessageHeaders headers = new MessageHeaders(map); this.publishSuccessEvent(event, headers); // Alternative 3: publish the event with messageKey and messageHeaders this.publishSuccessEvent(event, "customMessageKey", headers);
Full implementation example
Implementation example for publishing events assigned to command, service or agent:
//... imports import myproj.sdk.domain.schemas.SchemaGroup.BalanceCheckedSchema; @Service public class CardCommand extends CardCommandBase { private static Logger log = LoggerFactory.getLogger(CardCommand.class); @Override public Card createCreditCard(CreditCard creditCard) throws CreditCardCreationError { log.info("CardCommands.createCreditCard()"); // ... some command implementation // Publish an event, using a schema from the schema registry as payload BalanceCheckedEvent schemaEvent = this.eventBuilder.getCc().getBalanceCheckedEvent().build(); // Create the payload for the event BalanceCheckedSchema schema = new BalanceCheckedSchema(); schema.setProperty1('value'); schemaEvent.setPayload(schema); // create custom headers HashMap<String, Object> map = new HashMap(); map.put("headerKey", "headerValue"); MessageHeaders headers = new MessageHeaders(map); // publish the event with messageKey and messageHeaders this.publishBalanceCheckedEvent(schemaEvent, "customMessageKey", headers); // ... further command implementation } }