December 8, 2020
Azure Service Bus – Topics
Comments
(0)
December 8, 2020
Azure Service Bus – Topics
Newbie 10 posts
Followers: 0 people
(0)

This is the second of a series of posts, on Azure Service Bus integration with ColdFusion. In this post we will talk about how to use Topics in CF2021.

Topics are used for one to many transfer of messages in a publish/subscribe scenario. You may refer this document for more on the subject.

The following example demonstrates how to create a topic, publish messages to the created topic and subscribe to it. For the prerequisites to this exercise please refer my previous blog post on Service Bus.

proxyCF_SB = cloudService(
	{
		"vendorName" : "AZURE""connectionString" : "Endpoint=sb://<your namespace>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<your acess key>"
	}, 
	{
		"serviceName" : "SERVICE_BUS" 
	})
	
  //create a topic with default attributes. you may optionally pass a second argument in the form of a struct with the desired attributes
 topic = proxyCF_SB.createTopic("test-asb-topic")
 subscription = proxyCF_SB.subscribe(topic.getPath(), {
						      "subscriptionName" = "test-subscription"
						    })

registerMessageHandler(subscription)
message = {
		"messageBody" = "message data goes here",
		"messageProperties" = {"group" = "red", "priority":1 , "severity":2}
	}
	
//should return status code 200 is the message is sent successfully.
sendMessageResponse = topic.sendMessage(message)
cfdump(var="#sendMessageResponse#", label="msg publishing status: #topic.getPath()#")
	
//wait for the message to be output, when the message handler is invoked.
sleep(4000)

//unsubscribe and purge the test topic. returns status code 200 if the operation is successful.
responseUnsubscribe = proxyCF_SB.unsubscribe(topic.getPath(), subscription.getSubscriptionName())
responseDelTopic = proxyCF_SB.deleteTopic(topic.getPath())

void function registerMessageHandler(required any subscription) {
	
	onSuccess = function(message, subscription) {
		writeOutput("received message with ID: #message.messageId#; message Body: #message.messageBody.valueData#; subscription: #subscription.getSubscriptionName()#' for topic: '#subscription.getTopicName()#")
		subscription.completeMessage(
			{"lockToken" = message.lockToken}
		)
	}
	onError = function() {
		writeOutput("error receiving message with subscription '#subscription.getSubscriptionName()#'")
	}
	msgHandlerMethods = {
		"onSuccess" = onSuccess,
		"onError" = onError
	}
	registerationResponse = subscription.registerMessageHandler(msgHandlerMethods)
}

The “cloudService” method, in the example above, returns an object that exposes most of the methods for interacting with Azure Service Bus. We will discuss the other methods exposed by this proxy in another post, but you can sneak a peak by simply dumping the returned object in your test code.

The “createTopic” method can also accept a second argument in the form of a struct that specifies the attributes for the topic. Here’s an example:

topicAttributes = {
	"autoDeleteOnIdle" = "5M", //m millis, M min, d days, h hours, s secs, n nanos
	"requiresDuplicateDetection" = "true",
	"maxSizeInMB" = 1024,
	"entityStatus" = "Active", //values: Active, Disabled, ReceiveDisabled, SendDisabled. Gets the status of the entity. When an entity is disabled, that entity cannot send or receive messages.
	"userMetadata" = "topic description", // Custom metdata that user can associate with the description. Max length 1024 chars
	"enableBatchedOperations" = "true",
	"enablePartitioning" = "false"
}

You may read more on the possible topic attributes here and here.

I’ll follow with more on some other topic and queue features in CF2021.

0 Comments
Add Comment