Skip to content

Instantly share code, notes, and snippets.

@timpamungkasudemy
Created September 9, 2019 10:51
Show Gist options
  • Save timpamungkasudemy/381386c99f0c8d9661d39c321373cf92 to your computer and use it in GitHub Desktop.
Save timpamungkasudemy/381386c99f0c8d9661d39c321373cf92 to your computer and use it in GitHub Desktop.
Create RabbitMQ schema from consumer, using @RabbitListener
// Create RabbitMQ schema from consumer, using @RabbitListener
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class AutoDeclareConsumer {
private static final Logger log = LoggerFactory.getLogger(AnotherDummyConsumer.class);
@RabbitListener(bindings =
@QueueBinding(
value = @Queue(name = "q.fanout.one", durable = "true"),
exchange = @Exchange(name = "x.fanout", type = ExchangeTypes.FANOUT, durable = "true"),
key = "routing-key-one"))
public void listenFanout(String message) {
log.info("Message is : {}", message);
}
@RabbitListener(bindings =
@QueueBinding(
value = @Queue(name = "q.direct.one", durable = "true"),
exchange = @Exchange(name = "x.direct", type = ExchangeTypes.DIRECT, durable = "true"),
key = "routing-key-one"))
public void listenDirect(String message) {
log.info("Message is : {}", message);
}
@RabbitListener(bindings =
{
@QueueBinding(
value = @Queue(name = "q.topic.one", durable = "true"),
exchange = @Exchange(name = "x.topic", type = ExchangeTypes.TOPIC, durable = "true"),
key = "routing-key-one"),
@QueueBinding(
value = @Queue(name = "q.topic.two", durable = "true"),
exchange = @Exchange(name = "x.topic", type = ExchangeTypes.TOPIC, durable = "true"),
key = "routing-key-two")
})
public void listenTopic(String message) {
log.info("Message is : {}", message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment