Azure

Pushing Messages To Azure ServiceBus Queue From Azure Pipeline

On this article, we’re going to person powershell script to push payloads to azure service bus queue which is a wonderful manner if you wish to course of some massive present knowledge and doesn’t need to overload the appliance immediately. So this mechanism offers a managed method to carry out such job with assist of azure service bus queue.

Because the identify suggests azure service bus queue is a queue. Our purposes can ship messages to this queue and in addition can hear from this queue one after the other. Companies offers a easy method to decouple elements utterly and effectively. Service bus shops message durably

In Companies bus context there are two customers

  1. Producers
  2. Customers

Producers push messages to queue with out the expectation of any response.

Customers learn messages from queue and haven’t any obligation for producers on how you can course of the message

Subsequently producers and customers doesn’t must ship or obtain messages from queue on the identical time respectively and each can course of message at their very own tempo

There are two methods customers can obtain messages from service bus,

  1. Obtain and Delete
  2. Peak and Lock

Now let’s get began with powershell script to push messages to service bus queue.

[CmdletBinding()]
param(
	[Parameter (Mandatory = $false)]
	[string]
	$ResourceGroupName = "",
	[Parameter (Mandatory = $false)]
	[string]
	$NamespaceName = "",
	[Parameter(Mandatory = $false)]
	[string]
	$PolicyName = 'RootManageSharedAccessKey',
	[Parameter(Mandatory = $true)]
	[string]
	$ServiceBusName,
	[Parameter(Mandatory = $true)]
	[string]
	$$Namespace,
	[Parameter(Mandatory = $true)]
	[string]
	$lat,
	[Parameter(Mandatory = $true)]
	[string]
	$lengthy,
	[Parameter(Mandatory = $false)]
	[string]
	$userId,
)

operate Ship-AzServiceBusMessage
{
	$continuationToken = $null

	$module = Get-InstalledModule -Identify Az.ServiceBus
	
	if($module -ne $null)
	{
		write-host "module avaiable"
	}
	else
	{
		write-host "Az.ServiceBus module not avaiable obtain in progress"
		Set up-Module -Identify Az.ServiceBus -AllowClobber -force
	}
				
	$primaryKey = (Get-AzServiceBusKey -ResourceGroupName $ResourceGroupName -Namespace $namespacename -Identify $PolicyName).Primarykey
	
	$message = [pscustomobject] @{ "Physique" = "{`"userId`":`"$userId`",`"lat`":$lat, `"lengthy`": $lengthy}"; }
	write-host "josn knowledge $message"
	
	$physique = $message.Physique
	$message.psobject.properties.Take away("Physique")
	
	#arrange the url parameters for the Invoke-WebRequest
	$uri = "https://$Namespace.servicebus.home windows.web/"+$ServiceBusName+"/messages"
	
	$origin = [DateTime]"1/1/1970 00:00"
	$Expiry = (Get-Date).AddMinutes(1)
	
	#compute the token expiration time.
	$diff = New-TimeSpan -Begin $origin -Finish $Expiry
	$tokenExpirationTime = [Convert]::ToInt32($diff.TotalSeconds)
	
	Add-Kind -AssemblyName System.Net
	
	#create the string that will probably be hashed
	$stringToSign = [Web.HttpUtility]::UrlEncode($Namespace) + "`n" + $tokenExpirationTime
	
	#new-up the HMACSHA256 class
	$hmacsha = New-Object -TypeName System.Safety.Cryptography.HMACSHA256
	$hmacsha.Key = [Text.Encoding]::UTF8.GetBytes($primaryKey)
	
	#hash is computed with the HMACSHA256 class occasion. The hash is transformed to a base 64 string
	$hash = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign))
	$signature = [Convert]::ToBase64String($hash)
	
	#creating token right here
	$token = [string]::Format([Globalization.CultureInfo]::InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", [Web.HttpUtility]::UrlEncode($Namespace), [web.HttpUtility]::UrlEncode($signature),$tokenExpirationTime, $PolicyName)
	
	write-host "token : $token"
	
	$headers = @{"Authorization" = "$token"; "Content material-Kind" = "software/atom+xl;sort=entry;charset-utf-g"}
	$headers.Add("BrokerProperties", $(ConvertTo-Json -InputObject $Message -Compress))
	
	#Invoke-WebRequest name.
	#This invokes the resurce api to push payload to service bus queue
	Invoke-WebRequest -Uri $uri -Headers $headers -Technique Submit -Physique $physique > $null
}

Ship-AzServiceBusMessage

And utilizing this we are able to push our messages to service bus queues.

Tags
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close