Sprint之java.util.Time

编程入门 行业动态 更新时间:2024-10-25 00:32:58

Sprint之<a href=https://www.elefans.com/category/jswz/34/1770091.html style=java.util.Time"/>

Sprint之java.util.Time

参考资料
1 在Spring中使用JDK Timer进行任务调度
[url].shtml[/url]
小结
JDK Timer可以满足一些简单的任务调度需求,使用JDK Timer的任务对执行时间点应该没有严格的要求,因为JDK Timer只能做到近似的时间安排.
[b]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。[/b]
[color=green]在Spring之Email<封装了常用的四种发送Email的方法(TEXT,HTML,IMG,FILE)> 基础之上加的任务调度^_^[/color]
一 工程图片
[img].jpg[/img]
二 具体代码
1 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi=""
xmlns:aop=""
xsi:schemaLocation="
.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();
}
}

更多推荐

Sprint之java.util.Time

本文发布于:2024-02-27 00:05:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1704495.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:java   Sprint   Time   util

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!