Back to EIP

PatternThe recipient list pattern inspects an incoming message, determines the list of desired recipients, and forwards the message to all channels associated with the recipients in the list.
How Ballerina helps

Ballerina can extract information from messages and execute logic based on that. Iteration constructs such as foreach and while and query expressions can be used to execute a given logic for each recipient. Ballerina supports sending multiple messages in a single incoming message.

Recipient ListContent Based RouterMessage Router
Copy
import ballerina/http;

type Recipient readonly & record {|
    string recpientId;
    string catergory;
    string contact;
    "EMAIL"|"SMS"|"NOTIFICATION" subscription;
|};

type Message readonly & record {|
    string subject?;
    string body;
    string signature?;
|};

final map<Recipient[]> recipientList = {};

final http:Client emailManagerClient = check new ("http://api.email.manager.com.balmock.io");
final http:Client smsManagerClient = check new ("http://api.sms.manager.com.balmock.io");
final http:Client notificationManagerClient = check new ("http://api.notification.manager.com.balmock.io");

service /api/v1 on new http:Listener(8080) {

    resource function post quotes/[string catergory](Message message) returns error? {
        foreach var {contact, subscription} in recipientList.get(catergory) {
            match subscription {
                "EMAIL" => {
                    _ = check emailManagerClient->/send/[contact].post(message, targetType = http:Response);
                }
                "SMS" => {
                    _ = check smsManagerClient->/send/[contact].post(message, targetType = http:Response);
                }
                "NOTIFICATION" => {
                    _ = check notificationManagerClient->/send/[contact].post(message, targetType = http:Response);
                }
            }
        }
    }
}