Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 20 ■ Your First Spring XD Application Figure 20-10. Spring XD Counter at http://localhost:9393/metrics/counters/pdfcount Figure 20-10 shows the result of sending the PDF messages. In this case, the counter’s value is 19. Now you can start sending as many documents as you want. Listing 20-1 shows a Spring Boot client that uses rabbitmq and uses a consumer, just to see how it will interact with your Spring application and the Spring XD. Listing 20-1. rabbitmqStream.groovy @Log @Configuration @EnableRabbitMessaging class MyDocumentsRabbitMQStream implements CommandLineRunner { @Autowired RabbitTemplate rabbitTemplate private String queueName = "docs-pdf" private String exchangeName = "mydocuments" private String routingKey = ".pdf" @Bean Queue queue() { new Queue(queueName, false) } 285

Chapter 20 ■ Your First Spring XD Application @Bean DirectExchange exchange() { new DirectExchange("mydocuments") } @Bean Binding binding(Queue queue, DirectExchange exchange) { BindingBuilder .bind(queue) .to(exchange) .with("docs-pdf") } @Bean SimpleMessageListenerContainer container(CachingConnectionFactory connectionFactory) { return new SimpleMessageListenerContainer( connectionFactory: connectionFactory, queueNames: [queueName], messageListener: new MessageListenerAdapter(new Receiver(latch:latch), "receive") ) } void run(String... args) { log.info "Sending Documents..." 500.times { rabbitTemplate.convertAndSend(exchangeName, routingKey, "Document(id: ${it}, created: ${new Date().format('yyyy-MM-dd HH:mm:ss') })") sleep 1000 } } } @Log class MyDocumentsConsumer { } def receive(String message) { log.info "Document Received: ${message}" } Listing 20-1 shows a Spring Boot rabbitmq example that you can use. Take a look at the new annotation called @EnableRabbitMessaging that will enable the Spring AMQP module into your Spring Boot application, and the CommandLineRunner interface. This interface has the run method that you need to override; it will be the entry point for your Spring Boot application. You can run this Spring Boot application by executing spring run rabbitmqStream.groovy After executing the above command, the application will send 500 messages (of course, you can change this number to 1,000,0000 messages) and it will use a PDF as a routing key. The purpose of this example is just to experience the power of Spring XD. Don’t forget while you are running this example to take a look at http://localhost:9393/metrics/counters/pdfcount for the pdfcount value. 286

Chapter 20 ■ Your First <strong>Spring</strong> XD Application<br />

@Bean<br />

DirectExchange exchange() {<br />

new DirectExchange("mydocuments")<br />

}<br />

@Bean<br />

Binding binding(Queue queue, DirectExchange exchange) {<br />

BindingBuilder<br />

.bind(queue)<br />

.to(exchange)<br />

.with("docs-pdf")<br />

}<br />

@Bean<br />

SimpleMessageListenerContainer container(CachingConnectionFactory connectionFactory) {<br />

return new SimpleMessageListenerContainer(<br />

connectionFactory: connectionFactory,<br />

queueNames: [queueName],<br />

messageListener: new MessageListenerAdapter(new Receiver(latch:latch), "receive")<br />

)<br />

}<br />

void run(String... args) {<br />

log.info "Sending Documents..."<br />

500.times {<br />

rabbitTemplate.convertAndSend(exchangeName, routingKey, "Document(id: ${it},<br />

created: ${new Date().format('yyyy-MM-dd HH:mm:ss') })")<br />

sleep 1000<br />

}<br />

}<br />

}<br />

@Log<br />

class MyDocumentsConsumer {<br />

}<br />

def receive(String message) {<br />

log.info "Document Received: ${message}"<br />

}<br />

Listing 20-1 shows a <strong>Spring</strong> Boot rabbitmq example that you can use. Take a look at the new annotation called<br />

@EnableRabbitMessaging that will enable the <strong>Spring</strong> AMQP module into your <strong>Spring</strong> Boot application, and the<br />

CommandLineRunner interface. This interface has the run method that you need to override; it will be the entry point<br />

for your <strong>Spring</strong> Boot application. You can run this <strong>Spring</strong> Boot application by executing<br />

spring run rabbitmqStream.groovy<br />

After executing the above command, the application will send 500 messages (of course, you can change<br />

this number to 1,000,0000 messages) and it will use a PDF as a routing key. The purpose of this example is just to<br />

experience the power of <strong>Spring</strong> XD. Don’t forget while you are running this example to take a look at<br />

http://localhost:9393/metrics/counters/pdfcount for the pdfcount value.<br />

286

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!