Belajar RabbitMQ - Cara Membuat RabbitMQ Consumer - Java Community

Belajar RabbitMQ - Cara Membuat RabbitMQ Consumer

RabbitMQ Consumer merupakan aplikasi atau komponen yang menerima pesan ke message broker. Pastikan setelah membuat RabbitMQ Consumer, kalian membuat RabbitMQ Publisher untuk mengirimkan pesan ke RabbitMQ Consumer.

Berikut ini merupakan contoh source code untuk membuat RabbitMQ Consumer dengan menggunakan Spring Boot 3.3.2.

dependencies
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
properties
#Server
server.port=8080

#Konfigurasi RabbitMQ
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

#QUEQUE
rabbit.queue-log=queue_log
rabbit.exchange-log=exchange_log
rabbit.routing-log=routing_log
PropertiesRabbit
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "rabbit")
public class PropertiesRabbit {
    private String queueLog;
    private String exchangeLog;
    private String routingLog;
}
ConfigRabbit

Mendefinisikan konfigurasi RabbitMQ, termasuk pembuatan queue dan exchange.

ConfigRabbit
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConfigRabbit {

    @Autowired
    private PropertiesRabbit propertiesRabbit;

    @Bean
    public Queue queue(){
        return new Queue(propertiesRabbit.getQueueLog());
    }

    @Bean
    public TopicExchange exchange(){
        return new TopicExchange(propertiesRabbit.getExchangeLog());
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange exchange){
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(propertiesRabbit.getRoutingLog());
    }

    @Bean
    public MessageConverter converter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public AmqpTemplate template(ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(converter());
        return rabbitTemplate;
    }
}
Consumer
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.logging.Logger;

@Component
public class MessageConsumer {

    private Logger log = Logger.getLogger(this.getClass().getName());

    @RabbitListener(queues = "queue_log")
    public void create(String message) {
        log.info("Pesan : " + message);
    }
}

Dengan mengikuti tutorial ini, Anda telah membuat aplikasi sederhana menggunakan RabbitMQ sebagai consumer.

Subscribe to receive free email updates:

0 Response to "Belajar RabbitMQ - Cara Membuat RabbitMQ Consumer"

Posting Komentar