Spring定时任务轮询本地数据库实现过程解析
人气:1这篇文章主要介绍了Spring定时任务轮询本地数据库实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
要做的东西很莫名其妙 可以说是数据互通的一个接口吧 当本地有表单提交后 处理一下数据 发送给另一个公司的接口
在表单提交的存库的controller里 直接处理数据 封装 并发送就完事了 .
然而领导叫我写一个接口...接收数据 处理 并发送. 到最后又改成用触发器的方式 然而写触发器的并不会用它发送http请求 我只能用Spring的定时任务 读取数据库的更新状态 并且处理数据 发送到那个公司的接口里
这是httpclient的示例
复制代码
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 //创建post请求
9 //处理参数
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 //将对象转换成json字符串 然后存入entity中 请求body
23 System.out.println(JSON.toJSONString(object));
24 StringEntity entity = new StringEntity(JSON.toJSONString(object),"UTF-8");
25
26 //将json字符串放入请求body中
27 httpPost.setEntity(entity);
28
29 //设置请求头
30 httpPost.setHeader("Content-Type","application/json;charset=utf-8");
31
32 //获取response模型
33
34 CloseableHttpResponse response = null;
35 try {
36 //发送post请求后 由response模型接收
37 response = httpClient.execute(httpPost);
38 //从响应模型中获取body
39 HttpEntity responseEntity = response.getEntity();
40
41
42 responseJson = EntityUtils.toString(responseEntity);
43
44 if(responseEntity != null){
45 System.out.println("相应内容为:" + 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 }
复制代码
这是定时任务(非quartz)
复制代码
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- mybatis配置 -->
<import resource="classpath:applicationContext-mybatis.xml"/>
<task:annotation-driven />
<context:component-scan base-package="com.litsoft.workflowApi.task"/>
复制代码
任务的具体方法
@Component public class SendDataTask { @Autowired WorkFlowService workFlowServiceImpl; //首次推送待审 @Scheduled(cron = "0 */1 * * * ?") // 间隔一分钟执行 public void taskCycle() throws UnsupportedEncodingException { //第一次推送任务(产生任务),产生新任务则产生审批人,还需要取消掉没有认领任务的审批人的消息 List<ToDoTaskModel> list = workFlowServiceImpl.getToDoTaskInfoBySendStatus("0","100"); ToDoTaskModel toDoTaskModel = new ToDoTaskModel(); for (int i = 0; i < list.size(); i++) { toDoTaskModel = list.get(i); //自拼title //根据workflowID 判断拼接url String taskID = toDoTaskModel.getTaskID(); String realTaskID = ChangeTaskIDUitl.changeTaskIDUitl(taskID); String workflowID = toDoTaskModel.getWorkflowID(); String docID = toDoTaskModel.getDocID(); String title = "您有一条新的待审任务" + 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(); //查询审批人的真实ID //根据docid 和taskstatus =40 来查审批者的submitorID 然后根据这个来查真实工号 //获取数据库查询的 当前审批层级信息 List<ThisLevelInfoModel> tlist = toDoTaskModel.getThisLevelInfoList(); //new一个移动端要的格式的模型 List<ApproverPOJO> approverList = new ArrayList<ApproverPOJO>(); 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里的最高层对象(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"))) { //这要加判断? 是否是终审人? 条件:提交人工号=审核人工号时 该审批层级未最终审批???? //或者终审层级 有没有审核人 如果没有 移动端返回错误码 if (workFlowServiceImpl.isFirstSend() == 1) { workFlowServiceImpl.updateSendStatus("2", taskID); }else{ workFlowServiceImpl.updateSendStatus("1", taskID);} }else if("当前层级待审人不能为空".equals(jsonCode.get("errDesc"))){ workFlowServiceImpl.updateSendStatus("1", taskID); } /*_____________________________________________________________________________________*/ //查询旧表该docID下的所有taskID 并将(taskID和docID)插入新表 //先查询docID下所有的taskID List<String> taskIDList = new ArrayList<String>(); taskIDList = workFlowServiceImpl.selectAllByDocID(docID); for (int j = 0; j < taskIDList.size(); j++) { //想新表插入所有taskID和docID workFlowServiceImpl.creatAllInfo(taskIDList.get(j), docID); } TaskStatusPOJO taskStatusPOJO = new TaskStatusPOJO(); //查询新表和旧表 对比 旧表不存在二新表存在的taskID 通过第二个url推送给WeChat端,删掉消息 List<String> invalidList = new ArrayList<String>(); 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); } } //第二个接口 告诉当前待办人审批完结 List<TaskStatusPOJO> 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); } } } }
其实这个东西很简单 只是需求变三变 从提交表单给我发数据 到用触发器给我发http请求(json串 自己处理数据) 再到最后 我只能去数据库自己查 自己处理数据 自己发
您可能感兴趣的文章:
加载全部内容