← Wiki

Point-to-Point Channel in Mule: each paid order reaches a single invoice issuer through the Anypoint MQ queue

  • 9 min read
On this page

A paid order needs an invoice, and only one. If the order message reached two issuers, the customer would receive two invoices. The Point-to-Point Channel guarantees that each message is consumed by a single receiver, even when several are listening. In Mule 4, the channel is a queue: an Anypoint MQ queue between applications, or a VM queue, which also works inside one application. Because the Anypoint MQ queue exists on the broker, part of the pattern is Anypoint MQ configuration, outside the application’s XML.

The problem

The payments API confirms the payment of order ORD-4501 and has to ask for the invoice. The invoice request is a command: someone has to execute it, once. With a single issuer, a direct call would do. But the issuer is scaled out into replicas to handle the volume, and the payments API should not choose which replica issues each invoice, nor issue twice when two replicas are up.

The pattern in the book

The book asks how the caller can be sure that exactly one receiver will receive the document or execute the call. The solution is to send the message on a Point-to-Point Channel, which guarantees that only one receiver receives each message (Point-to-Point Channel).

The channel can have several receivers. If several try to consume the same message, the channel guarantees that only one succeeds, and the receivers do not need to coordinate among themselves. Several receivers can consume different messages at the same time, but each message is consumed by only one. That is the basis of Competing Consumers, where several consumers on the same channel share the load. The opposite is the Publish-Subscribe Channel, which delivers a copy of each message to each subscriber.

The diagram redraws the book’s sketch (CC BY 4.0) in the order scenario:

The payments API sends the invoice request to the point-to-point channel. The channel delivers this message to Issuer 1. Issuer 2 does not receive the message (dashed arrow).

In Mule

The Anypoint MQ documentation describes the broker as the way for applications to communicate by publishing a message to a queue for another application to consume (Anypoint MQ Overview). The queue is the point-to-point channel. What turns the queue into publish-subscribe is the message exchange: the queues bound to it each receive a copy (message exchanges). Without an exchange, the publisher is talking to one consumer per message.

In the consuming application, the queue has a Subscriber, the flow’s source. Single delivery holds while the message is with one consumer: with the prefetch strategy, a message that a consumer took from the queue and kept in its local buffer shows up as in-flight to the others and cannot be consumed by them (Anypoint MQ Subscriber Source). If the consumer does not acknowledge within the acknowledgment timeout, the message goes back to the queue (Configuring and Using Queues) and may go to another consumer.

The VM Connector handles communication inside the application and between applications through transient or persistent queues; that a VM queue delivers each message to a single consumer, like the Anypoint MQ one, is this page’s reading, which the documentation does not state. In a cluster, persistent queues live in the memory grid, and Mule decides whether to process the message on the originating node or send it to another node (VM Connector).

The payments API, a Mule application, does a Publish to the invoice-requests queue of Anypoint MQ. Each replica of the Invoice issuer (replica 1 and replica 2) consumes the queue through a Subscriber.

The queue is created on the Anypoint MQ Destinations page, with the message time to live (TTL, up to two weeks), the acknowledgment timeout and, if there is one, the dead-letter queue (Configuring and Using Queues). None of this is in the application’s XML.

In the MuleSoft documentation

The documentation page Enterprise Integration Patterns Using Mule maps the Point-to-Point Channel to the default channel inside a flow, with no link; from the Message Channel row of the same table, this page’s reading is that this is the connection between one processor and the next. This page covers the form between applications, the Anypoint MQ queue, which the table does not cite in that row.

The example

The three blocks in this section are reference configuration. The queue name (invoice-requests), the flow name and the body fields (orderId, amount) are illustration; the connector elements (config, connection, publish, body, subscriber) and their attributes appear in the examples on the Subscriber and Publish pages. They do not run in the mulesoft-patterns-examples repository: they need an Anypoint MQ broker, and while the connector is free, connecting to the service requires a license (Anypoint MQ Connector).

The global configuration points to the region’s URL and uses the ID and secret of an Anypoint MQ client app (the form is that of the example on the Subscriber page). The secret comes from a secure property, which needs the Secure Properties module and a secure-properties:config in the application.

<anypoint-mq:config name="Anypoint_MQ_Config">
    <anypoint-mq:connection url="${anypoint.mq.url}"
        clientId="${anypoint.mq.clientId}"
        clientSecret="${secure::anypoint.mq.clientSecret}"/>
</anypoint-mq:config>

