Last active
August 9, 2020 22:11
-
-
Save alucarded/789ed1388e6e1043abdf6e942238311c to your computer and use it in GitHub Desktop.
Spring + RabbitMQ basic configuration with topic exchange
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.phoneservice.phoneservice; | |
import org.springframework.amqp.core.Binding; | |
import org.springframework.amqp.core.BindingBuilder; | |
import org.springframework.amqp.core.Queue; | |
import org.springframework.amqp.core.TopicExchange; | |
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; | |
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.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
@SpringBootApplication | |
public class SignupServiceApplication { | |
static final String EXCHANGE_NAME = "devpeer"; | |
static final String QUEUE_NAME = "signups"; | |
static final String SIGNUP_ROUTING_KEY_BASE = "signup"; | |
static final String SIGNUP_ROUTING_KEY_BINDING_PATTERN = SIGNUP_ROUTING_KEY_BASE + ".#"; | |
@Bean | |
Queue queue() { | |
return new Queue(QUEUE_NAME, false); | |
} | |
@Bean | |
TopicExchange exchange() { | |
return new TopicExchange(EXCHANGE_NAME); | |
} | |
@Bean | |
Binding binding(Queue queue, TopicExchange exchange) { | |
return BindingBuilder.bind(queue).to(exchange).with(SIGNUP_ROUTING_KEY_BINDING_PATTERN); | |
} | |
@Bean | |
ConnectionFactory connectionFactory() { | |
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); | |
// Connect to RabbitMQ server with URI | |
connectionFactory.setUri("amqp://guest:guest@localhost:5672/%2F"); | |
return connectionFactory; | |
} | |
@Bean | |
public RabbitTemplate rabbitTemplate() { | |
RabbitTemplate template = new RabbitTemplate(connectionFactory()); | |
template.setMessageConverter(jsonMessageConverter()); | |
return template; | |
} | |
@Bean | |
public MessageConverter jsonMessageConverter() { | |
return new Jackson2JsonMessageConverter(); | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(SignupServiceApplication.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment