前端设置定时器请求查询线程表中的数据;
后台设置线程更新线程表中的数据;
/**
* @return
*/
@NeedAdmin
@Get("autoarrangeexamroom")
public String autoarrangeexamroom() {
ImplService service = apkcService;
// 调取批量代选课线程
threadService.startThread(inv, dw,key,ThreadType.ZDPKC.getCHValue(), ThreadType.ZDPKC.getValue(), yh.getCxid(), yh.getYhm(), yh.getYhxm(), schoolid, service,
jsonObject);
}
public String startThread(final Invocation inv, final Dw dw, final String xckey, final String name, final int type, long cxid, String username,
String realname, final int schoolid, final ImplService service, final JSONObject jsonObject) {
// 线程启动状态
boolean status = false;
// 通过key和模块类型及单位信息获取线程状态
XcLog xcLog = xcLogDAO.find(xckey, type, schoolid);
// 线程状态判断
if (xcLog != null) {
int xcstatus = xcLog.getStatus();
if (xcstatus != 0) {
status = true;
}
if (xcstatus == 1) {
xcLogDAO.updateStatus(2, xcLog.getId());
}
}
else {
status = true;
}
if (status) {
// 新增线程
final long threadId = this.setThread(inv, xckey, name, type, cxid, username, realname, schoolid, service, jsonObject);
try {
ThreadService task = new ThreadService(inv, dw, service, jsonObject, threadId, xcLogDAO);
Thread th = new Thread(task);
th.start(); //
Thread.sleep(1000);
Thread.yield();//
}
catch (Exception e) {
e.printStackTrace();
}
}
return "线程已启动";
}
public class ThreadService implements Runnable {
private ImplService service;
public void run(){
String msg = service.thread(inv, dw, jsonObject, threadId); //线程任务
}
}
public class APKCService implements ImplService {
public String thread(Invocation inv, Dw dw, JSONObject jsonObject, long threadId) {
commonService.threadService.updateNum(total, -1, successamount, erroramount, threadId);
}
}
public int updateNum(long total, long nowamount, long successamount, long erroramount, long id) {
// TODO: handle exception
int result = 0;
try {
result = xcLogDAO.updateNum(xcLog); //更新线程表中数据
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
html
<td align="center" id="numMsg"></td>
前端js
//按钮点击触发定时器
setInterval(getXLog(),3000);
//
function getXLog(){
$.ajax({
type : "get",
url : "/api/getThreadInfoByKey",
data : {
"key" : key,
"type" : type,
"schoolid" : schoolid
},
success : function(data){
var json = JSON.parse(data); //json.xcLog.total
if(json.xcLog.nowamount == ksykTotal || json.xcLog.status !=0){
$("#numMsg").html('本次安排考场结束');
clearInterval(myvar[checkid]); //结束定时器
}else{
$("#numMsg").html(json.xcLog.nowamount+'/'+ksykTotal);
}
}
});
}