Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 135 and 136: Chapter 10 ■ Showing Your Spring
- Page 137 and 138: Chapter 10 ■ Showing Your Spring
- Page 139 and 140: Chapter 10 ■ Showing Your Spring
- Page 141 and 142: Chapter 10 ■ Showing Your Spring
- Page 143 and 144: Chapter 10 ■ Showing Your Spring
- Page 145 and 146: Chapter 11 ■ Integrating Your Spr
- Page 147 and 148: Chapter 11 ■ Integrating Your Spr
- Page 149 and 150: Chapter 11 ■ Integrating Your Spr
- Page 151 and 152: Chapter 11 ■ Integrating Your Spr
- Page 153 and 154: Chapter 11 ■ Integrating Your Spr
- Page 155 and 156: Chapter 11 ■ Integrating Your Spr
- Page 157 and 158: Chapter 11 ■ Integrating Your Spr
- Page 159 and 160: Chapter 11 ■ Integrating Your Spr
- Page 161 and 162: Chapter 11 ■ Integrating Your Spr
- Page 163 and 164: Chapter 11 ■ Integrating Your Spr
- Page 165 and 166: Chapter 11 ■ Integrating Your Spr
- Page 167 and 168: Chapter 12 Exposing a REST API Nowa
- Page 169 and 170: Chapter 12 ■ Exposing a REST API
- Page 171 and 172: Chapter 12 ■ Exposing a REST API
- Page 173 and 174: Chapter 12 ■ Exposing a REST API
- Page 175 and 176: Chapter 12 ■ Exposing a REST API
- Page 177 and 178: Chapter 12 ■ Exposing a REST API
- Page 179 and 180: HTTP-DELETE Chapter 12 ■ Exposing
- Page 181 and 182: Chapter 13 Adding E-mail and Schedu
- Page 183 and 184: Chapter 13 ■ Adding E-mail and Sc
- Page 185: Chapter 13 ■ Adding E-mail and Sc
- Page 189 and 190: Chapter 13 ■ Adding E-mail and Sc
- Page 191 and 192: Part III Spring Framework Advanced
- Page 193 and 194: Chapter 14 ■ Using Dynamic Langua
- Page 195 and 196: Chapter 14 ■ Using Dynamic Langua
- Page 197 and 198: Chapter 14 ■ Using Dynamic Langua
- Page 199 and 200: Chapter 14 ■ Using Dynamic Langua
- Page 201 and 202: Chapter 15 ■ Spring Data Within Y
- Page 203 and 204: Chapter 15 ■ Spring Data Within Y
- Page 205 and 206: Chapter 15 ■ Spring Data Within Y
- Page 207 and 208: Chapter 15 ■ Spring Data Within Y
- Page 209 and 210: Chapter 15 ■ Spring Data Within Y
- Page 211 and 212: Chapter 15 ■ Spring Data Within Y
- Page 213 and 214: Chapter 15 ■ Spring Data Within Y
- Page 215 and 216: Chapter 16 ■ Messaging with Your
- Page 217 and 218: Chapter 16 ■ Messaging with Your
- Page 219 and 220: Chapter 16 ■ Messaging with Your
- Page 221 and 222: Chapter 16 ■ Messaging with Your
- Page 223 and 224: Chapter 16 ■ Messaging with Your
- Page 225 and 226: Chapter 16 ■ Messaging with Your
- Page 227 and 228: Chapter 17 Be Social and Go Mobile
- Page 229 and 230: Chapter 17 ■ Be Social and Go Mob
- Page 231 and 232: Chapter 17 ■ Be Social and Go Mob
- Page 233 and 234: Chapter 17 ■ Be Social and Go Mob
- Page 235 and 236: Chapter 17 ■ Be Social and Go Mob
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