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 13 ■ Adding E-mail and Scheduling Tasks<br />

Listing 13-10 shows the urlCheck method that will run every 10 seconds. This method will get all the documents<br />

that are a web type; for every document found, it will use its location to send a request and get a response. If the<br />

response is good, it will log “URL is still valid”; if not, it will log “URL INVALID”. It uses the java.net.URL class to send<br />

the request to the URL (the document’s location).<br />

For testing purposes, you are testing the web types; you can modify the code in order to verify that the location<br />

of a note, PDF, or any other type of document is still valid. Also, there are very good resources on the cron-tab<br />

expressions on the Web if you want to learn more.<br />

You can use the same code in Listing 13-9 (MyDocumentTest.java) and run it with<br />

gradle :ch13:test<br />

and see results like the following:<br />

2014-03-19 22:30:50,005 DEBUG [pool-1-thread-1] @@ Checking valid WEB type Document's URL...<br />

2014-03-19 22:30:50,937 DEBUG [pool-1-thread-1] Name lookup: Pro <strong>Spring</strong> Security<br />

2014-03-19 22:30:50,937 DEBUG [pool-1-thread-1] Code: 200<br />

2014-03-19 22:30:50,937 INFO [pool-1-thread-1] URL is still valid!!<br />

2014-03-19 22:30:51,717 DEBUG [pool-1-thread-1] Name lookup: Bad Book<br />

2014-03-19 22:30:51,717 DEBUG [pool-1-thread-1] Code: 404<br />

2014-03-19 22:30:51,717 ERROR [pool-1-thread-1] URL INVALID! Code: 404. Please let know the<br />

AdministratorL<br />

2014-03-19 22:30:51,935 DEBUG [pool-1-thread-1] Name lookup: Pro <strong>Spring</strong> 3<br />

2014-03-19 22:30:51,935 DEBUG [pool-1-thread-1] Code: 200<br />

2014-03-19 22:30:51,935 INFO [pool-1-thread-1] URL is still valid!!<br />

Using the book’s companion source code, you can find the data.sql. There are some new records that contain<br />

bad data. Those records are being checked by the document scheduler every 10 seconds.<br />

Summary<br />

In this chapter, you saw that sending e-mail is easy with the <strong>Spring</strong> <strong>Framework</strong> e-mail’s helper classes. Also, you saw<br />

that the @Async annotation helps to call a method asynchronously.<br />

You added a new feature to the <strong>Spring</strong> application that can validate the Location field and report back if it is still<br />

valid or not. As you can see, the <strong>Spring</strong> <strong>Framework</strong> offers very cool features for every task.<br />

In the next chapter, you’ll start using other languages besides Java, and you’ll see how they interact with the<br />

<strong>Spring</strong> container.<br />

192

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

Saved successfully!

Ooh no, something went wrong!