Commit 1129ab96272cb80be762ebf9ff0151347f7adb88

Authored by fangcheng
1 parent f5f514b990
Exists in master

添加节点数据push到中心

Showing 10 changed files with 184 additions and 4 deletions

parent/base.common/src/main/java/com/lyms/base/common/dao/sys/SyncDataBasicMapper.java View file @ 1129ab9
... ... @@ -32,5 +32,7 @@
32 32 */
33 33 public Integer updateBatchByIds(List<Serializable> ids);
34 34  
  35 + public Integer errorCountAdd(Serializable id);
  36 +
35 37 }
parent/base.common/src/main/java/com/lyms/base/common/dao/sys/SyncDataBasicMapper.xml View file @ 1129ab9
... ... @@ -9,11 +9,12 @@
9 9 <result column="DATA" property="data" />
10 10 <result column="CREATE_TIME" property="createTime" />
11 11 <result column="IFSUC" property="ifsuc" />
  12 + <result column="ERROR_COUNT" property="errorCount" />
12 13 </resultMap>
13 14  
14 15 <!-- 通用查询结果列 -->
15 16 <sql id="Base_Column_List">
16   - ID AS id, DATA AS data, CREATE_TIME AS createTime, IFSUC AS ifsuc
  17 + ID AS id, DATA AS data, CREATE_TIME AS createTime, IFSUC AS ifsuc,ERROR_COUNT AS errorCount
17 18 </sql>
18 19  
19 20 <sql id="Base_table">
20 21  
... ... @@ -22,11 +23,17 @@
22 23  
23 24  
24 25 <update id="updateBatchByIds" parameterType="list">
25   - update from <include refid="Base_table" /> as sb set sb.ifsuc=1
  26 + update <include refid="Base_table" /> as sb set sb.ifsuc=1
26 27 where sb.id in
27 28 <foreach item="item" collection="ids" open="(" close=")" separator=",">
28 29 #{item}
29 30 </foreach>
  31 + </update>
  32 +
  33 +
  34 + <update id="errorCountAdd">
  35 + update SYNC_DATA_BASIC set ERROR_COUNT= ERROR_COUNT + 1
  36 + where id = #{id}
30 37 </update>
31 38  
32 39 </mapper>
parent/base.common/src/main/java/com/lyms/base/common/entity/sys/SyncDataBasic.java View file @ 1129ab9
... ... @@ -42,7 +42,13 @@
42 42 */
43 43 @TableField(value = "IFSUC")
44 44 private Integer ifsuc;
45   -
  45 +
  46 +
  47 + /**
  48 + * 错误次数
  49 + */
  50 + @TableField(value = "ERROR_COUNT")
  51 + private Integer errorCount;
46 52 public String getId() {
47 53 return id;
48 54 }
... ... @@ -74,6 +80,14 @@
74 80 public void setIfsuc(Integer ifsuc) {
75 81 this.ifsuc = ifsuc;
76 82 }
  83 +
  84 + public Integer getErrorCount() {
  85 + return errorCount;
  86 + }
  87 +
  88 + public void setErrorCount(Integer errorCount) {
  89 + this.errorCount = errorCount;
  90 + }
77 91  
78 92 }
parent/base.common/src/main/java/com/lyms/base/common/service/sys/SyncDataBasicService.java View file @ 1129ab9
... ... @@ -59,6 +59,18 @@
59 59 * <li>修改时间:
60 60 */
61 61 public boolean addSyncData(SyncParamEnum syncParamConf,Object ...dataObjects);
  62 +
  63 +
  64 + /**
  65 + * <li>@Description:错误次数添加
  66 + * <li>@param id
  67 + * <li>@return
  68 + * <li>创建人:方承
  69 + * <li>创建时间:2017年5月6日
  70 + * <li>修改人:
  71 + * <li>修改时间:
  72 + */
  73 + public boolean errorCountAdd(Serializable id);
62 74  
63 75 }
parent/base.common/src/main/java/com/lyms/base/common/service/sys/impl/SyncDataBasicServiceImpl.java View file @ 1129ab9
... ... @@ -10,6 +10,7 @@
10 10  
11 11 import com.baomidou.mybatisplus.mapper.EntityWrapper;
12 12 import com.baomidou.mybatisplus.service.impl.ServiceImpl;
  13 +import com.baomidou.mybatisplus.toolkit.SqlUtils;
13 14 import com.lyms.base.common.dao.sys.SyncDataBasicMapper;
14 15 import com.lyms.base.common.entity.sys.SyncDataBasic;
15 16 import com.lyms.base.common.enums.SyncParamEnum;
... ... @@ -91,6 +92,12 @@
91 92  
92 93 entity.setData(cdata.toJsonString());
93 94 Integer tag = baseMapper.insert(entity);
  95 + return tag >= 1 ? true : false;
  96 + }
  97 +
  98 + @Override
  99 + public boolean errorCountAdd(Serializable id) {
  100 + Integer tag = baseMapper.errorCountAdd(id);
94 101 return tag >= 1 ? true : false;
95 102 }
96 103  
parent/center.manager/src/main/java/com/lyms/cm/controller/sync/SyncController.java View file @ 1129ab9
  1 +package com.lyms.cm.controller.sync;
  2 +
  3 +import java.io.IOException;
  4 +
  5 +import javax.servlet.http.HttpServletRequest;
  6 +
  7 +import org.apache.commons.lang3.builder.ToStringBuilder;
  8 +import org.slf4j.Logger;
  9 +import org.slf4j.LoggerFactory;
  10 +import org.springframework.http.MediaType;
  11 +import org.springframework.web.bind.annotation.RequestMapping;
  12 +import org.springframework.web.bind.annotation.RestController;
  13 +
  14 +import com.lyms.sync.ParamsAdpter;
  15 +import com.lyms.sync.SyncHandler;
  16 +import com.lyms.sync.SyncUtils;
  17 +import com.lyms.sync.channel.ChannelData;
  18 +import com.lyms.web.controller.BaseController;
  19 +
  20 +/**
  21 + * <li>@ClassName: RouterController
  22 + * <li>@Description: 远程接口调用数据处理分发
  23 + * <li>@author maliang
  24 + * <li>@date 2017年3月14日
  25 + * <li>
  26 + */
  27 +@RestController
  28 +@RequestMapping("/sync")
  29 +public class SyncController extends BaseController {
  30 +
  31 + private static final Logger log = LoggerFactory.getLogger(SyncController.class);
  32 +
  33 + @RequestMapping(value = "/push", produces = { MediaType.APPLICATION_JSON_VALUE })
  34 + public Object push(HttpServletRequest request) throws Exception {
  35 + ChannelData data = SyncUtils.conver(request);
  36 + log.debug("中心接受到节点推送数据:" + ToStringBuilder.reflectionToString(data));
  37 + Object handResult = SyncHandler.handler(data);
  38 + if (data.getLoop()) {
  39 + data = (ChannelData) handResult;
  40 + } else {
  41 + data.setData(ParamsAdpter.builder().push(handResult).toJsonString());
  42 + }
  43 + return data;
  44 + }
  45 +
  46 +
  47 +}
