25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

public void save(Document document) {<br />

try {<br />

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

if (null == findById(document.getDocumentId()))<br />

template.update(<br />

insert,<br />

new Object[] { document.getDocumentId(),<br />

document.getName(), document.getLocation(),<br />

document.getDescription(),<br />

document.getType().getTypeId(),<br />

document.getCreated(), document.getModified() });<br />

else<br />

template.update(<br />

update,<br />

new Object[] { document.getName(),<br />

document.getLocation(),<br />

document.getDescription(),<br />

document.getType().getTypeId(), new Date(),<br />

document.getDocumentId() });<br />

} catch (Exception ex) {<br />

ex.printStackTrace();<br />

}<br />

}<br />

}<br />

You have a more complete DocumentDAO implementation, right? See Listing 11-4, where you are saving, updating,<br />

and finding documents by id. Also notice that the save method will be used to save a new document or update an<br />

existing one; this is because you are checking to see if that document exists first by calling the findById method.<br />

So how will the JMSConsumer class (see Listing 11-2) be called or used to consume the message from the Apache<br />

ActiveMQ? Well, the <strong>Spring</strong> <strong>Framework</strong> will take care of this, but you need to help it a little bit. See Listing 11-5, the<br />

XML configuration.<br />

Listing 11-5. mydocuments-context.xml<br />

<br />

<br />

<br />

153

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

Saved successfully!

Ooh no, something went wrong!