Implement Domain Agent
Agent Base
For each agent there will be an abstract class Agent Base generated in the SDK.
The Agent Base provide access to the repository, entity builder, event builder and event producer.
The Agent Base contains one abstract method named onMessage.
The onMessage method needs to be implemented in the generated implementaion file for the agent.
The Agent implementation file onMessage is automatically triggered when the kafka event that the agent is modelled aganist is recieved.
Payload Entity
Agent onMessage method will recieve Payload Entity as first paramter (if event that the agent recieves is modelled with a payload).
Agent onMessage method will also recieve MessageHeaders as a paramter which is a key-value map that contains kafka message headers.
Events
If agent is modelled with business events, for each event there will be an inherited method in the agent base class that takes the event as an input and publishes it.
It is also possible to publish events directly using EventProducerService.
Implementation Example
Example of Balance service implemenation file.
package de.knowis.cards.operations.domain.cc.agent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.messaging.MessageHeaders;
import de.knowis.cards.operations.sdk.domain.cc.agent.CreditCardCreatedAgentBase;
import de.knowis.cards.operations.domain.facade.DomainEntityBuilder;
import de.knowis.cards.operations.domain.facade.DomainEventBuilder;
import de.knowis.cards.operations.sdk.domain.facade.Repository;
import de.knowis.cards.operations.sdk.domain.service.EventProducerService;
import de.knowis.cards.operations.sdk.domain.cc.entity.CreditCard;
import de.knowis.cards.operations.sdk.domain.cc.entity.Card;
@Service
public class CreditCardCreatedAgent extends CreditCardCreatedAgentBase {
private static Logger log = LoggerFactory.getLogger(CreditCardCreatedAgent.class);
// Declare Card Command
private final CardCommandBase cardCommand;
// Adjust your generated agent constructor to inject CardCommandBase so it can be used in agent logic
public CreditCardCreatedAgent(DomainEntityBuilder entityBuilder, DomainEventBuilder eventBuilder, EventProducerService eventProducer, Repository repo, CardCommandBase cardCommand) {
super(entityBuilder,eventBuilder, eventProducer,repo);
this.cardCommand = cardCommand;
}
@Override
public void onMessage(CreditCard creditCard, MessageHeaders headers) {
log.info("Agent CreditCardCreatedAgent received event cc:CreditCardCreated");
// Get root entity through repository
Card card = this.repo.getCc().getCards().findById(creditCard.getId());
// Call activateCard instance command to activate Card
// (this will call implemented logic for activate card command)
cardCommand.activateCard(card);
}
}