parent/center.manager/src/main/resources/xml/app-shiro.xml View file @ 1129ab9
... ... @@ -49,6 +49,7 @@
49 49 /remote/** =anon
50 50 /logout = logout
51 51 /static/** = anon
  52 + /sync/** = anon
52 53 /authenticated = authc
53 54 /** = user,forceLogout
54 55 </value>
parent/hospital.web/src/main/java/com/lyms/hospital/job/SyncPushToCenterJob.java View file @ 1129ab9
  1 +package com.lyms.hospital.job;
  2 +
  3 +import java.util.List;
  4 +
  5 +import org.apache.commons.lang3.builder.ToStringBuilder;
  6 +import org.slf4j.Logger;
  7 +import org.slf4j.LoggerFactory;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.beans.factory.annotation.Value;
  10 +
  11 +import com.alibaba.fastjson.JSONObject;
  12 +import com.baomidou.mybatisplus.mapper.EntityWrapper;
  13 +import com.lyms.base.common.entity.sys.SyncDataBasic;
  14 +import com.lyms.base.common.service.sys.SyncDataBasicService;
  15 +import com.lyms.sync.channel.ChannelData;
  16 +import com.lyms.util.HttpUtils;
  17 +import com.lyms.util.JsonUtils;
  18 +
  19 +/**
  20 + * <li>@ClassName: SyncTableJob
  21 + * <li>@Description: 同步任务执行器,获取SYNC_DATA_BASIC表格的数据进行推送到中心
  22 + * <li>@author 方承
  23 + * <li>@date 2017年5月5日
  24 + * <li>
  25 + */
  26 +public class SyncPushToCenterJob {
  27 + @Value("${center.syncpush.url}")
  28 + private String CENTER_SYNCPUSH_URL;
  29 +
  30 + private static final int ERROR_COUNT_MAX = 4;
  31 +
  32 + private static Logger log = LoggerFactory.getLogger(SyncPushToCenterJob.class);
  33 +
  34 + @Autowired
  35 + private SyncDataBasicService syncDataBasicService;
  36 +
  37 + @SuppressWarnings("unused")
  38 + public void excute() {
  39 + if(null != CENTER_SYNCPUSH_URL && CENTER_SYNCPUSH_URL.startsWith("http")){
  40 + List<SyncDataBasic> dataList = syncDataBasicService.selectList(new EntityWrapper<SyncDataBasic>().where("IFSUC=0").and("ERROR_COUNT < {0}", ERROR_COUNT_MAX));
  41 + if(dataList != null ){
  42 + log.debug("=======开始进行 SYNC_DATA_BASIC 表格数据的推送到中心=======");
  43 + for(SyncDataBasic entity : dataList){
  44 + try{
  45 + ChannelData data = JsonUtils.jsonToBean(entity.getData(), ChannelData.class);
  46 + data.setRemote(CENTER_SYNCPUSH_URL);
  47 + log.debug("节点推送数据:"+ToStringBuilder.reflectionToString(data));
  48 + String result = HttpUtils.REMOTE.post(data);
  49 + if(result == null){//远程无法访问等情况
  50 + syncDataBasicService.errorCountAdd(entity.getId());
  51 + return;
  52 + }
  53 + ChannelData model = JsonUtils.jsonToBean(result, ChannelData.class);
  54 + entity.setIfsuc(1);
  55 + syncDataBasicService.updateById(entity);
  56 + log.debug("中心处后数据:"+result);
  57 + }catch (Exception e) {
  58 + syncDataBasicService.errorCountAdd(entity.getId());
  59 + log.error("【SYNC_DATA_BASIC】表格数据的的推送到中心错误:SyncDataBasic = {} {}",JSONObject.toJSON(entity),e);
  60 + }
  61 + }
  62 + }
  63 + }
  64 + }
  65 +
  66 +
  67 +}
parent/hospital.web/src/main/resources/dev/conf.properties View file @ 1129ab9
1 1 token.prefix=token:user:
2 2 isCenterOperateSyncToNode=false
  3 +center.syncpush.url=http://localhost:8080/center.manager/sync/push
parent/hospital.web/src/main/resources/xml/app-timer.xml View file @ 1129ab9
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<beans xmlns="http://www.springframework.org/schema/beans"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xmlns:context="http://www.springframework.org/schema/context"
  5 + xmlns:tx="http://www.springframework.org/schema/tx"
  6 + xmlns:task="http://www.springframework.org/schema/task"
  7 + xmlns:aop="http://www.springframework.org/schema/aop"
  8 + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  9 + http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  10 + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  11 + http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
  12 + http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
  13 + ">
  14 +
  15 + <!-- 同步远程数据, 定时任务处理同步任务 -->
  16 + <task:scheduled-tasks>
  17 + <task:scheduled ref="syncPushToCenterJob" method="excute" cron="*/5 * * * * ?"/>
  18 + </task:scheduled-tasks>
  19 +
  20 + <bean class="com.lyms.hospital.job.SyncPushToCenterJob" id="syncPushToCenterJob"/>
  21 +
  22 +</beans>