這篇文章主要介紹了Spring定時(shí)任務(wù)輪詢本地?cái)?shù)據(jù)庫(kù)實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
為城區(qū)等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及城區(qū)網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、城區(qū)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
要做的東西很莫名其妙 可以說(shuō)是數(shù)據(jù)互通的一個(gè)接口吧 當(dāng)本地有表單提交后 處理一下數(shù)據(jù) 發(fā)送給另一個(gè)公司的接口
在表單提交的存庫(kù)的controller里 直接處理數(shù)據(jù) 封裝 并發(fā)送就完事了 .
然而領(lǐng)導(dǎo)叫我寫(xiě)一個(gè)接口...接收數(shù)據(jù) 處理 并發(fā)送. 到最后又改成用觸發(fā)器的方式 然而寫(xiě)觸發(fā)器的并不會(huì)用它發(fā)送http請(qǐng)求 我只能用Spring的定時(shí)任務(wù) 讀取數(shù)據(jù)庫(kù)的更新?tīng)顟B(tài) 并且處理數(shù)據(jù) 發(fā)送到那個(gè)公司的接口里
這是httpclient的示例
復(fù)制代碼
1 public class HttpClinet {
2
3 public static String doPost(Object object ) {
4
5 String responseJson = null;
6 //獲得http客戶端
7 CloseableHttpClient httpClient = HttpClientBuilder.create().build();
8 //創(chuàng)建post請(qǐng)求
9 //處理參數(shù)
10 String uri = null;
11 try{
12 uri ="http://10.145.1.12:80/promo/api/approval/todoItem?sourceID=XZBZ&secretKey=OHzwIVM6";
13
14
15 }catch (Exception e){
16 e.printStackTrace();
17 }
18
19 HttpPost httpPost = new HttpPost(uri);
20
21
22 //將對(duì)象轉(zhuǎn)換成json字符串 然后存入entity中 請(qǐng)求body
23 System.out.println(JSON.toJSONString(object));
24 StringEntity entity = new StringEntity(JSON.toJSONString(object),"UTF-8");
25
26 //將json字符串放入請(qǐng)求body中
27 httpPost.setEntity(entity);
28
29 //設(shè)置請(qǐng)求頭
30 httpPost.setHeader("Content-Type","application/json;charset=utf-8");
31
32 //獲取response模型
33
34 CloseableHttpResponse response = null;
35 try {
36 //發(fā)送post請(qǐng)求后 由response模型接收
37 response = httpClient.execute(httpPost);
38 //從響應(yīng)模型中獲取body
39 HttpEntity responseEntity = response.getEntity();
40
41
42 responseJson = EntityUtils.toString(responseEntity);
43
44 if(responseEntity != null){
45 System.out.println("相應(yīng)內(nèi)容為:" + responseJson);
46 }
47 }catch (Exception e){
48 e.printStackTrace();
49 }finally {
50 try {
51 if (httpClient != null){
52 httpClient.close();
53 }if (response != null){
54 response.close();
55 }
56 }catch (Exception e){
57 e.printStackTrace();
58 }
59 }
60 return responseJson;
61
62 }
63 }
復(fù)制代碼
這是定時(shí)任務(wù)(非quartz)
復(fù)制代碼
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
復(fù)制代碼
任務(wù)的具體方法
@Component public class SendDataTask { @Autowired WorkFlowService workFlowServiceImpl; //首次推送待審 @Scheduled(cron = "0 */1 * * * ?") // 間隔一分鐘執(zhí)行 public void taskCycle() throws UnsupportedEncodingException { //第一次推送任務(wù)(產(chǎn)生任務(wù)),產(chǎn)生新任務(wù)則產(chǎn)生審批人,還需要取消掉沒(méi)有認(rèn)領(lǐng)任務(wù)的審批人的消息 Listlist = workFlowServiceImpl.getToDoTaskInfoBySendStatus("0","100"); ToDoTaskModel toDoTaskModel = new ToDoTaskModel(); for (int i = 0; i < list.size(); i++) { toDoTaskModel = list.get(i); //自拼title //根據(jù)workflowID 判斷拼接url String taskID = toDoTaskModel.getTaskID(); String realTaskID = ChangeTaskIDUitl.changeTaskIDUitl(taskID); String workflowID = toDoTaskModel.getWorkflowID(); String docID = toDoTaskModel.getDocID(); String title = "您有一條新的待審任務(wù)" + toDoTaskModel.getTodoTitle() +"等待審批"; String taskStatus = "1"; String submitorID = toDoTaskModel.getSubmitorID(); String loginName = workFlowServiceImpl.getLoginName(submitorID); String submitorName = toDoTaskModel.getSubmitorName(); String submitDateTime = toDoTaskModel.getSubmitDateTime(); String openUrl = null; String beforeUrl = "http://oa20.cctv.com/"; String afterUrl = "/myWork/workWait/gwWeChatWait.do?instanceId="; //5合同 if(workflowID.equals("5")){ openUrl = beforeUrl + "HT_WeChat" + afterUrl +docID +"&workflowId="+workflowID; }else{ openUrl = beforeUrl + "GW_WeChat" + afterUrl +docID +"&workflowId="+workflowID; } String sendStatus = toDoTaskModel.getSendStatus(); //查詢審批人的真實(shí)ID //根據(jù)docid 和taskstatus =40 來(lái)查審批者的submitorID 然后根據(jù)這個(gè)來(lái)查真實(shí)工號(hào) //獲取數(shù)據(jù)庫(kù)查詢的 當(dāng)前審批層級(jí)信息 List tlist = toDoTaskModel.getThisLevelInfoList(); //new一個(gè)移動(dòng)端要的格式的模型 List approverList = new ArrayList (); ApproverPOJO approverPOJO = new ApproverPOJO(); for (int j = 0; j < tlist.size(); j++) { String realApproverID = workFlowServiceImpl.getLoginName(tlist.get(j).getApproverID()); approverPOJO.setDocID(tlist.get(j).getDocID()); approverPOJO.setApproverName(tlist.get(j).getApproverName()); approverPOJO.setApproverID(realApproverID); approverList.add(approverPOJO); } //ToDoTaskPOJO里的最高層對(duì)象(list(模型)) ThisLevelInfoPOJO thisLevelInfoPOJO = new ThisLevelInfoPOJO(); thisLevelInfoPOJO.setApproverList(approverList); ToDoTaskPOJO toDoTaskPOJO = new ToDoTaskPOJO(realTaskID,workflowID,docID,title,taskStatus, loginName,submitorName,submitDateTime,openUrl,sendStatus,thisLevelInfoPOJO); JSONObject jsonObject= JSONObject.fromObject(toDoTaskPOJO); String jsonStr = HttpClinet.doPost(jsonObject); JSONObject jsonCode = JSONObject.fromObject(jsonStr); if("0".equals(jsonCode.get("errCode"))) { //這要加判斷? 是否是終審人? 條件:提交人工號(hào)=審核人工號(hào)時(shí) 該審批層級(jí)未最終審批???? //或者終審層級(jí) 有沒(méi)有審核人 如果沒(méi)有 移動(dòng)端返回錯(cuò)誤碼 if (workFlowServiceImpl.isFirstSend() == 1) { workFlowServiceImpl.updateSendStatus("2", taskID); }else{ workFlowServiceImpl.updateSendStatus("1", taskID);} }else if("當(dāng)前層級(jí)待審人不能為空".equals(jsonCode.get("errDesc"))){ workFlowServiceImpl.updateSendStatus("1", taskID); } /*_____________________________________________________________________________________*/ //查詢舊表該docID下的所有taskID 并將(taskID和docID)插入新表 //先查詢docID下所有的taskID List taskIDList = new ArrayList (); taskIDList = workFlowServiceImpl.selectAllByDocID(docID); for (int j = 0; j < taskIDList.size(); j++) { //想新表插入所有taskID和docID workFlowServiceImpl.creatAllInfo(taskIDList.get(j), docID); } TaskStatusPOJO taskStatusPOJO = new TaskStatusPOJO(); //查詢新表和舊表 對(duì)比 舊表不存在二新表存在的taskID 通過(guò)第二個(gè)url推送給WeChat端,刪掉消息 List invalidList = new ArrayList (); invalidList = workFlowServiceImpl.SelectInvalidApprover(); String invalidTaskID = null; for (int j = 0; j < invalidList.size(); j++) { invalidTaskID = ChangeTaskIDUitl.changeTaskIDUitl(invalidList.get(j)); taskStatusPOJO.setTaskID(invalidTaskID); taskStatusPOJO.setTaskStatus("2"); JSONObject jsonObj= JSONObject.fromObject(taskStatusPOJO); HttpSecondSend.doPost(jsonObj); } } //第二個(gè)接口 告訴當(dāng)前待辦人審批完結(jié) List secondList = workFlowServiceImpl.getNewStatus("1","100"); TaskStatusPOJO taskStatusPOJO = new TaskStatusPOJO(); for (int i = 0; i < secondList.size(); i++) { String dID = secondList.get(i).getTaskID(); String docID = secondList.get(i).getOnlyDocID(); String realTaskID =ChangeTaskIDUitl.changeTaskIDUitl(workFlowServiceImpl.getTrueTaskID(docID)); taskStatusPOJO.setTaskID(realTaskID); taskStatusPOJO.setTaskStatus("2"); JSONObject jsonObject= JSONObject.fromObject(taskStatusPOJO); String Str = HttpSecondSend.doPost(jsonObject); JSONObject json = JSONObject.fromObject(Str); if ("0".equals(json.get("errCode"))) { workFlowServiceImpl.updateSendStatus("2", dID); } } } }
其實(shí)這個(gè)東西很簡(jiǎn)單 只是需求變?nèi)?從提交表單給我發(fā)數(shù)據(jù) 到用觸發(fā)器給我發(fā)http請(qǐng)求(json串 自己處理數(shù)據(jù)) 再到最后 我只能去數(shù)據(jù)庫(kù)自己查 自己處理數(shù)據(jù) 自己發(fā)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。