Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve callMethod logic #855

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
callMethod(service.getValue(), ON_MESSAGE, 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) {
callMethod(service.getValue(), ON_ERROR, 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) {
callMethod(service.getValue(), ON_CLOSE, error);
}
} else {
log.error("Runtime should not be null.");
}
});
}

protected void addService(BObject service) {
Expand All @@ -116,4 +123,10 @@ protected void addService(BObject service) {
}
}

private void callMethod(BObject service, String methodName, Object args) {
ObjectType serviceType = (ObjectType) TypeUtils.getReferredType(TypeUtils.getType(service));
boolean isConcurrentSafe = serviceType.isIsolated() && serviceType.isIsolated(methodName);
runtime.callMethod(service, methodName, new StrandMetadata(isConcurrentSafe, null), args);
}

}
Loading