Skip to content

Instantly share code, notes, and snippets.

@timpamungkasudemy
timpamungkasudemy / RabbitmqSchemaConfig_One.java
Created September 9, 2019 07:08
Create Direct Exchange using individual methods
// Create Direct Exchange using individual methods
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@timpamungkasudemy
timpamungkasudemy / RabbitmqSchemaConfig_Two.java
Created September 9, 2019 09:51
Create Fanout Exchange using single method that return `Declarables`
// Create Fanout Exchange using single method that return `Declarables`
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Binding.DestinationType;
import org.springframework.amqp.core.Declarables;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@timpamungkasudemy
timpamungkasudemy / RabbitmqSchemaConfig_Three.java
Created September 9, 2019 09:59
Create Topic Exchange using single method that return `Declarables`
// Create Topic Exchange using single method that return `Declarables`
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Binding.DestinationType;
import org.springframework.amqp.core.Declarables;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@timpamungkasudemy
timpamungkasudemy / RabbitmqSchemaConfig_Four.java
Created September 9, 2019 10:09
How to use `BindingBuilder`
// How to use `BindingBuilder`
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@timpamungkasudemy
timpamungkasudemy / AutoDeclareConsumer.java
Created September 9, 2019 10:51
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;
@timpamungkasudemy
timpamungkasudemy / ListenerSample.java
Created October 27, 2019 03:39
Sample Listener if you don't have __TypeID__ but have other header (for example : type)
public class ListenerSample {
// Adjust the header name if required, on @Header parameter
@RabbitListener(queues = "q.finance.invoice")
public void listenInvoiceCreated(@Payload String message, @Header(AmqpHeaders.DELIVERY_TAG) long tag,
@Header("type") String type) throws IOException {
if (StringUtils.equalsIgnoreCase(type, "invoice.paid")) {
log.info("Delegate to invoice paid handler");
} else if (StringUtils.equalsIgnoreCase(type, "invoice.created")) {
log.info("Delegate to invoice created handler");
@timpamungkasudemy
timpamungkasudemy / RabbitmqConfig.java
Created October 27, 2019 03:50
Sample RabbitmqConfig if you don't have __TypeID__ but have other header (for example : type)
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@timpamungkasudemy
timpamungkasudemy / InvoiceListener.java
Created October 27, 2019 03:51
Sample Listener if you don't have __TypeID__ but have other header (for example : type)
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import com.course.finance.message.invoice.InvoiceCreatedMessage;
import com.course.finance.message.invoice.InvoicePaidMessage;
{"tenant_name":"Jogja Chocolate","registered_date":"2018-04-21","buildings":[{"address":"107 Diponegoro Street","type":"Warehouse","size":4000,"occupied":true},{"address":"52 Arjuna Street","type":"Store","size":300,"occupied":true},{"address":"94 Malioboro Street","type":"Store","size":250,"occupied":true}]}
{"tenant_name":"Merapi Batik","registered_date":"2015-02-18","buildings":[{"address":"52 Kaliurang Street","type":"Store","size":800,"occupied":true},{"address":"255 Solo Street","type":"Store","size":400,"occupied":false}]}