Skip to content

Commit

Permalink
Merge pull request #96 from ayeshLK/local_development
Browse files Browse the repository at this point in the history
Add functionality to set replyTo and correlationId to the message
  • Loading branch information
ayeshLK authored Aug 10, 2023
2 parents de8e522 + 9d9e6ec commit a8c4260
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
14 changes: 12 additions & 2 deletions ballerina/message.bal
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ isolated function externWriteText(handle message, handle value) returns error? =
'class: "javax.jms.TextMessage"
} external;

isolated function externWriteBytes(handle message, byte[] value) returns error? = @java:Method {
isolated function externWriteBytes(handle message, byte[] value) returns Error? = @java:Method {
name: "writeBytes",
'class: "io.ballerina.stdlib.java.jms.JmsMessageUtils"
} external;
Expand All @@ -99,7 +99,17 @@ isolated function externSetString(handle message, handle name, handle value) ret
'class: "javax.jms.MapMessage"
} external;

isolated function externSetBytes(handle message, handle name, byte[] value) returns error? = @java:Method {
isolated function externSetBytes(handle message, handle name, byte[] value) returns Error? = @java:Method {
name: "writeBytesField",
'class: "io.ballerina.stdlib.java.jms.JmsMessageUtils"
} external;

isolated function externSetReplyTo(Session session, handle message, Destination replyTo) returns Error? = @java:Method {
name: "setReplyTo",
'class: "io.ballerina.stdlib.java.jms.JmsMessageUtils"
} external;

isolated function externSetCorrelationId(handle message, string correlationId) returns Error? = @java:Method {
name: "setCorrelationId",
'class: "io.ballerina.stdlib.java.jms.JmsMessageUtils"
} external;
21 changes: 21 additions & 0 deletions ballerina/message_producer.bal
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public isolated client class MessageProducer {
};

isolated function getJmsMessage(Session session, Message message) returns handle|Error {
handle jmsMessage = check constructJmsMessage(session, message);
check updateReplyToMessageField(session, jmsMessage, message.replyTo);
check updateCorrelationIdField(jmsMessage, message.correlationId);
return jmsMessage;
}

isolated function constructJmsMessage(Session session, Message message) returns handle|Error {
if message is TextMessage {
handle jmsMessage = check session.createJmsMessage("TEXT");
error? result = trap externWriteText(jmsMessage, java:fromString(message.content));
Expand All @@ -94,6 +101,20 @@ isolated function getJmsMessage(Session session, Message message) returns handle
return error Error("Unidentified message type");
}

isolated function updateReplyToMessageField(Session session, handle jmsMessage, Destination? replyTo = ()) returns Error? {
if replyTo is () {
return;
}
check externSetReplyTo(session, jmsMessage, replyTo);
}

isolated function updateCorrelationIdField(handle jmsMessage, string? correlationId = ()) returns Error? {
if correlationId is () {
return;
}
check externSetCorrelationId(jmsMessage, correlationId);
}

isolated function populateMapMessage(handle mapMessage, map<anydata> keyValues) returns error? {
foreach string 'key in keyValues.keys() {
var value = keyValues.get('key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@
package io.ballerina.stdlib.java.jms;

import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BString;

import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import static io.ballerina.stdlib.java.jms.CommonUtils.createError;
import static io.ballerina.stdlib.java.jms.CommonUtils.getDestination;
import static io.ballerina.stdlib.java.jms.Constants.JMS_ERROR;
import static io.ballerina.stdlib.java.jms.Constants.NATIVE_SESSION;

/**
* Representation of {@link javax.jms.BytesMessage} with utility methods to invoke as inter-op functions.
Expand Down Expand Up @@ -67,4 +75,45 @@ public static Object writeBytesField(MapMessage message, String fieldName, BArra
}
return null;
}

/**
* Updates `replyTo` field in the JMS message.
*
* @param session Ballerina JMS session object
* @param message JMS message
* @param replyTo Relevant JMS destination
* @return A Ballerina `jms:Error` if there is an error while setting replyTo field to the JMS message
*/
public static Object setReplyTo(BObject session, Message message, BMap<BString, Object> replyTo) {
Session nativeSession = (Session) session.getNativeData(NATIVE_SESSION);
try {
Destination replyToDestination = getDestination(nativeSession, replyTo);
message.setJMSReplyTo(replyToDestination);
} catch (BallerinaJmsException exception) {
return createError(JMS_ERROR, exception.getMessage(), exception);
} catch (JMSException exception) {
return createError(JMS_ERROR,
String.format("Error occurred while setting a reply-to field in the JMS message: %s",
exception.getMessage()), exception);
}
return null;
}

/**
* Updates `correlationId` field in the JMS message.
*
* @param message JMS message
* @param correlationId Message correlationId
* @return A Ballerina `jms:Error` if there is an error while setting correlationId field to the JMS message
*/
public static Object setCorrelationId(Message message, BString correlationId) {
try {
message.setJMSCorrelationID(correlationId.getValue());
} catch (JMSException exception) {
return createError(JMS_ERROR,
String.format("Error occurred while setting correlation-id field to the JMS message: %s",
exception.getMessage()), exception);
}
return null;
}
}

0 comments on commit a8c4260

Please sign in to comment.