Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 18 ■ Spring and Groovy rabbit.'listener-container'(id:"pdfListener", 'connection-factory':"rabbitConnectionFactory"){ rabbit.listener(ref:"pdfAdapter", queues:"docs-pdf") rabbit.listener(ref:"textAdapter", queues:"docs-txt") rabbit.listener(ref:"webAdapter", queues:"docs-web") } rabbit.queue(name:"docs-pdf") rabbit.queue(name:"docs-txt") rabbit.queue(name:"docs-web") } pdfAdapter(MLA,ref("pdfConsumer"),ref("messageConverter")){ defaultListenerMethod = "process" } textAdapter(MLA,ref("textConsumer"),ref("messageConverter")){ defaultListenerMethod = "process" } webAdapter(MLA,ref("webConsumer"),ref("messageConverter")){ defaultListenerMethod = "process" } Listing 18-6 shows the RabbitMQ bean definitions and the use of the xmlns rabbit namespace. Remember that you need to use your OXM Marshaller to convert the message. This is needed in the producers and consumers, so it shows the importBeans to import the OXM script. Next, it shows all the definitions needed to set up the RabbitMQ broker (rabbit.'connection-factory', rabbit.admin), producer (rabbit.template), consumer (rabbit.'listener-container'), queue (rabbit.queue), exchange (rabbit.'direct-exchange'), and binding (rabbit.binding). Let’s Test Groovy You are going to start testing the new Spring DSL and see the difference between using the XML or using the Groovy way. Listing 18-7 shows the unit test written using Groovy. Listing 18-7. MyDocumentsTest.groovy package com.apress.isf.spring.test import static org.junit.Assert.assertNotNull import static org.junit.Assert.assertEquals import org.junit.Test import org.junit.Ignore import org.slf4j.Logger import org.slf4j.LoggerFactory import org.springframework.context.support.GenericGroovyApplicationContext import com.apress.isf.java.model.Document; 257
Chapter 18 ■ Spring and Groovy class MyDocumentsTest { private static final Logger log = LoggerFactory.getLogger(MyDocumentsTest.class) private final RECORDS = 6 private final ID = "1acbb68a-a859-49c9-ac88-d9e9322bac55" private final ctx = new GenericGroovyApplicationContext("classpath:META-INF/groovy/ mydocuments.groovy") @Test @Ignore void testJDBC(){ log.info "Testing JDBC..." assertNotNull ctx def documentFacade = ctx.getBean("documentFacade") assertNotNull documentFacade int size = documentFacade.allDocuments.size() assertEquals RECORDS,size } assertNotNull documentFacade.findDocumentById(ID) @Test public void testProducer(){ log.debug("Testing RabbitMQ Producer..."); def documentFacade = ctx.getBean("documentFacade") def rabbitmqProducer = ctx.getBean("rabbitmqProducer") assertNotNull(rabbitmqProducer) assertNotNull(documentFacade) } } documentFacade.allDocuments.each{ rabbitmqProducer.send it } sleep 5000 Listing 18-7 shows the unit test. In this case, you wrote the unit test using Groovy, but you can use Java and still make the use of the Groovy scripts. So, what is the difference between this unit test versus the ones you did in previous chapters? The first big difference is that all your bean definitions are using the Spring DSL, and the second is that you are using the GenericGroovyApplicationContext class (that will load the groovy DSL files) instead of the ClassPathXmlApplicationContext class that loads only XML files. Run this test with gradle :ch18:test You will have the same output as in Chapter 16 because you are using the same bean descriptions and doing RabbitMQ messaging (don’t forget to have the RabbitMQ up and running). Take a moment and do a last review of the unit test and the Spring DSL. You will find that you got rid of all the XML verbosity and simplified the bean definitions, thanks to the Groovy programing language. 258
- Page 201 and 202: Chapter 15 ■ Spring Data Within Y
- Page 203 and 204: Chapter 15 ■ Spring Data Within Y
- Page 205 and 206: Chapter 15 ■ Spring Data Within Y
- Page 207 and 208: Chapter 15 ■ Spring Data Within Y
- Page 209 and 210: Chapter 15 ■ Spring Data Within Y
- Page 211 and 212: Chapter 15 ■ Spring Data Within Y
- Page 213 and 214: Chapter 15 ■ Spring Data Within Y
- Page 215 and 216: Chapter 16 ■ Messaging with Your
- Page 217 and 218: Chapter 16 ■ Messaging with Your
- Page 219 and 220: Chapter 16 ■ Messaging with Your
- Page 221 and 222: Chapter 16 ■ Messaging with Your
- Page 223 and 224: Chapter 16 ■ Messaging with Your
- Page 225 and 226: Chapter 16 ■ Messaging with Your
- Page 227 and 228: Chapter 17 Be Social and Go Mobile
- 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: Chapter 18 ■ Spring and Groovy Li
- 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 and 280: Chapter 20 ■ Your First Spring XD
- Page 281 and 282: 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
Chapter 18 ■ <strong>Spring</strong> and Groovy<br />
rabbit.'listener-container'(id:"pdfListener", 'connection-factory':"rabbitConnectionFactory"){<br />
rabbit.listener(ref:"pdfAdapter", queues:"docs-pdf")<br />
rabbit.listener(ref:"textAdapter", queues:"docs-txt")<br />
rabbit.listener(ref:"webAdapter", queues:"docs-web")<br />
}<br />
rabbit.queue(name:"docs-pdf")<br />
rabbit.queue(name:"docs-txt")<br />
rabbit.queue(name:"docs-web")<br />
}<br />
pdfAdapter(MLA,ref("pdfConsumer"),ref("messageConverter")){<br />
defaultListenerMethod = "process"<br />
}<br />
textAdapter(MLA,ref("textConsumer"),ref("messageConverter")){<br />
defaultListenerMethod = "process"<br />
}<br />
webAdapter(MLA,ref("webConsumer"),ref("messageConverter")){<br />
defaultListenerMethod = "process"<br />
}<br />
Listing 18-6 shows the RabbitMQ bean definitions and the use of the xmlns rabbit namespace. Remember<br />
that you need to use your OXM Marshaller to convert the message. This is needed in the producers and consumers,<br />
so it shows the importBeans to import the OXM script. Next, it shows all the definitions needed to set up the<br />
RabbitMQ broker (rabbit.'connection-factory', rabbit.admin), producer (rabbit.template), consumer<br />
(rabbit.'listener-container'), queue (rabbit.queue), exchange (rabbit.'direct-exchange'), and binding<br />
(rabbit.binding).<br />
Let’s Test Groovy<br />
You are going to start testing the new <strong>Spring</strong> DSL and see the difference between using the XML or using the Groovy<br />
way. Listing 18-7 shows the unit test written using Groovy.<br />
Listing 18-7. MyDocumentsTest.groovy<br />
package com.apress.isf.spring.test<br />
import static org.junit.Assert.assertNotNull<br />
import static org.junit.Assert.assertEquals<br />
import org.junit.Test<br />
import org.junit.Ignore<br />
import org.slf4j.Logger<br />
import org.slf4j.LoggerFactory<br />
import org.springframework.context.support.GenericGroovyApplicationContext<br />
import com.apress.isf.java.model.Document;<br />
257