Skip to content

Commit

Permalink
Register-RabbitMqEvent fix and minor Features (#20) and !deploy
Browse files Browse the repository at this point in the history
* adding Exchange declaration in connect-rmqchannel
* adding option to allow empty exchange for default one
* reverting default port to 5672 unless Ssl version is set, or port is set to override
* Adding support for Explicit exchange declaration when ExchangeType is specified (optional)
* fixing #19 and adding ActionData feature, and option to name the listener's job via -ListenerJobName
  • Loading branch information
gaelcolas authored and gpduck committed Feb 14, 2017
1 parent b0e8ea8 commit 44d3131
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
15 changes: 14 additions & 1 deletion PSRabbitMq/Connect-RabbitMqChannel.ps1
Expand Up @@ -12,6 +12,10 @@
.PARAMETER Exchange
Optional PSCredential to connect to RabbitMq with
.PARAMETER ExchangeType
Specify the Exchange Type to be Explicitly declared as non-durable, non-autodelete, without any option.
Should you want more specific Exchange, create it prior connecting to the channel, and do not specify this parameter.
.PARAMETER Key
Routing Keys to look for
Expand Down Expand Up @@ -44,6 +48,10 @@
[AllowEmptyString()]
[string]$Exchange,

[parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
[ValidateSet('Direct','Fanout','Topic','Headers')]
[string]$ExchangeType = $null,

[parameter(ParameterSetName = 'NoQueueNameWithBasicQoS',Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[parameter(ParameterSetName = 'NoQueueName',Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[parameter(ParameterSetName = 'QueueName',Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
Expand Down Expand Up @@ -84,7 +92,12 @@
{
$Channel = $Connection.CreateModel()

Write-Progress -id 10 -Activity 'Create SCMB Connection' -Status 'Attempting connection to channel' -PercentComplete 80
Write-Progress -id 10 -Activity 'Create SCMB Connection' -Status 'Attempting connection to channel' -PercentComplete 80

#Actively declare the Exchange (as non-autodelete, non-durable)
if($ExchangeType -and ![string]::IsNullOrEmpty($Exchange)) {
$ExchangeResult = $Channel.ExchangeDeclare($Exchange,$ExchangeType.ToLower())
}

#Create a personal queue or bind to an existing queue
if($QueueName)
Expand Down
39 changes: 26 additions & 13 deletions PSRabbitMq/Register-RabbitMqEvent.ps1
Expand Up @@ -14,6 +14,10 @@
.PARAMETER Exchange
RabbitMq Exchange
.PARAMETER ExchangeType
Specify the Exchange Type to be Explicitly declared as non-durable, non-autodelete, without any option.
Should you want more specific Exchange, create it prior connecting to the channel, and do not specify this parameter.
.PARAMETER Key
Routing Keys to look for
Expand Down Expand Up @@ -61,6 +65,9 @@
.PARAMETER vhost
Create a connection via the specified virtual host, default is /
.PARAMETER ActionData
Allows you to specify an Object to be available in the Action scriptblock triggered upon reception of message.
.PARAMETER IncludeEnvelope
Include the Message envelope (Metadata) of the message. If ommited, only
the payload (body of the message) is returned
Expand All @@ -81,6 +88,10 @@
[parameter(Mandatory = $True)]
[AllowEmptyString()]
[string]$Exchange,

[parameter(Mandatory = $false)]
[ValidateSet('Direct','Fanout','Topic','Headers')]
[string]$ExchangeType = $null,

[parameter(ParameterSetName = 'NoQueueName', Mandatory = $true)]
[parameter(ParameterSetName = 'NoQueueNameWithBasicQoS', Mandatory = $true)]
Expand Down Expand Up @@ -134,27 +145,24 @@
[parameter(ParameterSetName = 'NoQueueNameWithBasicQoS', Mandatory = $true)]
[switch]$global,

[switch]$IncludeEnvelope
)
[switch]$IncludeEnvelope,

if ($PSBoundParameters['Credential'])
{
[string]$ListenerJobName,

$ArgList = $ComputerName, $Exchange, $Key, $Action, $Credential, $Ssl, $LoopInterval, $QueueName, $Durable, $Exclusive, $AutoDelete, $RequireAck,$prefetchSize,$prefetchCount,$global,[bool]$IncludeEnvelope
$ActionData
)

if ( !$PSBoundParameters.ContainsKey('ListenerJobName') ) {
$ListenerJobName = "RabbitMq_${ComputerName}_${Exchange}_${Key}"
}

elseif ($PSBoundParameters['CertPath'])
{

$ArgList = $ComputerName, $Exchange, $Key, $Action, $CertPath, $CertPassphrase, $Ssl, $LoopInterval, $QueueName, $Durable, $Exclusive, $AutoDelete, $RequireAck,$prefetchSize,$prefetchCount,$global,[bool]$IncludeEnvelope
$ArgList = $ComputerName, $Exchange, $ExchangeType, $Key, $Action, $Credential, $CertPath, $CertPassphrase, $Ssl, $LoopInterval, $QueueName, $Durable, $Exclusive, $AutoDelete, $RequireAck,$prefetchSize,$prefetchCount,$global,[bool]$IncludeEnvelope,$ActionData

}

Start-Job -Name "RabbitMq_${ComputerName}_${Exchange}_${Key}" -ArgumentList $Arglist -ScriptBlock {
Start-Job -Name $ListenerJobName -ArgumentList $Arglist -ScriptBlock {
param(
$ComputerName,
$Exchange,
$ExchangeType,
$Key,
$Action,
[PSCredential]
Expand All @@ -172,7 +180,8 @@
$prefetchSize,
$prefetchCount,
$global,
$IncludeEnvelope
$IncludeEnvelope,
$ActionData
)

$ActionSB = [System.Management.Automation.ScriptBlock]::Create($Action)
Expand Down Expand Up @@ -205,6 +214,10 @@
$ChanParams.Add('global',$global)
}

if( $ExchangeType ) {
$ChanParams.Add('ExchangeType',$ExchangeType)
}

#Create the connection and channel
$Connection = New-RabbitMqConnectionFactory @ConnParams
$Channel = Connect-RabbitMqChannel @ChanParams -Connection $Connection
Expand Down
9 changes: 9 additions & 0 deletions PSRabbitMq/Start-RabbitMqListener.ps1
Expand Up @@ -14,6 +14,10 @@
.PARAMETER Exchange
RabbitMq Exchange
.PARAMETER ExchangeType
Specify the Exchange Type to be Explicitly declared as non-durable, non-autodelete, without any option.
Should you want more specific Exchange, create it prior connecting to the channel, and do not specify this parameter.
.PARAMETER Key
Routing Key to look for
Expand Down Expand Up @@ -73,6 +77,10 @@

[parameter(Mandatory = $True)]
[string]$Exchange,

[parameter(Mandatory = $false)]
[ValidateSet('Direct','Fanout','Topic','Headers')]
[string]$ExchangeType = $null,

[parameter(ParameterSetName = 'NoQueueName',Mandatory = $true)]
[parameter(ParameterSetName = 'QueueName',Mandatory = $false)]
Expand Down Expand Up @@ -122,6 +130,7 @@
'Credential' { $ConnParams.Add('Credential',$Credential) }
'vhost' { $ConnParams.Add('vhost',$vhost) }
'Key' { $ChanParams.Add('Key',$Key)}
'ExchangeType' { $ChanParams.Add('ExchangeType',$ExchangeType)}
'QueueName'
{
$ChanParams.Add('QueueName',$QueueName)
Expand Down
24 changes: 17 additions & 7 deletions PSRabbitMq/Wait-RabbitMqMessage.ps1
Expand Up @@ -14,6 +14,10 @@
.PARAMETER Exchange
RabbitMq Exchange
.PARAMETER ExchangeType
Specify the Exchange Type to be Explicitly declared as non-durable, non-autodelete, without any option.
Should you want more specific Exchange, create it prior connecting to the channel, and do not specify this parameter.
.PARAMETER Key
Routing Key to look for
Expand Down Expand Up @@ -72,26 +76,31 @@
the payload (body of the message) is returned
.EXAMPLE
Wait-RabbitMqMessage -ComputerName rabbitmq.contoso.com -Exchange MyExchange -Key "message.key"
Wait-RabbitMqMessage -ComputerName rabbitmq.contoso.com -Exchange MyExchange -Key "message.key"
# Wait for the "message.key" message on the "MyExchange" exchange.
# Wait for the "message.key" message on the "MyExchange" exchange.
.EXAMPLE
Wait-RabbitMqMessage -ComputerName rabbitmq.contoso.com -Exchange MyExchange -Queue MyQueue -Key "message.key" -Ssl Tls12 -Credential $Credential
Wait-RabbitMqMessage -ComputerName rabbitmq.contoso.com -Exchange MyExchange -Queue MyQueue -Key "message.key" -Ssl Tls12 -Credential $Credential
# Connect to rabbitmq.contoso.com over SSL, with credentials stored in $Credential
# Wait for the "message.key" message on the "MyExchange" exchange, "MyQueue" queue.
# Wait for the "message.key" message on the "MyExchange" exchange, "MyQueue" queue.
#>
[Cmdletbinding(DefaultParameterSetName = 'NoQueueName')]
param(
param(
[string]$ComputerName = $Script:RabbitMqConfig.ComputerName,

[parameter(Mandatory = $True)]
[string]$Exchange,
[AllowEmptyString()]
[string]$Exchange,

[parameter(Mandatory = $false)]
[ValidateSet('Direct','Fanout','Topic','Headers')]
[string]$ExchangeType,

[parameter(ParameterSetName = 'NoQueueName',Mandatory = $true)]
[parameter(ParameterSetName = 'QueueName',Mandatory = $false)]
[string]$Key,
[string]$Key,

[parameter(ParameterSetName = 'QueueName',
Mandatory = $True)]
Expand Down Expand Up @@ -142,6 +151,7 @@
'Credential' { $ConnParams.Add('Credential',$Credential) }
'vhost' { $ConnParams.Add('vhost',$vhost) }
'Key' { $ChanParams.Add('Key',$Key)}
'ExchangeType' { $ChanParams.Add('ExchangeType',$ExchangeType)}
'QueueName'
{
$ChanParams.Add('QueueName',$QueueName)
Expand Down

0 comments on commit 44d3131

Please sign in to comment.