# `Alternera.Plugins.AnypointMq`
[🔗](https://github.com/10e-ab/alternera-anypoint-mq/blob/v0.9.0/lib/alternera/plugins/anypoint_mq.ex#L1)

Publishes to and consumes from Anypoint MQ queues and exchanges, through
the broker's REST API. One step does one `operation`:

  * `publish` sends one message to a destination: its body is the step's
    `source` (the side's body by default), with `properties`, an optional
    `message_id` (a new UUID by default), `message_group_id` (FIFO
    queues) and `delivery_delay_ms`;
  * `consume` long-polls a queue for up to `batch_size` messages, waiting
    up to `polling_time_ms` for the first, and locks each for
    `lock_ttl_ms`. Its outputs are the messages (`messages`, `message`
    the first, `count`). A poll that brings none ends the pipeline with
    nothing (`when_empty: "return"`, the default), which a trigger's run
    counts as empty, or goes on with no messages (`"continue"`). With
    `ack: "immediate"` the messages are acknowledged as they arrive
    (at most once); the default, `"manual"`, leaves that to later steps;
  * `ack` acknowledges one message (`message_id` and `lock_id`), which
    deletes it from the queue;
  * `nack` releases a message's lock, so the queue delivers it again.

    %{"operation" => "consume",
      "broker_url" => "https://mq-us-east-1.anypoint.mulesoft.com/api/v1",
      "organization_id" => "{{MQ_ORG}}", "environment_id" => "{{MQ_ENV}}",
      "destination" => "orders",
      "proxy_user_id" => "0192…",
      "batch_size" => 5}

A source trigger (`Pipelines.Trigger`, mode message, source
`anypoint_mq`) reads a queue itself through `AnypointMq.Source`: it runs
its pipeline once per message and acknowledges, releases or delays each
by how its run ended. A pipeline can also consume with the step, run by a
continuous trigger: a consume step, a For each over its messages that
does the work and acknowledges each message (`${input.item.message_id}`,
`${input.item.lock_id}`), and nothing when the poll was empty.

## The calls

Every call goes to the forward side as the proxy user `proxy_user_id`
names (`Plugin.Provisional.Outbound`), as a Request step's does, and is
recorded as an exchange of its own under the step's. It carries the
token of the tenant's auth profile for the broker's host
(`Plugin.Provisional.Credentials.for_call/3`): an OAuth2 client
credentials profile whose token URL is the broker's `/authorize`, with
the client app's id and secret. The step holds no credentials, and a
`401` gets one more call with a token fetched then. No profile, or no
token, fails the step `auth_failed` and sends nothing.

`broker_url`, `organization_id` and `environment_id` may be set by a
Configuration step (`configurable`), so one Configuration step names the
broker for every step after it; each is a plain template, so a tenant
variable (`{{MQ_ORG}}`) fills it too. `destination`, `message_id`,
`lock_id`, `message_group_id` and the properties' values are plain
templates: none holds a secret, since the broker's credentials come
from the tenant's auth profile and every one of them is carried in the
message, which the broker keeps and hands to its consumers.

## Results

A status outside 2xx fails `http_status`, with the status and the start
of the broker's answer; an ack or nack the broker answers with `status:
"failure"` (an expired lock) fails `mq_failure`; an answer that is not
the JSON it should be fails `invalid_response`. A failed call keeps the
outbound call's type (`connection`, `timeout`, `tls`, …).

A message is `%{"message_id", "lock_id", "body", "properties",
"headers", "delivery_count", "created"}`, plus `"json"` when its body
parses as JSON.

## In the preview

The step reaches outside (`side_effect?/0`): a preview sends nothing and
says what it would call, with the call's `fingerprint`; a played one
runs for real and returns its `record`. In a request phase that runs
provisionally it waits for the deciding pass.

# `defaults`

```elixir
@spec defaults() :: %{required(String.t()) =&gt; term()}
```

A consume step's defaults, by key.

# `operation_keys`

```elixir
@spec operation_keys(String.t()) :: [String.t()]
```

The settings keys `operation` takes beside the common ones.

# `operations`

```elixir
@spec operations() :: [String.t()]
```

The operations a step does.

# `range`

```elixir
@spec range(String.t()) :: {integer(), integer()}
```

The inclusive range of a number setting.

# `source`

```elixir
@spec source() :: module()
```

The queue as a message source, for a source trigger (`AnypointMq.Source`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
