RamblingCookieMonster / PSRabbitMq

PowerShell module to send and receive messages from a RabbitMq server
http://ramblingcookiemonster.github.io/RabbitMQ-Intro/
MIT License
47 stars 29 forks source link

How to reply to a message? #26

Closed schneidr closed 6 years ago

schneidr commented 6 years ago

I'm implementing RPC via direct reply-to messages. The PHP part that sends a request works, I get the request in PowerShell, but I find nothing on how to send back an acknowledgement.

working variant 1:

while ($true) {
    Try {
        $data = Wait-RabbitMqMessage -Exchange $ExchangeName -Key $QueueName -QueueName $QueueName -Timeout 120 @Params -ErrorAction SilentlyContinue
        $data = $data |ConvertFrom-Json
        $data
    } Catch {
        # do nothing
        $_.Exception
    }
}

working variant 2:

Start-RabbitMqListener -ComputerName 'localhost' -Exchange $ExchangeName -QueueName $QueueName -Key $QueueName -Credential $credentials |% { $_ }

Both variants print out the message. But all I get is a string ... how do I send a response?

gpduck commented 6 years ago

I think using the -IncludeEnvelope parameter (with either method) will give you an object that includes the headers (including properties.reply_to and properties.reply_to_address) which should give you what you need to craft the response (using Send-RabbitMqMessage).

schneidr commented 6 years ago

Thanks, that was the key.

For completeness, when somebody runs into the same problem, this is how I got it working:

$data = Wait-RabbitMqMessage -Exchange $ExchangeName -QueueName $QueueName -Key $QueueName `
    -Timeout 120 -IncludeEnvelope @Params -ErrorAction SilentlyContinue
$return = @{
    result = 'ok'
}
# do whatever you want with $data.payload, store your result in $return
Send-RabbitMqMessage -ReplyTo $data.properties.reply_to -Key $data.properties.reply_to -DeliveryMode 2 `
    -CorrelationID $data.properties.correlation_id -ContentType 'application/json' `
    @Params -InputObject $return