Skip to content

Commit

Permalink
Merge pull request rabbitmq#6 from rabbitmq/rabbitmq-delayed-message-…
Browse files Browse the repository at this point in the history
…exchange-5

moves Erlang examples to Java examples
  • Loading branch information
michaelklishin committed Apr 17, 2015
2 parents 91e630d + add5a58 commit c6c2a3c
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,13 @@ rabbitmq-plugins enable rabbitmq_delayed_message_exchange
To use the delayed-messaging feature, declare an exchange with the
type `x-delayed-message`:

```erlang
... elided code ...

Declare = #'exchange.declare' {
exchange = <<"my-exchange">>",
type = <<"x-delayed-message">>,
durable = true,
auto_delete = false,
arguments = [{<<"x-delayed-type">>,
longstr, <<"direct">>}]
},
amqp_channel:call(Chan, Declare),
... more code ...

```java
// ... elided code ...
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-delayed-type", "direct");
channel.exchangeDeclare("my-exchange", "x-delayed-message", true, false, args);
// ... more code ...
```

Note that we pass an extra header called `x-delayed-type`, more on it
Expand All @@ -46,25 +39,26 @@ under the _Routing_ section.
Once we have the exchange declared we can publish messages providing a
header telling the plugin for how long to delay our messages:

```erlang
... elided code ...
amqp_channel:call(Chan,
#'basic.publish'{exchange = Ex},
#amqp_msg{props = #'P_basic'{headers = [{<<"x-delay">>, signedint, 5000}]},
payload = <<"delayed payload">>}),
amqp_channel:call(Chan,
#'basic.publish'{exchange = Ex},
#amqp_msg{props = #'P_basic'{headers = [{<<"x-delay">>, signedint, 1000}]},
payload = <<"more delayed payload">>}),
... more code ...
```java
// ... elided code ...
byte[] messageBodyBytes = "delayed payload".getBytes("UTF-8");
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("x-delay", 5000);
AMQP.BasicProperties.Builder props = new AMQP.BasicProperties.Builder().headers(headers);
channel.basicPublish("my-exchange", "", props.build(), messageBodyBytes);

byte[] messageBodyBytes2 = "more delayed payload".getBytes("UTF-8");
Map<String, Object> headers2 = new HashMap<String, Object>();
headers2.put("x-delay", 1000);
AMQP.BasicProperties.Builder props2 = new AMQP.BasicProperties.Builder().headers(headers2);
channel.basicPublish("my-exchange", "", props2.build(), messageBodyBytes2);
// ... more code ...
```

In the above example we publish two messages, specifying the delay
time with the `x-delay` header. For this example, the plugin will
deliver to our queues first the message with the body `<<"more delayed
payload">>` and then the one with the body `<<delayed payload">>`.
deliver to our queues first the message with the body `"more delayed
payload"` and then the one with the body `"delayed payload"`.

If the `x-delay` header is not present, then the plugin will proceed
to route the message without delay.
Expand All @@ -73,11 +67,11 @@ to route the message without delay.

This plugin allows for flexible routing via the `x-delayed-type`
arguments that can be passed during `exchange.declare`. In the example
above we used `<<"direct">>` as exchange type. That means the plugin
above we used `"direct"` as exchange type. That means the plugin
will have the same routing behavior shown by the direct exchange.

If you want a different routing behavior, then you could provide a
different exchange type, like `<<"topic">>` for example. You can also
different exchange type, like `"topic"` for example. You can also
specify exchange types provided by plugins. Note that this argument is
**required** and **must** refer to an **existing exchange type**.

Expand Down

0 comments on commit c6c2a3c

Please sign in to comment.