Implementation process analysis of spring scheduled task polling local database

This article mainly introduces the implementation process analysis of spring timed task polling local database. It is introduced in great detail through the example code, which has a certain reference value for everyone's study or work. Friends in need can refer to it

The things to be done are inexplicable. It can be said that it is an interface for data exchange. When a local form is submitted, deal with the interface for sending data to another company

In the controller of the form submission repository, directly handle the data encapsulation and sending

However, the leader asked me to write an interface Receive data, process and send In the end, it was changed to trigger. However, those who write triggers do not use it to send HTTP requests. I can only use spring's scheduled tasks to read the update status of the database and process the data and send it to the company's interface

This is an example of an httpclient

Copy code 1 public class httpclinet {2 3 public static string dopost (object) {4 5 string responsejason = null; 6 / / obtain HTTP client 7 closeablehttpclient httpclient = httpclientbuilder. Create(). Build(); 8 / / create post request 9 / / processing parameters 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 / / convert the object into JSON string and store it in entity. Request body 23 system.out.println (JSON. Tojsonstring (object)); 24 stringentity = new stringentity (JSON. Tojsonstring (object)," UTF-8 "); 25 26 / / put the JSON string into the request body 27 httppost.setentity (entity); 28 29 / / set the request header 30 httppost.setheader (" content type "," application / JSON; charset = UTF-8 "); 31 32 / / get the response model 33 34 closeablehttpresponse response response = null; 35 try {36 / / after sending the post request, the response model receives 37 response = httpclient.execute (httppost) ; 38 / / obtain body 39 httpentity responseentity = response from the response model getEntity(); 40 41 42 responseJson = EntityUtils. toString(responseEntity); 43 44 If (responseentity! = null) {45 system. Out. Println ("the corresponding content is:" + responsejason "); 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} copy code

This is a scheduled task (non quartz)

Copy code http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd ">

Specific method of 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);
      }
    }
  }
}

In fact, this thing is very simple, but the demand changes three times, from submitting the form to sending me data to sending me HTTP requests with triggers (JSON string to process the data myself), and finally I can only go to the database to check, process and send the data myself

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>