易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Java Web项目中定时器的简单运用
Java Web项目中定时器的简单运用:
import
java.io.FileNotFoundException;
import
java.io.IOException;
import
java.util.Date;
import
java.util.Timer;
import
javax.servlet.ServletConfig;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
org.apache.log4j.PropertyConfigurator;
public
class
InitServlet
extends
HttpServlet {
private
static
final
long
serialVersionUID = -2693899729090015551L;
public
static
final
String FILE_SEPARATOR = System.getProperties().getProperty(
"file.separator"
);
private
static
String contextPath;
private
static
String serverConfig;
private
static
String ftpPath;
// 添加定时器任务
private
Timer timer;
/**
* @Override
* @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig) <BR>
* Method name: init <BR>
* Description: please write your description <BR>
* Remark: <BR>
* @param config
* @throws ServletException <BR>
*/
@SuppressWarnings
(
"deprecation"
)
@Override
public
void
init(ServletConfig config)
throws
ServletException {
super
.init(config);
String prefix = config.getServletContext().getRealPath(
"/"
);
InitServlet.contextPath = prefix;
if
(FILE_SEPARATOR.equals(
"\"
)) {
// 获取内容服务器配置文件的路径
serverConfig = prefix +
"\WEB-INF\config.properties"
;
}
else
if
(FILE_SEPARATOR.equals(
"/"
)) {
serverConfig = prefix +
"/WEB-INF/config.properties"
;
}
// 处理的是ftp文件位置,具体到哪个文件在Action中处理
Property property =
new
Property(InitServlet.getServerConfig());
// 读取path信息
String path =
null
;
try
{
path = property.getProperty(
"path"
);
if
(FILE_SEPARATOR.equals(
"/"
)) {
// 在Linux环境中判断是否以"/"结尾
if
(!(
"/"
.equalsIgnoreCase(path.substring(path.length()-
1
, path.length())))) {
path = path +
"/"
;
}
}
else
if
(FILE_SEPARATOR.equals(
"\"
)) {
// 在Windows环境下判断是否以"\"结尾
if
(!(
"\"
.equalsIgnoreCase(path.substring(path.length()-
1
, path.length())))) {
path = path +
"\"
;
}
}
}
catch
(FileNotFoundException e1) {
e1.printStackTrace();
}
catch
(IOException e1) {
e1.printStackTrace();
}
InitServlet.ftpPath = path;
// 创建定时器
timer =
new
Timer();
// 启动定时器任务
Date date =
new
Date();
// 设定每天凌晨2点开始
date.setHours(CommonStateDefine.HOUR);
timer.schedule(
new
DeleteRetransFolderTicker(),
new
Date(date.getTime()
+
1
*
24
*
60
*
60
*
1000
),
1
*
24
*
60
*
60
*
1000
);
}
public
static
final
String getContextPath() {
return
contextPath;
}
public
static
final
String getServerConfig() {
return
serverConfig;
}
public
static
String getFtpPath() {
return
ftpPath;
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图