December 2, 2020
Azure Service Bus – Queues – An introduction
Comments
(0)
December 2, 2020
Azure Service Bus – Queues – An introduction
Newbie 10 posts
Followers: 0 people
(0)

CFNext introduces integration with Microsoft Azure Service Bus, a managed message brokering service that enables decoupling of applications by allowing asynchronous transfer of data and state.

The integration with messaging service falls into the following 2 categories:

  • Queues: provides a buffer in the form of a queue for one to one transfer of messages between a sender and a receiver.
  • Topics: can be used to transfer messages in a publish/subscribe scenario.

In this post, which is the first of a series that discusses Service Bus features in CFNext, we will discuss how to get started with Queues using ColdFusion.

Prerequisites:

  1. Get an Azure account.
  2. Create a Service Bus namespace.
  3. Get the Azure connection string. Instructions on how to get the connection string can be found here.
  4. Install ColdFusion 2021

Following is an simple CFML application example that demonstrates creating a queue, sending a message to the queue and receiving that message. You can try it after substituting the connectionString with the same from your Azure account. You can also create a queue in Azure portal by following the instructions at this article.

proxyCF_SB = cloudService(
	{
		"vendorName" : "AZURE", //other possible value: AWS
		"connectionString" : "Endpoint=sb://?yournamespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=?"
	}, 
	{
		//possible values for Azure services: AZURE_BLOB, SERVICE_BUS 
		//possible values for AWS services: S3, SQS, SNS, DYNAMODB
		"serviceName" : "SERVICE_BUS" 
	})

//create a queue.
queue = proxyCF_SB.createQueue("myFirstQueue3")

//send message to the queue. if you do not specify the messageId attribue, it is Azure assigns the message a unique ID.
sendMsgResponse = proxyCF_SB.sendMessage( queue.getPath(),
					{
						"messageBody" = "message data goes here",
						"messageProperties" = { "timestamp" : "#dateTimeFormat(now())#", "message_category""test" }
					}
				)
//a successful send should return the statusCode "200".
cfdump(var="#sendMsgResponse#", label="response from sending msg to queue: #queue.getPath()#")

//receive the message. reading with PEEKLOCK mode leaves the message in the queue intact.
receiveMessageMetadata = {
		"receiveMode" = "RECEIVEANDDELETE", //possible values: PEEKLOCK, RECEIVEANDDELETE
		"maxMessageCount" = 1
	}
receiveMessageResponse = proxyCF_SB.receiveMessage(queue.getPath(), receiveMessageMetadata)
cfdump(var="#receiveMessageResponse#", label="message from queue: #queue.getPath()#")

//delete the queue
proxyCF_SB.deleteQueue(queue.getPath())

You can also send a batch of messages in one go by using the sendMessageBatch method, that accepts an array of message structs

batchSize = 2
for(n=1; n LTE batchSize; n++){
	msgStructTemplate =  {
		"messageBody" = "msg sno.#n# of #batchSize#",
		"messageProperties" = {
					 "timestamp" : "#dateTimeFormat(now())#", "message category" : "test" 
				   }
	}
	msgBatchArray[n] = msgStructTemplate
}
proxyCF_SB.sendMessageBatch(queue.getPath(), msgBatchArray)


We’ll be discussing the other methods in subsequent posts on this forum. I’ll be following up this post with another one on how to work with topic with CFNext.

 
0 Comments
Add Comment