Recently I have been having a keen interest on Microservice Architecture using Spring Boot. My implementation has two Spring boot applications;
Application One receives requests from a RESTful API, converts and sends jSON payload to a RabbitMQ queueA.
Application Two, has subscribed to queueA, receives the jSON payload(Domain Object User) and is supposed to activate a service within Application Two eg. send email to a user.
Using no XML in my Application Two configuration, how do I configure a converter that will convert the jSON payload received from RabbitMQ into a Domain Object User.
Below are snippets from Spring Boot configurations on Application Two
Application.class
@SpringBootApplication
@EnableRabbit
public class ApplicationInitializer implements CommandLineRunner {
final static String queueName = "user-registration";
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
AnnotationConfigApplicationContext context;
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
TopicExchange topicExchange() {
return new TopicExchange("user-registrations");
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}
public static void main(String[] args) {
SpringApplication.run(ApplicationInitializer.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("Waiting for messages...");
}
}
TestService.java
@Component
public class TestService {
/**
* This test verifies whether this consumer receives message off the user-registration queue
*/
@RabbitListener(queues = "user-registration")
public void testReceiveNewUserNotificationMessage(User user) {
// do something like, convert payload to domain object user and send email to this user
}
}