`

Sprint之java.util.Time

 
阅读更多
参考资料
1 在Spring中使用JDK Timer进行任务调度
http://www.builder.com.cn/2007/0529/404717.shtml
小结
JDK Timer可以满足一些简单的任务调度需求,使用JDK Timer的任务对执行时间点应该没有严格的要求,因为JDK Timer只能做到近似的时间安排.
Spring在org.springframework.scheduling.timer中提供了几个JDK Timer的支持类,主要在以下三方面对JDK Timer提供了支持:
1) ScheduledTimerTask,它对TimerTask提供封装并或配置调度信息;
2)通过MethodInvokingTimerTaskFactoryBean类可以将一个Bean的方法封装为TimerTask;
3)通过TimerFactoryBean可以更方便地配置Timer,此外让Timer的生命周期和Spring容器的生命周期相关,在初始化TimerFactoryBean后,启动Timer,在Spring容器关闭前取消Timer。

在Spring之Email<封装了常用的四种发送Email的方法(TEXT,HTML,IMG,FILE)> 基础之上加的任务调度^_^
一 工程图片

二 具体代码
1 applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
										
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:mail.properties</value>
		</property>
	</bean>

	<bean id="sendMessageTime" class="test.time.SendMessageTime">
		<property name="subject">
			<value>${mail.subject}</value>
		</property>
		<property name="text">
			<value>${mail.text}</value>
		</property>
	</bean>

	<bean id="scheduledHelloTask"
		class="org.springframework.scheduling.timer.ScheduledTimerTask">
		<property name="timerTask">
			<ref bean="sendMessageTime" />
		</property>
		
		<property name="period">
			<value>2000</value>
		</property>
		 <property name="delay">  
	        <value>1000</value>  
	    </property>  
	</bean>

	<bean
		class="org.springframework.scheduling.timer.TimerFactoryBean">
		<property name="scheduledTimerTasks">
			<list>
				<ref bean="scheduledHelloTask" />
			</list>
		</property>		
	</bean>	

</beans>

2 任务类:SendMessageTime.java
public class SendMessageTime extends TimerTask{	
	//消息计数器
	private static int messageCount = 0;
	//发送标题
	private String subject;
	private String text;
	private MailMessageFactory mms = new MailMessageFactory();

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	@Override
	public void run() {		
		System.out.println("开始发送消息...");		
		mms.setSubject(getSubject())
		.setText(getText())
		.send();
		System.out.println("发送消息条数: "+(++messageCount));
	}
}

3 启动类
public class SpringTimeTest {

	
	public static void main(String[] args) {
		
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");	
		
		//休眠30秒后结束进程
		try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {           
            e.printStackTrace();
        }    
        context.close();		
	}
}
  • 大小: 46.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics