diff --git a/ballerina/session.bal b/ballerina/session.bal index e6d65558..d63de98c 100644 --- a/ballerina/session.bal +++ b/ballerina/session.bal @@ -69,6 +69,13 @@ public isolated client class Session { 'class: "io.ballerina.stdlib.java.jms.JmsSession" } external; + # Closes the session. + # + # + return - `jms:Error` if there is an error or else nil + isolated remote function close() returns Error? = @java:Method { + 'class: "io.ballerina.stdlib.java.jms.JmsSession" + } external; + isolated function createJmsMessage(string messageType) returns handle|Error = @java:Method { 'class: "io.ballerina.stdlib.java.jms.JmsSession" } external; diff --git a/native/src/main/java/io/ballerina/stdlib/java.jms/JmsSession.java b/native/src/main/java/io/ballerina/stdlib/java.jms/JmsSession.java index 8d2a4acb..a246f23f 100644 --- a/native/src/main/java/io/ballerina/stdlib/java.jms/JmsSession.java +++ b/native/src/main/java/io/ballerina/stdlib/java.jms/JmsSession.java @@ -185,4 +185,28 @@ public static Object rollback(BObject session) { } return null; } + + /** + * Closes the session. + * + * @param session Ballerina session object + * @return {@link javax.jms.Message} or Ballerina `jms:Error` iif the JMS provider fails to close the session due + * to some internal error. + */ + public static Object close(BObject session) { + Object nativeSession = session.getNativeData(NATIVE_SESSION); + if (Objects.isNull(nativeSession)) { + return ErrorCreator.createError(ModuleUtils.getModule(), JMS_ERROR, + StringUtils.fromString("Could not find the native JMS session"), null, null); + } + try { + ((Session) nativeSession).close(); + } catch (JMSException exception) { + BError cause = ErrorCreator.createError(exception); + return ErrorCreator.createError(ModuleUtils.getModule(), JMS_ERROR, + StringUtils.fromString(String.format("Error while closing the JMS session: %s", + exception.getMessage())), cause, null); + } + return null; + } }