`

用Spring做的定时器

阅读更多
<bean id="sayHelloTask" class="com.aykj.c2c.utils.SayHelloTask"></bean>
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="sayHelloTask"/>
<!-- 任务执行周期 2m 关于一些任务的参数请参考JDK doc文档和Spring相关文档 -->
<property name="period">
<value>300000</value>
</property>
<!--  延时1m 执行任务  -->
<property name="delay">
<value>1000</value>
</property>
</bean>
<!-- 启动定时器  -->
<bean id="timerBean" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="scheduledTask"/>
</list>
</property>
</bean>



package com.aykj.c2c.utils;

import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.aykj.c2c.pojo.AykjUser;

public class SayHelloTask extends TimerTask {
	Configuration cfg = new Configuration().configure("com/aykj/command/hibernate.cfg.xml");
	SessionFactory factory = cfg.buildSessionFactory();
	Session session = factory.openSession();
	String hql = "from AykjUser";
	String hqls = "from AykjShop";
	AykjUser aykjUser = null;
	Query query = null;
	int a = 0;
	public void run() {
		query = session.createQuery(hql);
		query.list().iterator();
		query = session.createQuery(hql);
		query.list().iterator();
		System.out.println(++a);		
		System.gc();
	}	
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics