All Projects → quarkiverse → quarkus-rabbitmq-client

quarkiverse / quarkus-rabbitmq-client

Licence: Apache-2.0 License
Quarkus extension supporting RabbitMQ

Programming Languages

java
68154 projects - #9 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to quarkus-rabbitmq-client

quarkus-resteasy-problem
Unified error responses for Quarkus REST APIs via Problem Details for HTTP APIs (RFC7807). Supports Quarkus 2.0+ and 1.4+
Stars: ✭ 36 (+9.09%)
Mutual labels:  quarkus-extension
quarkus-jberet
Quarkus Extension for Batch Applications.
Stars: ✭ 26 (-21.21%)
Mutual labels:  quarkus-extension
quarkus-google-cloud-services
Google Cloud Services Quarkus Extensions
Stars: ✭ 42 (+27.27%)
Mutual labels:  quarkus-extension
quarkus-freemarker
Quarkus Freemarker Extension
Stars: ✭ 13 (-60.61%)
Mutual labels:  quarkus-extension
quarkus-mybatis
Quarkus MyBatis Extension
Stars: ✭ 45 (+36.36%)
Mutual labels:  quarkus-extension
quarkus-logging-manager
Quarkus extension that allows you to view the log online and change log levels using a UI
Stars: ✭ 25 (-24.24%)
Mutual labels:  quarkus-extension
quarkus-github-app
Develop your GitHub Apps in Java with Quarkus.
Stars: ✭ 28 (-15.15%)
Mutual labels:  quarkus-extension

Quarkiverse RabbitMQ Client

All Contributors version

This is a Quarkus 2 extension for the RabbitMQ Java Client.

RabbitMQ is a popular message broker. This Quarkus extension provides a client for RabbitMQ which is configurable using the application.properties.

Note: Looking for the Quarkus 1.x extension, see the 1.x branch for the details.

Coordinates

<dependency>
    <groupId>io.quarkiverse.rabbitmqclient</groupId>
    <artifactId>quarkus-rabbitmq-client</artifactId>
    <version>0.5.0.Final</version>
</dependency>

Usage

Assuming you have RabbitMQ running on localhost:5672 you should add the following properties to your application.properties and fill in the values for <username> and <password>.

quarkus.rabbitmqclient.virtual-host=/
quarkus.rabbitmqclient.username=<username>
quarkus.rabbitmqclient.password=<password>
quarkus.rabbitmqclient.hostname=localhost
quarkus.rabbitmqclient.port=5672

Once you have configured the properties, you can start using the RabbitMQ client.

@ApplicationScoped
public class MessageService {

    private static final Logger log = LoggerFactory.getLogger(MessageService.class);

    @Inject
    RabbitMQClient rabbitMQClient;

    private Channel channel;

    public void onApplicationStart(@Observes StartupEvent event) {
        // on application start prepare the queus and message listener
        setupQueues();
        setupReceiving();
    }

    private void setupQueues() {
        try {
            // create a connection
            Connection connection = rabbitMQClient.connect();
            // create a channel
            channel = connection.createChannel();
            // declare exchanges and queues
            channel.exchangeDeclare("sample", BuiltinExchangeType.TOPIC, true);
            channel.queueDeclare("sample.queue", true, false, false, null);
            channel.queueBind("sample.queue", "test", "#");
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    private void setupReceiving() {
        try {
            // register a consumer for messages
            channel.basicConsume("sample.queue", true, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    // just print the received message.
                    log.info("Received: " + new String(body, StandardCharsets.UTF_8));
                }
            });
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    public void send(String message) {
        try {
            // send a message to the exchange
            channel.basicPublish("test", "#", null, message.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}

You do not need to worry about closing connections as the RabbitMQClient will close them for you on application shutdown.

Multiple RabbitMQ Clients

The extension supports having multiple RabbitMQ clients. You can add named RabbitMQ clients as follows.

quarkus.rabbitmqclient.<name>.virtual-host=/
quarkus.rabbitmqclient.<name>.username=<username>
quarkus.rabbitmqclient.<name>.password=<password>
quarkus.rabbitmqclient.<name>.hostname=localhost
quarkus.rabbitmqclient.<name>.port=5672

All configuration options that are available on the default non named RabbitMQ client are available. Injecting a named RabbitMQ client, e.g. foo, can be achieved as follows.

@ApplicationScoped
public class MessageService {
    
    @Inject
    @NamedRabbitMQClient("foo")
    RabbitMQClient fooClient;
}

It is possible to use multiple RabbitMQ clients in the same class as long as they are all named, or in combination with the default client. The name default is reserved for the default client and if used will trigger a deployment exception.

Disabling Clients

It is possible to disable clients using the quarkus.rabbitmqclient.<client-nane>.enabled=false configuration property. To disable the default client, use quarkus.rabbitmqclient.enabled=false.

Metrics

Both Micrometer and SmallRye Metrics are supported and enabled by default if the quarkus-micrometer or quarkus-smallrye-metrics dependency is included in the project. If both are present micrometer will be used.

Metrics are gathered on a per-client basis and tagged with name=<client-name> of the client. The default clients is tagged with name=default and are all prefixed with rabbitmq.

It is possible to opt-out of metrics by specifying quarkus.rabbitmqclient.metrics.enabled=false. This will disable all metrics gathering.

License

This extension is licensed under the Apache License 2.0.

Contributors

Thanks goes to these wonderful people (emoji key):


Bas Passon

💻 🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].