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

Listing 13-8 shows the @Scheduled annotation that takes a parameter called fixedRate, which is a long value in<br />

milliseconds. This will make the method sampleCronMethod run every three seconds. To test this class, let’s create a<br />

small Test method in your unit test class. See Listing 13-9.<br />

Listing 13-9. MyDocumentsTest.java<br />

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

import org.junit.Test;<br />

import org.junit.runner.RunWith;<br />

import org.slf4j.Logger;<br />

import org.slf4j.LoggerFactory;<br />

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

import org.springframework.test.context.ContextConfiguration;<br />

import org.springframework.test.context.junit4.<strong>Spring</strong>JUnit4ClassRunner;<br />

@RunWith(<strong>Spring</strong>JUnit4ClassRunner.class)<br />

@ContextConfiguration("classpath:META-INF/spring/mydocuments-context.xml")<br />

public class MyDocumentsTest {<br />

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

}<br />

@Test<br />

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

Thread.sleep(45000);<br />

}<br />

The output of running Listing 13-9 should be the following:<br />

2014-03-19 22:06:33,030 DEBUG [pool-1-thread-1] Running every 3 seconds...<br />

2014-03-19 22:06:36,000 DEBUG [pool-1-thread-1] Running every 3 seconds...<br />

2014-03-19 22:06:39,001 DEBUG [pool-1-thread-1] Running every 3 seconds...<br />

2014-03-19 22:06:42,000 DEBUG [pool-1-thread-1] Running every 3 seconds...<br />

2014-03-19 22:06:45,001 DEBUG [pool-1-thread-1] Running every 3 seconds...<br />

2014-03-19 22:06:48,000 DEBUG [pool-1-thread-1] Running every 3 seconds...<br />

2014-03-19 22:06:51,000 DEBUG [pool-1-thread-1] Running every 3 seconds...<br />

2014-03-19 22:06:54,000 DEBUG [pool-1-thread-1] Running every 3 seconds...<br />

2014-03-19 22:06:57,001 DEBUG [pool-1-thread-1] Running every 3 seconds...<br />

Now you know what to do for scheduling the logic to validate the document’s location, right? Yes! You use the<br />

@Scheduled annotation in a method that runs every 2 hours perhaps to validate the location. But what happens if you<br />

need more granularity? In other words, what happens if you need to schedule this validation logic every Monday,<br />

every two months? Thanks to the <strong>Spring</strong> scheduling classes, the @Scheduled also can accept "cron-tab" expressions.<br />

(The cron is a tool used in Unix to execute commands periodically at specified date/time intervals). Listing 13-10<br />

shows the final DocumentScheduler class.<br />

190

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

Saved successfully!

Ooh no, something went wrong!