= Publish Messages and Consume Replies Using the JMS Connector :page-aliases: connectors::jms/jms-publish-consume.adoc Anypoint Connector for JMS (JMS Connector) *Publish consume* operation enables you to publish a message to any specified destination, and then consume the message reply on a different specified destination. Request and reply is a very common pattern between applications. When using this operation, the `JMSReplyTo` header of the outgoing message contains the ID of the destination on which your application waits to consume a response. The destination might be a well-known destination or a temporary destination created with the purpose of receiving a single response message. The result of the operation is the message received as a response, with the same structure as invoking the *Consume* operation. == Receive a Reply on a Temporary Queue To set a temporary queue, you configure the message being sent, and the operation automatically waits for a response. In the following example, you configure the *Publish consume* operation to publish a message with the following characteristics: * Current `payload` as its body * `JMSReplyTo` header configured to a temporary destination * *Destination* field set to `targetDestination` Then, the operation automatically tries to consume the response from the temporary queue until a message is consumed or the *Maximum wait* is reached, in which case it fails with a `JMS:TIMEOUT` error. To configure the operation follow these steps: . In your Studio flow, select the *Publish consume* operation. . In the connector configuration screen, set the *Destination* field to `targetDestination`. . Scroll down to the *Consume Configuration* section and set the *Maximum wait* field to `20`. . Set the *Maximum wait unit* to `SECONDS`. .Temporary Queue configuration image::jms-publishconsume-config-1.png[In the Publish consume configuration screen, set the Destination field to the name of the Destination where the message is sent] In the XML editor, the `destination` and `` configuration looks like this: [source,xml,linenums] ---- ---- == Receive a Reply On a Known Queue To set a known queue for replies, configure the *Reply To* field on the outgoing message. In the following example, you configure the *Publish consume* operation to publish a message to the `targetDestination` with the following characteristics: * Current `payload` as its body * `JMSReplyTo` header configured to the known queue * *Reply To* field set to `replyToDestination` Then, the operation automatically tries to consume the response from the known `replyToDestination` queue until a message is consumed or the *Maximum wait* is reached, in which case it fails with a `JMS:TIMEOUT` error. To configure the operation follow these steps: . In your Studio flow, select the *Publish consume* operation. . In the connector configuration screen, set the *Destination* field to `targetDestination`. . In the *Message* section, set the *Reply To* field to *Edit inline*. . Set the *Destination Name* field to `replyToDestination`. . Scroll down to the *Consume Configuration* section and set the *Maximum wait* field to `20`. . Set the *Maximum wait unit* to `SECONDS`. .Known Queue configuration image::jms-publishconsume-config-2.png[In the Publish consume configuration screen, set the Reply To field to The JMSReplyTo header information of the Destination where the message is replied to] In the XML editor, the `destination` and `` configuration looks like this: [source,xml,linenums] ---- ---- == Configure Request and Reply Patterns JMS uses two well-known request and reply patterns that help you understand how request and reply messages are correlated. In the *Request-Reply Pattern* field of the operation, you can configure the *CORRELATION_ID (Default)* and *MESSAGE_ID* patterns. Both ensure that an incoming message is the correct reply to an outgoing message. You can also configure the field to *NONE*, causing the consumer to try to consume from the reply queue without using any selector to correlate messages. This option is useful when working with temporal queues that ensure that the queue has only one message. === Configure the Correlation ID Pattern The *CORRELATION_ID (Default)* pattern implies that the operation, after publishing a message to the destination queue, consumes a message using a selector that expects a message with the same correlation ID that was used to publish the request message. The message correlation ID is a value used to link one message with another. In the first flow of the following example, you configure the *Publish consume* operation to publish a message to the `targetDestination` with the following characteristics: * Current `payload` as its body * `JMSReplyTo` header configured to the known queue * *Reply To* field set to `replyToDestination` * *Request-Reply Pattern* field set to *CORRELATION_ID (Default)* + Then, in the second flow, an *On New Message* source reads the published message and responds using the same correlation ID pattern. A *Logger* component, logs the message response. + Back in the first flow, the *Publish consume* operation consumes the response received and then another *Logger* component, and logs the final message. To configure the operation follow these steps: . In your Studio flow, select the *Publish consume* operation. . In the connector configuration screen, set the *Destination* field to `targetDestination`. . Set the *Request-Reply Pattern* to *CORRELATION_ID (Default)*. . In the *Message* section, set the *Reply To* field to *Edit inline*. . Set the *Destination Name* field to `replyToDestination`. . Drag a *Logger* component to right of the Publish consume. . Set the *Message* field to `Received message from 'replyToDestination' with Correlation ID: #[attributes.headers.correlationId]`. . Drag a JMS *On New Message* source below the first flow. . Set the *Destination* field to `targetDestination`. . Scroll down to the *Response* section and set the *Request-Reply Pattern* to *CORRELATION_ID (Default)*. . Drag another *Logger* component to right of *On New Message*. . Set the *Message* field to `About to reply to 'targetDestination' with Correlation ID: #[attributes.headers.correlationId]`. .Correlation ID pattern implementation image::jms-publishconsume-correlation-example.png[Set the Request-Reply Pattern to CORRELATION_ID (Default)] In the XML editor, the `requestReplyPattern` configuration looks like this: [source,xml,linenums] ---- ---- === Configure the Message ID Pattern The *MESSAGE_ID* pattern implies that the operation, after publishing a message to the destination queue, consumes a message using a selector that expects a message with a message ID of the same value as the message ID of the request message. The message ID is a value that uniquely identifies each message sent by a provider. In the first flow of the following example, you configure the *Publish consume* operation to publish a message to the `targetDestination` with the following characteristics: * Current `payload` as its body * `JMSReplyTo` header configured to the known queue * *Reply To* field set to `replyToDestination` * *Request-Reply Pattern* field set to *MESSAGE_ID* Then, in the second flow, an *On New Message* source reads the published message and responds using the same message ID pattern. A *Logger* component, logs the message response. + Back in the first flow, the *Publish consume* operation consumes the response received and then another *Logger* component and logs the final message. To configure the operation follow these steps: . In your Studio flow, select the *Publish consume* operation. . In the connector configuration screen, set the *Destination* field to `targetDestination`. . Set the *Request-Reply Pattern* to *MESSAGE_ID*. . In the *Message* section, set the *Reply To* field to *Edit inline*. . Set the *Destination Name* field to `replyToDestination`. . Drag a *Logger* component to right of *Publish consume*. . Set the *Message* field to `Received message from 'replyToDestination' with Message ID: #[attributes.headers.correlationId]`. . Drag a JMS *On New Message* source below the first flow. . Set the *Destination* field to `targetDestination`. . Scroll down to the *Response* section and set the *Request-Reply Pattern* to *MESSAGE_ID*. . Drag another *Logger* component to right of *On New Message*. . Set the *Message* field to `About to reply to 'targetDestination' with Message ID: #[attributes.headers.messageId]`. .Message ID pattern implementation image::jms-publishconsume-message-example.png[Set the Request-Reply Pattern to MESSAGE_ID] [source,xml,linenums] ---- ---- == See Also * xref:jms-publish.adoc[Publish Messages] * xref:jms-examples.adoc[JMS Connector Examples]