Implement Agents

A published event will automatically trigger the agents, which have the event assigned as trigger event. The payload provided in the event will be available as input in the agent. Besides that, any kind of logic can be implemented in an agent (e. g. calling another service).

Within the agent implementation, the message key, the message headers and the message timestamp are available and can be used. Both custom headers as well as commonly used headers can be accessed here.

  /**
   * Agent execution
   */
  public async execute(): Promise<void> {
    const log = this.util.log;

    // Get the payload from the event that triggered the agent
    const { property1 } =  this.input;

    // log message key
    log.info(`Message key: ${this.messageKey}`);

    // log value of my custom header, wich has a string as value
    const strHeaderValue = this.messageHeaders['customHeader1'].toString();
    log.info(`Custom header "customHeader1" has value: ${strHeaderValue}`);

    // log value of another header, which has Buffer as value
    const bufHeaderValue = this.messageHeaders['customHeader2'].toString();
    log.info(`Custom header "customHeader2" has value: ${bufHeaderValue}`);

    // log value of message timestamp in the format of an ISO String
    const msgTime = new Date(this.messageTimestamp);
    log.info(`Message timestamp: ${msgTime.toISOString()}`)

    // Initialize input for calling another service
    const input = this.factory.entity.order.ServiceIdentifier_Input();

    // Set the value from the event payload as input value
    input.property1 = property1;

    // Call service and pass as input the input entity created above
    await this.services.order.ServiceIdentifier(input);
  }