import
java.util.List;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
cn.com.vogue.entity.UrlTaskBase;
import
cn.com.vogue.service.systemManagement.UrlTaskBaseService;
import
cn.com.vogue.utils.HttpUtils;
import
cn.com.vogue.utils.SpringFactory;
public
class
UrlTask
implements
Runnable{
private
static
Logger logger = LoggerFactory.getLogger(UrlTask.
class
);
public
static
Integer isRun =
new
Integer(
0
);
public
void
run() {
while
(
true
){
try
{
UrlTaskBaseService taskSer = (UrlTaskBaseService)SpringFactory.getObject(
"urlTaskBaseService"
);
List<UrlTaskBase> taskList = taskSer.findUrlTaskBaseUnSucc();
if
(
null
!= taskList && taskList.size() >
0
){
logger.warn(
"本次需要执行的任务个数:"
+ taskList.size() +
" 个"
);
for
(UrlTaskBase task : taskList){
Init1ListenerRun run =
new
Init1ListenerRun(task, taskSer);
SpringFactory.executorService.execute(run);
try
{Thread.sleep(
1000
);}
catch
(Exception e) {}
}
}
else
{
logger.warn(
"没有找到要执行的任务,休眠后继续执行"
);
}
}
catch
(Exception e) {
logger.error(
"查询执行任务时异常"
, e);
}
while
(isRun >
0
){
try
{Thread.sleep(
1
*
1000
);}
catch
(Exception e) {}
}
logger.error(
"60秒后进行下一次任务轮询"
);
try
{Thread.sleep(
60
*
1000
);}
catch
(Exception e) {}
}
}
}
class
Init1ListenerRun
implements
Runnable {
private
static
Logger logger = LoggerFactory.getLogger(Init1ListenerRun.
class
);
public
void
run() {
synchronized
(UrlTask.isRun) { UrlTask.isRun ++; }
try
{
logger.warn(task.getId() +
" 任务路径:"
+ task.getTaskUrl());
if
(task.getTaskType() ==
0
){
String result = HttpUtils.HttpGet(task.getTaskUrl());
logger.warn(task.getId() +
" 返回结果:"
+ result);
if
(
null
!= result && !
""
.equals(result)){
task.setBackStr(result);
}
task.setTryCount(task.getTryCount() +
1
);
taskSer.save(task);
}
else
{
String result = HttpUtils.HttpPost(task.getTaskUrl(), task.getTaskBody());
logger.warn(task.getId() +
" 返回结果:"
+ result);
if
(
null
!= result && !
""
.equals(result)){
task.setBackStr(result);
}
task.setTryCount(task.getTryCount() +
1
);
taskSer.save(task);
}
}
catch
(Exception e) {
logger.error(
"执行任务异常"
, e);
}
synchronized
(UrlTask.isRun) { UrlTask.isRun --; }
}
private
UrlTaskBase task;
private
UrlTaskBaseService taskSer;
public
Init1ListenerRun(UrlTaskBase task, UrlTaskBaseService taskSer){
this
.task = task;
this
.taskSer = taskSer;
}
}