|
| 1 | +#! /usr/bin/env elixir |
| 2 | +require Logger |
| 3 | + |
| 4 | +# Installing the rabbitmq_stream Library |
| 5 | +Mix.install([ |
| 6 | + {:rabbitmq_stream, "~> 0.4.1"} |
| 7 | +]) |
| 8 | + |
| 9 | +# First we start a Connection to the RabbitMQ Server |
| 10 | +{:ok, connection} = RabbitMQStream.Connection.start_link() |
| 11 | + |
| 12 | +# We can assume the stream doesn't exist yet, and attempt to create it. If it already exists, |
| 13 | +# it should be still be good to go. |
| 14 | +RabbitMQStream.Connection.create_stream(connection, "my_stream") |
| 15 | + |
| 16 | +# Before publishing a message, we need to declare a producer. It is required by the |
| 17 | +# RabbitMQ Sever to prevent message duplication. |
| 18 | +{:ok, producer_id} = |
| 19 | + RabbitMQStream.Connection.declare_producer(connection, "my_stream", "my_producer") |
| 20 | + |
| 21 | +# Each producer has a sequence number, that must must be published with the message, and |
| 22 | +# incremented after each message. |
| 23 | +{:ok, sequence_number} = |
| 24 | + RabbitMQStream.Connection.query_producer_sequence(connection, "my_stream", "my_producer") |
| 25 | + |
| 26 | +# Now we can publish a message. Note that we only specify the producer_id and sequence number. |
| 27 | +# The target Stream is already tracked by the server based on the producer_id. |
| 28 | +:ok = |
| 29 | + RabbitMQStream.Connection.publish(connection, producer_id, sequence_number + 1, "Hello, World!") |
| 30 | + |
| 31 | +Logger.info("Published: \"Hello, World!\"") |
0 commit comments