Skip to content

Commit

Permalink
Update value type for application properties in asb:Message
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeshLK committed Mar 16, 2023
1 parent 68110ba commit 096da17
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion asb-ballerina/message.bal
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ public type Message record {|
@display {label: "Application Properties"}
public type ApplicationProperties record {|
@display {label: "Properties"}
map<any> properties?;
map<byte[]> properties?;
|};
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@
import com.azure.core.util.IterableStream;
import com.azure.messaging.servicebus.ServiceBusClientBuilder;
import com.azure.messaging.servicebus.ServiceBusException;
import com.azure.messaging.servicebus.ServiceBusMessage;
import com.azure.messaging.servicebus.ServiceBusReceivedMessage;
import com.azure.messaging.servicebus.ServiceBusReceiverClient;
import com.azure.messaging.servicebus.ServiceBusClientBuilder.ServiceBusReceiverClientBuilder;
import com.azure.messaging.servicebus.models.DeadLetterOptions;
import com.azure.messaging.servicebus.models.ServiceBusReceiveMode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.ballerina.runtime.api.PredefinedTypes;
import io.ballerina.runtime.api.creators.TypeCreator;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.types.ArrayType;
import io.ballerina.runtime.api.types.MapType;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BMap;
Expand Down Expand Up @@ -58,6 +65,7 @@
*/
public class MessageReceiver {
private static final Logger log = Logger.getLogger(MessageReceiver.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private ServiceBusReceiverClient receiver;

/**
Expand Down Expand Up @@ -256,16 +264,29 @@ private BMap<BString, Object> getReceivedMessage(BObject endpointClient, Service
map.put("deadLetterReason", StringUtils.fromString(receivedMessage.getDeadLetterReason()));
map.put("deadLetterSource", StringUtils.fromString(receivedMessage.getDeadLetterSource()));
map.put("state", StringUtils.fromString(receivedMessage.getState().toString()));
BMap<BString, Object> applicationProperties = ValueCreator.createRecordValue(ModuleUtils.getModule(),
ASBConstants.APPLICATION_PROPERTY_TYPE);
Object appProperties = ASBUtils.toBMap(receivedMessage.getApplicationProperties());
map.put("applicationProperties", ValueCreator.createRecordValue(applicationProperties, appProperties));
map.put("applicationProperties", getApplicationProperties(receivedMessage));
BMap<BString, Object> createRecordValue = ValueCreator.createRecordValue(ModuleUtils.getModule(),
ASBConstants.MESSAGE_RECORD, map);
endpointClient.addNativeData(receivedMessage.getLockToken(), receivedMessage);
return createRecordValue;
}

private static BMap<BString, Object> getApplicationProperties(ServiceBusReceivedMessage message)
throws JsonProcessingException {
BMap<BString, Object> applicationPropertiesRecord = ValueCreator.createRecordValue(ModuleUtils.getModule(),
ASBConstants.APPLICATION_PROPERTY_TYPE);
ArrayType byteArrayType = TypeCreator.createArrayType(PredefinedTypes.TYPE_BYTE);
MapType mapType = TypeCreator.createMapType(byteArrayType);
BMap<BString, Object> applicationProperties = ValueCreator.createMapValue(mapType);
for (Map.Entry<String, Object> property: message.getApplicationProperties().entrySet()) {
String propertyKey = property.getKey();
byte[] propertyValue = OBJECT_MAPPER.writeValueAsBytes(property.getValue());
applicationProperties.put(
StringUtils.fromString(propertyKey), ValueCreator.createArrayValue(propertyValue));
}
return ValueCreator.createRecordValue(applicationPropertiesRecord, applicationProperties);
}

/**
* Receive Batch of Messages with configurable parameters as Map when Receiver
* Connection is given as a parameter,
Expand Down

0 comments on commit 096da17

Please sign in to comment.