From f282d75d7a6feceaf6598b2dff46c5c5589c0c1b Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Mon, 7 Aug 2023 12:29:01 +0530 Subject: [PATCH] Add commit and rollback functionality to JMS session --- ballerina/session.bal | 14 ++++++ .../ballerina/stdlib/java.jms/JmsSession.java | 48 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/ballerina/session.bal b/ballerina/session.bal index de38ab84..0b63cc46 100644 --- a/ballerina/session.bal +++ b/ballerina/session.bal @@ -55,6 +55,20 @@ public isolated client class Session { return new MessageConsumer(self, consumerOptions); } + # Commits all messages done in this transaction and releases any locks currently held. + # + # + return - `jms:Error` if there is an error or else nil + isolated remote function 'commit() returns Error? = @java:Method { + 'class: "io.ballerina.stdlib.java.jms.JmsSession" + } external; + + # Rolls back any messages done in this transaction and releases any locks currently held. + # + # + return - `jms:Error` if there is an error or else nil + isolated remote function 'rollback() 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 549ec791..8d2a4acb 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 @@ -137,4 +137,52 @@ public static Object createJmsMessage(BObject session, BString messageType) { exception.getMessage())), cause, null); } } + + /** + * Commits all messages done in this transaction and releases any locks currently held. + * + * @param session Ballerina session object + * @return {@link javax.jms.Message} or Ballerina `jms:Error` if the session is not using a local transaction or + * else the JMS provider fails to commit the transaction due to some internal error. + */ + public static Object commit(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).commit(); + } catch (JMSException exception) { + BError cause = ErrorCreator.createError(exception); + return ErrorCreator.createError(ModuleUtils.getModule(), JMS_ERROR, + StringUtils.fromString(String.format("Error while committing the JMS transaction: %s", + exception.getMessage())), cause, null); + } + return null; + } + + /** + * Rolls back any messages done in this transaction and releases any locks currently held. + * + * @param session Ballerina session object + * @return {@link javax.jms.Message} or Ballerina `jms:Error` if the session is not using a local transaction or + * else the JMS provider fails to roll back the transaction due to some internal error. + */ + public static Object rollback(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).rollback(); + } catch (JMSException exception) { + BError cause = ErrorCreator.createError(exception); + return ErrorCreator.createError(ModuleUtils.getModule(), JMS_ERROR, + StringUtils.fromString(String.format("Error while rolling back the JMS transaction: %s", + exception.getMessage())), cause, null); + } + return null; + } }