Skip to content

Commit

Permalink
Improve callMethod logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ravinperera00 committed Nov 22, 2024
1 parent 4d56eac commit e3f6b20
Showing 1 changed file with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Map.Entry<String, BObject>> services = registeredServices.entrySet();
for (Map.Entry<String, BObject> service : services) {
runtime.callMethod(service.getValue(), ON_MESSAGE, null, email);
Thread.startVirtualThread(() -> {
Object email = emailEvent.getEmailObject();
if (runtime != null) {
Set<Map.Entry<String, BObject>> services = registeredServices.entrySet();
for (Map.Entry<String, BObject> 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;
}

Expand All @@ -78,33 +81,37 @@ 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<Map.Entry<String, BObject>> services = registeredServices.entrySet();
for (Map.Entry<String, BObject> service : services) {
runtime.callMethod(service.getValue(), ON_ERROR, null, error);
Thread.startVirtualThread(() -> {
log.error(((BError) error).getMessage());
if (runtime != null) {
Set<Map.Entry<String, BObject>> services = registeredServices.entrySet();
for (Map.Entry<String, BObject> 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.");
}
});
}

/**
* Place an error in Ballerina if error has occurred while closing.
* @param error Email object to be received
*/
public void onClose(Object error) {
if (error != null) {
log.error(((BError) error).getMessage());
}
if (runtime != null) {
Set<Map.Entry<String, BObject>> services = registeredServices.entrySet();
for (Map.Entry<String, BObject> 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<Map.Entry<String, BObject>> services = registeredServices.entrySet();
for (Map.Entry<String, BObject> 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) {
Expand Down

0 comments on commit e3f6b20

Please sign in to comment.