Spring Task Example

pom.xml

[code]

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.tugrulaslan</groupId>
<artifactId>SpringScheduledTaskExample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>SpringScheduledTaskExample</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.2.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

[/code]

batch.xml in src>java>resources

[code]

<?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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

<context:component-scan base-package="com.tugrulaslan.scheduling"/>
<task:annotation-driven/>
<bean id="taskBean" class="com.tugrulaslan.scheduling.TaskBean"/>
<task:scheduled-tasks>
<!– for every second: 0/1 * * * * ? –>
<!– for every minute: 0 0/1 * * * ? –>
<!– for every hour: 0 0 0/1 * * ? –>
<task:scheduled ref="taskBean" method="runTask" cron="0 0/1 * * * ?"/>
</task:scheduled-tasks>

</beans>

[/code]

TaskBean.java in com.tugrulaslan.scheduling

[code]

package com.tugrulaslan.scheduling;

public class TaskBean {

public void runTask(){
System.out.println("running the task: " + new java.util.Date());
}
}

[/code]

App.java in com.tugrulaslan

[code]

package com.tugrulaslan;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("batch.xml");
}
}

[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *