Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 13 ■ Adding E-mail and Scheduling Tasks true true Listing 13-6 shows that adding the tag will let the container know when a method call will be asynchronous because it will have the @Async annotation on it. Listing 13-7 contains the unit test; let’s see how long it will take to send the e-mail. Listing 13-7. MyDocumentsTest.java package com.apress.isf.spring.test; import static org.junit.Assert.assertNotNull; import java.util.Date; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.apress.isf.spring.email.EmailService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:META-INF/spring/mydocuments-context.xml") public class MyDocumentsTest { private static final Logger log = LoggerFactory.getLogger(MyDocumentsTest.class); @Autowired EmailService email; @Test @Ignore 188

Chapter 13 ■ Adding E-mail and Scheduling Tasks public void testAsyncEmail() throws InterruptedException{ log.debug("Testing Async Email..."); assertNotNull(email); long start = new Date().getTime(); email.sendAsync("user@gmail.com", "user@gmail.com", "New Document Add", "A new document was added to the collection."); } long end = new Date().getTime(); long time = (end - start)/1000; log.debug("Sending Async email done. Took: " + time + " seconds."); } Running the test with gradle :ch13:test you should have the following output: 2014-03-19 22:19:33,567 DEBUG [main] Testing Async Email... 2014-03-19 22:19:33,568 DEBUG [main] Sending Async email done. Took: 0 seconds. It returns immediately! Now you can take care of more business logic, and let those heavy tasks run in the background. Let’s Schedule Another feature you need to add to your Spring application is to periodically check out if any document’s location (on the hard drive or on the Web) is still valid. Because you have some web document types, you need to check this often for any broken resource links. The Spring Framework provides Schedulers classes that can help you with these tasks. Let’s create a simple example and see how easy it is to schedule a task every three seconds. Listing 13-8 shows the example. Listing 13-8. DocumentScheduler.java package com.apress.isf.spring.scheduler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class DocumentScheduler { private static Logger log = LoggerFactory.getLogger(DocumentScheduler.class); } @Scheduled(fixedRate=3000) public void sampleCronMethod(){ log.debug("Running every 3 seconds..."); } 189

Chapter 13 ■ Adding E-mail and Scheduling Tasks<br />

public void testAsyncEmail() throws InterruptedException{<br />

log.debug("Testing Async Email...");<br />

assertNotNull(email);<br />

long start = new Date().getTime();<br />

email.sendAsync("user@gmail.com", "user@gmail.com", "New Document Add",<br />

"A new document was added to the collection.");<br />

}<br />

long end = new Date().getTime();<br />

long time = (end - start)/1000;<br />

log.debug("Sending Async email done. Took: " + time + " seconds.");<br />

}<br />

Running the test with<br />

gradle :ch13:test<br />

you should have the following output:<br />

2014-03-19 22:19:33,567 DEBUG [main] Testing Async Email...<br />

2014-03-19 22:19:33,568 DEBUG [main] Sending Async email done. Took: 0 seconds.<br />

It returns immediately! Now you can take care of more business logic, and let those heavy tasks run in the<br />

background.<br />

Let’s Schedule<br />

Another feature you need to add to your <strong>Spring</strong> application is to periodically check out if any document’s location<br />

(on the hard drive or on the Web) is still valid. Because you have some web document types, you need to check this<br />

often for any broken resource links.<br />

The <strong>Spring</strong> <strong>Framework</strong> provides Schedulers classes that can help you with these tasks. Let’s create a simple<br />

example and see how easy it is to schedule a task every three seconds. Listing 13-8 shows the example.<br />

Listing 13-8. DocumentScheduler.java<br />

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

import org.slf4j.Logger;<br />

import org.slf4j.LoggerFactory;<br />

import org.springframework.scheduling.annotation.Scheduled;<br />

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

@Component<br />

public class DocumentScheduler {<br />

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

}<br />

@Scheduled(fixedRate=3000)<br />

public void sampleCronMethod(){<br />

log.debug("Running every 3 seconds...");<br />

}<br />

189

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

Saved successfully!

Ooh no, something went wrong!