From 932e4440205a7616e97b945957dd563ee706a869 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Mon, 7 Aug 2023 15:14:46 +0530 Subject: [PATCH] Add close API to JMS session --- ballerina/session.bal | 7 ++++++ .../ballerina/stdlib/java.jms/JmsSession.java | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+) 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; + } }