Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!