Setting answer type from endpoint type address, when getting answer from rabbitmq queue

181 views Asked by At

I have a call to an rabbitmq queue, which in return responds with a json payload.

        <inSequence>
            <call>
                <endpoint>
                    <address statistics="enable" trace="enable" uri="rabbitmq:/AMQPProducerSample?rabbitmq.server.host.name=10.0.0.2&amp;rabbitmq.server.port=5672&amp;rabbitmq.server.virtual.host=dev&amp;rabbitmq.server.vhost=devAuth&amp;rabbitmq.queue.name=hello&amp;rabbitmq.queue.durable=false&amp;rabbitmq.queue.auto.ack=true&amp;rabbitmq.queue.exclusive=false&amp;rabbitmq.replyto.name=false">
                        <suspendOnFailure>
                            <initialDuration>-1</initialDuration>
                            <progressionFactor>-1</progressionFactor>
                            <maximumDuration>0</maximumDuration>
                        </suspendOnFailure>
                        <markForSuspension>
                            <retriesBeforeSuspension>0</retriesBeforeSuspension>
                        </markForSuspension>
                    </address>
                </endpoint>
            </call>
            <header name="Content-Type" scope="transport" value="application/json"/>
            <respond description="respond whatever"/>
        </inSequence>

But when the answer from the queue return, console shows: [2020-10-19 19:20:39,741] WARN {RabbitMQUtils} - Unable to determine content type for message urn:uuid:88BF095D7137B387921603153239744 setting to text/plain

and it adds "text" to my already json payload: {"text":"{"jwt":"5f8e2d21c2184","error":false}"}

How do I set the answer from the rabbitmq to json or do I need to tansform that answer? because it get all strip protected.

2

There are 2 answers

0
Mr. Criptos On BEST ANSWER

The type of the answer, is set by the endpoint as an attribute to the message, as stated by de rabbitmq documentation: https://www.rabbitmq.com/consumers.html#message-properties In case of the php rabbitmq library add 'content_type'=>'application_json' to property array of the message.

1
Sajitha Liyanage On

Please try the following Synapse artifact.

<proxy xmlns="http://ws.apache.org/ns/synapse"
    name="RabbitMQRPCProxy"
    startOnLoad="true"
    trace="enable"
       transports="http">
   <description/>
   <target>
    <inSequence>
        <log level="full">
            <property name="received" value="true"/>
        </log>
        <call>
            <endpoint>
                 <address uri="rabbitmq://?rabbitmq.server.host.name=localhost&amp;rabbitmq.server.port=5672&amp;rabbitmq.server.user.name=guest&amp;rabbitmq.server.password=guest&amp;rabbitmq.queue.name=rpc_queue&amp;rabbitmq.queue.routing.key=rpc_queue&amp;rabbitmq.replyto.name=dummy"/>
            </endpoint>
        </call>
        <log level="full">
            <property name="response" value="true"/>
        </log>
        <respond/>
    </inSequence>
    <outSequence/>
   </target>
</proxy>

Here,

  • When WSO2 Enterprise Integrator(WSO2 EI) starts up, it creates an anonymous, exclusive callback queue.
  • For a remote procedure call request, WSO2 EI sends a message with the following properties:
    • reply_to : This is set to the callback queue
    • correlation_id : This is set to a unique value for every request.
  • The request is then sent to the rpc_queue.
  • The RPC Server waits for requests on that queue. When a request appears, it does the job and sends a message with the result back to the WSO2 EI server, using the queue from the reply_to field with the same correlation_id.
  • WSO2 EI waits for data on the reply_to queue. When a message appears, it checks the correlation_id property. If it matches the value from the request, it returns the response to the application.

For more detail, have a look RabbitMQ listener and sender documentation.