After confirming the payment, the API’s flow publishes the invoice request to the queue. The Publish operation takes the queue name in destination, and the body can be built with DataWeave in anypoint-mq:body (Publish).

<anypoint-mq:publish config-ref="Anypoint_MQ_Config" destination="invoice-requests">
    <anypoint-mq:body><![CDATA[#[output application/json ---
    {
        orderId: payload.orderId,
        amount: payload.amount
    }]]]></anypoint-mq:body>
</anypoint-mq:publish>

The invoice issuer listens on the queue with the Subscriber. Each replica of the application runs the same flow, and the queue delivers each request to one of them.

<flow name="invoice-request-consumer">
    <anypoint-mq:subscriber config-ref="Anypoint_MQ_Config"
        destination="invoice-requests"/>
    <flow-ref name="sf-issue-invoice"/>
</flow>

With no acknowledgment attribute, the Subscriber uses the AUTO mode, the default: the connector acknowledges the message only if the flow finishes without an error (Anypoint MQ Subscriber Source). The acknowledgment is what takes the message off the channel; until then, it belongs to the consumer that picked it up.

Caveats

  • One consumer per delivery, not per processing. Anypoint MQ standard queues give the “at least once” guarantee and, because of the distributed architecture, occasionally deliver the same message more than once; FIFO queues give the “exactly once” guarantee, with deduplication by message ID within a five-minute window (Configuring and Using Queues). With a standard queue, the issuer needs to be an Idempotent Receiver, or the second delivery becomes the second invoice.
  • Processing that takes longer than the acknowledgment. KB article 001119407 describes messages processed two or more times, every two minutes, because the Subscriber in AUTO did not respect the queue’s acknowledgment timeout; the fix is to set acknowledgementTimeout on the Subscriber itself (Anypoint MQ Message Getting Processed Twice). With several replicas, the second delivery may land on another one while the first is still issuing the invoice (this page’s inference).
  • In-flight messages that look stuck. KB article 001117384 shows up to three in-flight messages on a queue with maxConcurrency="1" and a one-message prefetch: one in the flow, one in the local buffer and one reserved by the prefetch so it does not go to another consumer. The article says this is expected behavior and that the number grows with more replicas, more concurrency or a larger buffer (Anypoint MQ | Number of In-Flight Messages with Flow maxConcurrency=1 and Prefetch maxLocalMessages=1 is Too High). According to the Subscriber page, a message in the local buffer is not available to another consumer; that an idle replica ends up with no work because of this is this page’s inference.
  • A topic is not a queue. In a 2023 Stack Overflow case, the question was whether several workers reading a queue would process the same message twice; a comment answers that on a queue only the consumer that fetches first processes it, and the author clarifies they were listening to a topic, where several queues receive the same message; another comment points out that Anypoint MQ is not JMS compatible and has no topics like JMS (How Queues behave in mule 4 multi workers API?). In Anypoint MQ, the equivalent is the message exchange: a queue bound to it receives copies, and single delivery holds only within each queue (this page’s inference).
  • The message that never succeeds. With a dead-letter queue assigned, Anypoint MQ reroutes the message after a number of delivery attempts (default 10) (Configuring and Using Queues). The documentation does not say what happens without one; the reading is that a message that always fails keeps going back to the queue until the TTL erases it (this page’s inference). The Dead Letter Channel page shows the DLQ, and the Invalid Message Channel separates the message that cannot even be read from the one that merely failed.

When not to use it

When the event matters to several different systems, each one needs its own copy: that is the Publish-Subscribe Channel. When the caller needs the result in the response (the invoice number, for example), the queue only confirms that it accepted the message, and a synchronous call to the issuer is the simpler contract. And when there is a single consumer, a single replica and no need to decouple the caller’s timing from the processor’s, the queue adds a broker to operate with no gain (this page’s recommendation).

Running the example and the tests

This page has no runnable example or MUnit suite in the repository. The queue exists only on the Anypoint MQ broker, and the series does not simulate Anypoint MQ with VM queues. Competing Consumers has a runnable example of single delivery without a broker, with an atomic reservation in the Object Store in place of the queue. To see the pattern with a real queue, you need an account with Anypoint MQ:

  1. In Anypoint Platform > MQ > Destinations, create the invoice-requests queue (Configuring and Using Queues).
  2. Create an Anypoint MQ client app and use its ID and secret in the global configuration above (connector reference).
  3. Run two instances of the issuer, publish a few requests to invoice-requests and check in the logs that each request was processed by a single instance.

The other patterns are in the catalog of integration patterns in Mule and, by component name, in the index by component.