From e3f6b202dbee7cdc56fe82274fc184549f9e5f16 Mon Sep 17 00:00:00 2001 From: ravinperera00 Date: Fri, 22 Nov 2024 14:48:23 +0530 Subject: [PATCH] Improve callMethod logic --- .../stdlib/email/server/EmailListener.java | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/email/server/EmailListener.java b/native/src/main/java/io/ballerina/stdlib/email/server/EmailListener.java index ff9f4a26..5e5dc878 100644 --- a/native/src/main/java/io/ballerina/stdlib/email/server/EmailListener.java +++ b/native/src/main/java/io/ballerina/stdlib/email/server/EmailListener.java @@ -19,6 +19,7 @@ package io.ballerina.stdlib.email.server; import io.ballerina.runtime.api.Runtime; +import io.ballerina.runtime.api.concurrent.StrandMetadata; import io.ballerina.runtime.api.types.ObjectType; import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BError; @@ -61,15 +62,17 @@ public EmailListener(Runtime runtime) { * @return If successful return true */ public boolean onMessage(EmailEvent emailEvent) { - Object email = emailEvent.getEmailObject(); - if (runtime != null) { - Set> services = registeredServices.entrySet(); - for (Map.Entry service : services) { - runtime.callMethod(service.getValue(), ON_MESSAGE, null, email); + Thread.startVirtualThread(() -> { + Object email = emailEvent.getEmailObject(); + if (runtime != null) { + Set> services = registeredServices.entrySet(); + for (Map.Entry service : services) { + runtime.callMethod(service.getValue(), ON_MESSAGE, new StrandMetadata(true, null), email); + } + } else { + log.error("Runtime should not be null."); } - } else { - log.error("Runtime should not be null."); - } + }); return true; } @@ -78,15 +81,17 @@ public boolean onMessage(EmailEvent emailEvent) { * @param error Email object to be received */ public void onError(Object error) { - log.error(((BError) error).getMessage()); - if (runtime != null) { - Set> services = registeredServices.entrySet(); - for (Map.Entry service : services) { - runtime.callMethod(service.getValue(), ON_ERROR, null, error); + Thread.startVirtualThread(() -> { + log.error(((BError) error).getMessage()); + if (runtime != null) { + Set> services = registeredServices.entrySet(); + for (Map.Entry service : services) { + runtime.callMethod(service.getValue(), ON_ERROR, new StrandMetadata(true, null), error); + } + } else { + log.error("Runtime should not be null."); } - } else { - log.error("Runtime should not be null."); - } + }); } /** @@ -94,17 +99,19 @@ public void onError(Object error) { * @param error Email object to be received */ public void onClose(Object error) { - if (error != null) { - log.error(((BError) error).getMessage()); - } - if (runtime != null) { - Set> services = registeredServices.entrySet(); - for (Map.Entry service : services) { - runtime.callMethod(service.getValue(), ON_CLOSE, null, error); + Thread.startVirtualThread(() -> { + if (error != null) { + log.error(((BError) error).getMessage()); } - } else { - log.error("Runtime should not be null."); - } + if (runtime != null) { + Set> services = registeredServices.entrySet(); + for (Map.Entry service : services) { + runtime.callMethod(service.getValue(), ON_CLOSE, new StrandMetadata(true, null), error); + } + } else { + log.error("Runtime should not be null."); + } + }); } protected void addService(BObject service) {