25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11 ■ Integrating Your <strong>Spring</strong> Application with External Systems<br />

@RunWith(<strong>Spring</strong>JUnit4ClassRunner.class)<br />

@ContextConfiguration("classpath:META-INF/spring/mydocuments-context.xml")<br />

public class MyDocumentsTest {<br />

private static final Logger log = LoggerFactory.getLogger(MyDocumentsTest.class);<br />

//Based on the META-INF/data/jms.txt - only one record<br />

private static final int MAX_ALL_DOCS = 5;<br />

private static final int MAX_WEB_DOCS = 2;<br />

@Autowired<br />

private SearchEngine engine;<br />

@Test<br />

public void test<strong>Spring</strong>JMS() throws InterruptedException {<br />

log.debug("Testing <strong>Spring</strong> JMS Listener/Insert...");<br />

assertNotNull(engine);<br />

//Waiting a least 5 seconds so the message is consumed.<br />

Thread.sleep(5000);<br />

//After the JMS message and insert, must be 5 Documents<br />

assertEquals(MAX_ALL_DOCS, engine.listAll().size());<br />

}<br />

Type documentType = new Type("WEB",".url");<br />

assertEquals(MAX_WEB_DOCS, engine.findByType(documentType).size());<br />

}<br />

If you run this test (using gradle :ch11:test), it will work as long as you have an XML message sitting on the<br />

queue; in other words, if you run the test again, it will fail because there are no messages. Then you need to do the<br />

same as before: copy and paste the XML (Listing 11-1) and send it to the queue.<br />

Maybe you are wondering why this test will fail. Based on the data.sql file that is loaded as an embedded<br />

database in memory, you have only four records; after the consumer consumes the XML document and saves it<br />

into the database, you will have five documents and the assertion will fail (assertEquals(MAX_ALL_DOCS, engine.<br />

listAll().size()) because it is comparing the documents number in the database with the MAX_ALL_DOCS variable<br />

that is set to 5. If you decide to run this test over and over, it will become tedious to manually send a message into the<br />

ActiveMQ web page, right?<br />

The <strong>Spring</strong> <strong>Framework</strong> also provides a template, the org.springframework.jms.core.JmsTemplate class, to help<br />

you to send messages to any JMS broker so you can avoid copying and pasting messages manually into the ActiveMQ<br />

web page. So let’s create a producer that will send messages through the broker. Listing 11-7 shows the producer.<br />

Listing 11-7. JMSProducer.java<br />

package com.apress.isf.spring.jms;<br />

import java.io.IOException;<br />

import java.io.InputStream;<br />

import java.util.Scanner;<br />

import javax.jms.JMSException;<br />

import javax.jms.Message;<br />

import javax.jms.Session;<br />

155

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

Saved successfully!

Ooh no, something went wrong!