Skip to content

Instantly share code, notes, and snippets.

@benelog
Created September 13, 2012 07:40
Show Gist options
  • Save benelog/3712643 to your computer and use it in GitHub Desktop.
Save benelog/3712643 to your computer and use it in GitHub Desktop.
Spring scheduler + Spel
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="every5Secs">0/5 * * * * ?</entry>
<entry key="every8Secs">0/8 * * * * ?</entry>
</properties>
package net.benelog.schedule;
import org.springframework.stereotype.Component;
@Component
public class SpelSchedule {
public void hello1() {
System.out.println("hello1!!!");
}
public void hello2() {
System.out.println("hello222!!"); }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="net.benelog.schedule"/>
<util:properties id="schedules" location="classpath:/schedules.xml" />
<task:scheduler id="myScheduler" pool-size="10"/>
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="spelSchedule" method="hello1" cron="#{schedules.every5Secs}" />
<task:scheduled ref="spelSchedule" method="hello2" cron="#{schedules.every8Secs}" />
</task:scheduled-tasks>
<!-- http://forum.springsource.org/showthread.php?125975-Scheduled(cron)-does-not-support-SpEL -->
</beans>
package net.benelog.schedule;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpelScheduleTest {
@Test
public void test() throws InterruptedException {
TimeUnit.MINUTES.sleep(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment