Back to EIP

PatternAggregator patiently collects a sequence of messages and combines them once all have been received.
How Ballerina helps

Ballerina provides convenient map and table data structures for temporary message storage. Ballerina provides robust support for distributed storage with the Redis package. Additionally, it offers seamless integration with various SQL and NoSQL databases for persistent data storage. Ballerina's cache package is an efficient in-memory cache solution that includes automatic cleanup mechanisms.

Aggregator
Copy
import ballerina/http;

final map<json[]> partialSurveys = {};

final http:Client formSubmitClient = check new ("http://api.surveyme.com.balmock.io");

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

    resource function post survey/[string id](@http:Header string userId, @http:Payload json formData) returns error? {
        json[]? surveyData = partialSurveys[userId];
        if surveyData == () {
            json[] newSurvey = [formData];
            partialSurveys[userId] = newSurvey;
        } else {
            surveyData.push(formData);
            if surveyData.length() == 3 {
                _ = check formSubmitClient->/survey/[id]/submit.post({userId: surveyData}, targetType = http:Response);
                _ = partialSurveys.remove(userId);
            }
        }
    }
}