Class MessageInterceptorAbstract

Allows intercepting messages before their publication.

An interceptor can reject or modify messages. Multiple interceptors can be registered, forming a chain in which each interceptor is called one by one in registration order.

For each message, the platform invokes the intercept method of the first registered interceptor, passing the message and the next handler as arguments. By calling the next handler in the intercept method, message dispatching is continued. If there is no more interceptor in the chain, the message is transported to the receivers, if any. But, if throwing an error in the intercept method, message dispatching is aborted, and the error transported back to the sender.

Registering Interceptors

You register interceptors with the bean manager when the host application starts. Interceptors can be registered only in the host application. They are invoked in registration order.

Beans.register(MessageInterceptor, {useClass: MessageLoggerInterceptor, multi: true});

Filtering Messages for Interception

The platform passes all messages to the interceptors, including platform messages vital for its operation. You can use the TopicMatcher to filter messages, allowing you to test whether a topic matches a pattern. The pattern must be a topic, not a regular expression; thus, it must consist of one or more segments, each separated by a forward slash. The pattern can contain wildcard segments. Wildcard segments start with a colon (:), acting act as a placeholder for any segment value.

class ProductValidatorInterceptor implements MessageInterceptor {

private topicMatcher = new TopicMatcher('product/:id');

public intercept(message: TopicMessage, next: Handler<TopicMessage>): Promise<void> {
// Pass messages sent to other topics.
if (!this.topicMatcher.match(message.topic).matches) {
return next.handle(message);
}

// Validate the payload of the message.
if (isValid(message.body)) {
return next.handle(message);
}

throw Error('Message failed schema validation');
}
}

Hierarchy

  • MessageInterceptor

Implements

Constructors

Methods

Constructors

Methods

  • Intercepts a message before being published to its topic.

    Decide if to continue publishing by passing the message to the next handler, or to reject publishing by throwing an error, or to swallow the message by not calling the next handler at all. If rejecting publishing, the error is transported to the message publisher.

    Important: When passing the message to the next handler, either return its Promise or await it. Otherwise, errors of subsequent interceptors would not be reported to the sender.

    Parameters

    Returns Promise<void>

    Throws

    throw an error to reject publishing; the error is transported to the message publisher.

Generated using TypeDoc