Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 229 and 230: Chapter 17 ■ Be Social and Go Mob
- Page 231 and 232: Chapter 17 ■ Be Social and Go Mob
- Page 233 and 234: Chapter 17 ■ Be Social and Go Mob
- Page 235 and 236: Chapter 17 ■ Be Social and Go Mob
- Page 237 and 238: Chapter 17 ■ Be Social and Go Mob
- Page 239 and 240: Chapter 17 ■ Be Social and Go Mob
- Page 241 and 242: Chapter 17 ■ Be Social and Go Mob
- Page 243 and 244: Chapter 17 ■ Be Social and Go Mob
- Page 245 and 246: Chapter 17 ■ Be Social and Go Mob
- Page 247 and 248: Part IV Spring I/O Spring I/O is no
- Page 249 and 250: Chapter 18 ■ Spring and Groovy
- Page 251 and 252: Chapter 18 ■ Spring and Groovy Li
- Page 253 and 254: Chapter 18 ■ Spring and Groovy cl
- Page 255 and 256: Chapter 18 ■ Spring and Groovy If
- Page 257 and 258: Chapter 18 ■ Spring and Groovy Li
- Page 259 and 260: Chapter 19 ■ Spring Boot, Simplif
- Page 261 and 262: Chapter 19 ■ Spring Boot, Simplif
- Page 263 and 264: Chapter 19 ■ Spring Boot, Simplif
- Page 265 and 266: Chapter 19 ■ Spring Boot, Simplif
- Page 267 and 268: Chapter 19 ■ Spring Boot, Simplif
- Page 269 and 270: Chapter 19 ■ Spring Boot, Simplif
- Page 271 and 272: Chapter 19 ■ Spring Boot, Simplif
- Page 273 and 274: Chapter 20 ■ Your First Spring XD
- Page 275 and 276: Chapter 20 ■ Your First Spring XD
- Page 277 and 278: Chapter 20 ■ Your First Spring XD
- Page 279: Chapter 20 ■ Your First Spring XD
- Page 283 and 284: Appendix A Installing Tools In this
- Page 285 and 286: Appendix A ■ Installing Tools Fig
- Page 287 and 288: Setting Environment Variables Appen
- Page 289 and 290: Appendix A ■ Installing Tools Fig
- Page 291 and 292: Appendix A ■ Installing Tools 6.
- Page 293 and 294: Appendix A ■ Installing Tools Fig
- Page 295 and 296: Appendix A ■ Installing Tools ■
- Page 297 and 298: Appendix A ■ Installing Tools Fig
- Page 299 and 300: Appendix A ■ Installing Tools Fig
- Page 301 and 302: Appendix A ■ Installing Tools Fig
- Page 303 and 304: Appendix A ■ Installing Tools Fig
- Page 305 and 306: Stop MongoDB Server Appendix A ■
- Page 307 and 308: Stop ActiveMQ Appendix A ■ Instal
- Page 309 and 310: Installing RabbitMQ on Windows Appe
- Page 311 and 312: Appendix A ■ Installing Tools Fig
- Page 313 and 314: Installing Spring XD on Windows App
- Page 315 and 316: ■ index • I I/O operations, 62
- Page 317 and 318: ■ index Spring application (cont.
- Page 319 and 320: Introducing Spring Framework A Prim
- Page 321 and 322: To my parents, Rocio Cruz and Felip
- Page 323 and 324: ■ Contents ■■Chapter 3: Apply
- Page 325 and 326: ■ Contents ■■Chapter 12: Expo
- Page 327 and 328: ■ Contents ■■Chapter 20: Your
- Page 329 and 330: About the Author Felipe Gutierrez i
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