Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 11 ■ Integrating Your Spring Application with External Systems import com.apress.isf.java.model.Document; import com.apress.isf.java.utils.XmlUtils; import com.apress.isf.spring.data.DocumentDAO; @Component public class JMSConsumer implements MessageListener{ } @Autowired DocumentDAO documentDAO; @Override public void onMessage(Message message) { TextMessage textMessage = (TextMessage)message; try{ Document document = XmlUtils.fromXML(textMessage.getText(), Document.class); documentDAO.save(document); }catch(JMSException ex){ ex.printStackTrace(); } } Let’s analyze what you are doing in Listing 11-2. First, you are implementing the javax.jms.MessageListener. The JMS API exposes this interface where you need to implement the onMessage method; this method accepts a javax.jms.Message that will contain the message. Also, you are using a small Utility class, which will help to convert the XML into a document object and be saved into the database using the DocumentDAO implementation. Next, take a look at Listing 11-3, which shows the Utility class that is used in the consumer. Listing 11-3. XmlUtils.java package com.apress.isf.java.utils; import com.thoughtworks.xstream.XStream; public class XmlUtils { public static String toXML(T object){ XStream xstream = new XStream(); xstream.alias(object.getClass().getSimpleName().toLowerCase(), object.getClass()); return xstream.toXML(object); } } @SuppressWarnings({ "unchecked"}) public static T fromXML(String xml, Class _class){ XStream xstream = new XStream(); xstream.alias(_class.getSimpleName().toLowerCase(), _class); return (T)xstream.fromXML(xml); } 151

Chapter 11 ■ Integrating Your Spring Application with External Systems Listing 11-3 uses XStream, a library that serializes/deserializes objects into an XML String back and forth. Because you need to save an actual document object, it is necessary to add this helper to the application. Note in Listing 11-2 that there is a documentDAO.save(document)call. This is a new method that you will add to the DocumentDAO implementation that will save a Document object. See Listing 11-4, the DocumentRepository class. Listing 11-4. DocumentRepository.java package com.apress.isf.spring.data; import java.util.Date; import java.util.List; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import com.apress.isf.java.model.Document; @Repository("documentDAO") public class DocumentRepository implements DocumentDAO { @Autowired private DataSource dataSource; @Autowired private String query; @Autowired private String insert; @Autowired private String find; @Autowired private String update; public List getAll() { return new JdbcTemplate(dataSource).query(query, new DocumentRowMapper()); } public Document findById(String id) { Document updateDocument = null; JdbcTemplate template = new JdbcTemplate(dataSource); } try { updateDocument = template.queryForObject(find, new Object[] { id }, new DocumentRowMapper()); } catch (EmptyResultDataAccessException ex) {} return updateDocument; 152

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

Listing 11-3 uses XStream, a library that serializes/deserializes objects into an XML String back and forth.<br />

Because you need to save an actual document object, it is necessary to add this helper to the application. Note<br />

in Listing 11-2 that there is a documentDAO.save(document)call. This is a new method that you will add to the<br />

DocumentDAO implementation that will save a Document object. See Listing 11-4, the DocumentRepository class.<br />

Listing 11-4. DocumentRepository.java<br />

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

import java.util.Date;<br />

import java.util.List;<br />

import javax.sql.DataSource;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.dao.EmptyResultDataAccessException;<br />

import org.springframework.jdbc.core.JdbcTemplate;<br />

import org.springframework.stereotype.Repository;<br />

import com.apress.isf.java.model.Document;<br />

@Repository("documentDAO")<br />

public class DocumentRepository implements DocumentDAO {<br />

@Autowired<br />

private DataSource dataSource;<br />

@Autowired<br />

private String query;<br />

@Autowired<br />

private String insert;<br />

@Autowired<br />

private String find;<br />

@Autowired<br />

private String update;<br />

public List getAll() {<br />

return new JdbcTemplate(dataSource).query(query,<br />

new DocumentRowMapper());<br />

}<br />

public Document findById(String id) {<br />

Document updateDocument = null;<br />

JdbcTemplate template = new JdbcTemplate(dataSource);<br />

}<br />

try {<br />

updateDocument = template.queryForObject(find,<br />

new Object[] { id },<br />

new DocumentRowMapper());<br />

} catch (EmptyResultDataAccessException ex) {}<br />

return updateDocument;<br />

152

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

Saved successfully!

Ooh no, something went wrong!