Commit 4e922454c9b967d4f9d59ae8bfc0bfde5a960cdc

Authored by maliang
1 parent dd3d1d3db3
Exists in master

简化任务传递流程

Showing 8 changed files with 178 additions and 101 deletions

center.manager/src/main/java/com/lyms/cm/controller/sys/SysRemoteController.java View file @ 4e92245
... ... @@ -9,7 +9,7 @@
9 9 import org.springframework.web.bind.annotation.RequestMapping;
10 10 import org.springframework.web.bind.annotation.ResponseBody;
11 11  
12   -import com.lyms.sycn.ParamsHandler;
  12 +import com.lyms.sycn.ParamsAdpter;
13 13 import com.lyms.sycn.SycnHandler;
14 14 import com.lyms.sycn.SycnUtils;
15 15 import com.lyms.sycn.channel.ChannelData;
... ... @@ -25,7 +25,7 @@
25 25 public Object push(HttpServletRequest request) throws ClassNotFoundException, IOException {
26 26 ChannelData model = SycnUtils.conver(request);
27 27 Object object = SycnHandler.handler(model);
28   - model.setData(ParamsHandler.builder().push(object).toJsonString());
  28 + model.setData(ParamsAdpter.builder().push(object).toJsonString());
29 29 model.setAck(true);
30 30 return model;
31 31 }
center.manager/src/main/java/com/lyms/cm/job/SycnFixJob.java View file @ 4e92245
... ... @@ -6,7 +6,7 @@
6 6 import org.slf4j.LoggerFactory;
7 7 import org.springframework.beans.factory.annotation.Autowired;
8 8  
9   -import com.lyms.sycn.ParamsHandler;
  9 +import com.lyms.sycn.ParamsAdpter;
10 10 import com.lyms.sycn.SycnCallback;
11 11 import com.lyms.sycn.SycnCenter;
12 12 import com.lyms.sycn.SycnCenter.Work;
... ... @@ -55,7 +55,7 @@
55 55 Boolean tag = (Boolean) handlResult;
56 56 if (tag) {
57 57 data.setRemote(oldData.getRemote());
58   - data.setData(ParamsHandler.builder().push(data.getId()).toJsonString());
  58 + data.setData(ParamsAdpter.builder().push(data.getId()).toJsonString());
59 59 data.setAck(true);
60 60 // 处理成功回执消息
61 61 data.setRemoteClazz("com.lyms.hospital.service.sys.impl.SyncDataBasicServiceImpl");
center.manager/src/test/java/center/manager/test/user/WorkTest.java View file @ 4e92245
... ... @@ -6,11 +6,11 @@
6 6  
7 7 import com.alibaba.fastjson.JSON;
8 8 import com.lyms.cm.entity.sys.SysUsers;
9   -import com.lyms.synch.ParamsHandler;
10   -import com.lyms.synch.SyncCallback;
11   -import com.lyms.synch.SyncnCenter;
12   -import com.lyms.synch.SyncnCenter.Work;
13   -import com.lyms.synch.entity.ChannelData;
  9 +import com.lyms.sycn.ParamsAdpter;
  10 +import com.lyms.sycn.SycnCallback;
  11 +import com.lyms.sycn.SycnCenter;
  12 +import com.lyms.sycn.SycnCenter.Work;
  13 +import com.lyms.sycn.channel.ChannelData;
14 14 import com.lyms.util.HttpUtils;
15 15 import com.lyms.util.JsonUtils;
16 16 import com.lyms.util.StrUtils;
... ... @@ -25,7 +25,7 @@
25 25 public class WorkTest extends BaseTest {
26 26  
27 27 @Autowired
28   - public SyncnCenter center;
  28 + public SycnCenter center;
29 29  
30 30 @Test
31 31 public void push() {
... ... @@ -38,7 +38,7 @@
38 38  
39 39 ChannelData push = new ChannelData(remote, remoteClazz, remoteMethod, null, loop);
40 40  
41   - center.buildWork().push(push, new SyncCallback() {
  41 + center.buildWork().push(push, new SycnCallback() {
42 42 @Override
43 43 public void callBack(Object object) {
44 44 System.out.println("push callBack : " + object);
... ... @@ -77,7 +77,7 @@
77 77 try {
78 78 // final AckObj ack = new AckObj();
79 79  
80   - work.pullTmp(new SyncCallback() {
  80 + work.pullTmp(new SycnCallback() {
81 81 @Override
82 82 public void callBack(Object object) {
83 83 if (object == null)
... ... @@ -115,7 +115,7 @@
115 115 Work work2 = center.buildWork();
116 116 try {
117 117 // final AckObj ack = new AckObj();
118   - work2.pullFix(new SyncCallback() {
  118 + work2.pullFix(new SycnCallback() {
119 119 @Override
120 120 public void callBack(Object object) {
121 121 if (object != null && object instanceof ChannelData) {
... ... @@ -151,7 +151,7 @@
151 151 // Object params = new Object[] { user, null };
152 152  
153 153 // 设置参数
154   - ParamsHandler params = ParamsHandler.builder().push(user).push(null);
  154 + ParamsAdpter params = ParamsAdpter.builder().push(user).push(null);
155 155 data.setData(params.toJsonString());
156 156  
157 157 String str = JSON.toJSONString(data);
core.sdk/src/main/java/com/lyms/sycn/ParamsAdpter.java View file @ 4e92245
  1 +package com.lyms.sycn;
  2 +
  3 +import java.io.Serializable;
  4 +import java.util.LinkedList;
  5 +import java.util.List;
  6 +
  7 +import com.lyms.util.JsonUtils;
  8 +
  9 +/**
  10 + * <li>@ClassName: ParamsHandler
  11 + * <li>@Description: 参数处理
  12 + * <li>@author maliang
  13 + * <li>@date 2017年3月15日
  14 + * <li>
  15 + */
  16 +public class ParamsAdpter implements Serializable {
  17 +
  18 + /**
  19 + */
  20 + private static final long serialVersionUID = 7048461500310029490L;
  21 + private List<Param> params = new LinkedList<>();
  22 +
  23 + public ParamsAdpter() {
  24 + }
  25 +
  26 + public static ParamsAdpter builder() {
  27 + return new ParamsAdpter();
  28 + }
  29 +
  30 + public String toJsonString() {
  31 + return JsonUtils.beanToJson(this);
  32 + }
  33 +
  34 + public ParamsAdpter push(Object obj) {
  35 + if (obj == null) {
  36 + params.add(new Param(Object.class.getName(), "null"));
  37 + } else {
  38 + params.add(new Param(obj.getClass().getName(), obj));
  39 + }
  40 + return this;
  41 + }
  42 +
  43 + public static class Param implements Serializable {
  44 + /**
  45 + */
  46 + private static final long serialVersionUID = -1634342674830431814L;
  47 + private String className;
  48 + private Object obj;
  49 +
  50 + public Param() {
  51 + };
  52 +
  53 + public Param(String className, Object obj) {
  54 + this.className = className;
  55 + this.obj = obj;
  56 + }
  57 +
  58 + public String getClassName() {
  59 + return className;
  60 + }
  61 +
  62 + public void setClassName(String className) {
  63 + this.className = className;
  64 + }
  65 +
  66 + public Object getObj() {
  67 + return obj;
  68 + }
  69 +
  70 + public void setObj(Object obj) {
  71 + this.obj = obj;
  72 + }
  73 + }
  74 +
  75 + public List<Param> getParams() {
  76 + return params;
  77 + }
  78 +
  79 + public void setParams(List<Param> params) {
  80 + this.params = params;
  81 + }
  82 +
  83 +}
core.sdk/src/main/java/com/lyms/sycn/ParamsHandler.java View file @ 4e92245
1   -package com.lyms.sycn;
2   -
3   -import java.io.Serializable;
4   -import java.util.LinkedList;
5   -import java.util.List;
6   -
7   -import com.lyms.util.JsonUtils;
8   -
9   -/**
10   - * <li>@ClassName: ParamsHandler
11   - * <li>@Description: 参数处理
12   - * <li>@author maliang
13   - * <li>@date 2017年3月15日
14   - * <li>
15   - */
16   -public class ParamsHandler implements Serializable {
17   -
18   - /**
19   - */
20   - private static final long serialVersionUID = 7048461500310029490L;
21   - private List<Param> params = new LinkedList<>();
22   -
23   - public ParamsHandler() {
24   - }
25   -
26   - public static ParamsHandler builder() {
27   - return new ParamsHandler();
28   - }
29   -
30   - public String toJsonString() {
31   - return JsonUtils.beanToJson(this);
32   - }
33   -
34   - public ParamsHandler push(Object obj) {
35   - if (obj == null) {
36   - params.add(new Param(Object.class.getName(), "null"));
37   - } else {
38   - params.add(new Param(obj.getClass().getName(), obj));
39   - }
40   - return this;
41   - }
42   -
43   - public static class Param implements Serializable {
44   - /**
45   - */
46   - private static final long serialVersionUID = -1634342674830431814L;
47   - private String className;
48   - private Object obj;
49   -
50   - public Param() {
51   - };
52   -
53   - public Param(String className, Object obj) {
54   - this.className = className;
55   - this.obj = obj;
56   - }
57   -
58   - public String getClassName() {
59   - return className;
60   - }
61   -
62   - public void setClassName(String className) {
63   - this.className = className;
64   - }
65   -
66   - public Object getObj() {
67   - return obj;
68   - }
69   -
70   - public void setObj(Object obj) {
71   - this.obj = obj;
72   - }
73   - }
74   -
75   - public List<Param> getParams() {
76   - return params;
77   - }
78   -
79   - public void setParams(List<Param> params) {
80   - this.params = params;
81   - }
82   -
83   -}
core.sdk/src/main/java/com/lyms/sycn/SycnHandler.java View file @ 4e92245
... ... @@ -9,7 +9,7 @@
9 9 import org.slf4j.LoggerFactory;
10 10  
11 11 import com.lyms.spring.SpringContextHolder;
12   -import com.lyms.sycn.ParamsHandler.Param;
  12 +import com.lyms.sycn.ParamsAdpter.Param;
13 13 import com.lyms.sycn.channel.ChannelData;
14 14 import com.lyms.util.JsonUtils;
15 15  
... ... @@ -25,6 +25,13 @@
25 25  
26 26 private static Logger LOG = LoggerFactory.getLogger(SycnHandler.class);
27 27  
  28 + /**
  29 + * 单例构建同步中心
  30 + * <p>
  31 + * 最好能保证确实是单例,由中心构建不同的 work
  32 + */
  33 + private static final SycnCenter center = SycnCenter.install();
  34 +
28 35 public static Object handler(ChannelData data) {
29 36 try {
30 37 return invoke(data);
... ... @@ -54,7 +61,7 @@
54 61 return null;
55 62 Object[] objs = null;
56 63 if (StringUtils.isNotBlank(params)) {
57   - ParamsHandler handler = JsonUtils.jsonToBean(params, ParamsHandler.class);
  64 + ParamsAdpter handler = JsonUtils.jsonToBean(params, ParamsAdpter.class);
58 65 if (handler != null) {
59 66 List<Param> ps = handler.getParams();
60 67 objs = new Object[ps.size()];
... ... @@ -106,6 +113,51 @@
106 113 }
107 114 }
108 115 return null;
  116 + }
  117 +
  118 + /**
  119 + * <li>@Description:TODO(方法描述)
  120 + * <li>@param t
  121 + * <li>@param loop
  122 + * <li>@return
  123 + * <li>创建人:maliang
  124 + * <li>创建时间:2017年3月16日
  125 + * <li>修改人:
  126 + * <li>修改时间:
  127 + */
  128 + public static boolean pushFix(String id, String remote, String remoteClazz, String remoteMethod,
  129 + ParamsAdpter adpter) {
  130 +
  131 + ChannelData data = new ChannelData(id, remote, remoteClazz, remoteMethod, adpter.toJsonString(), true);
  132 + try {
  133 + center.buildWork().push(data, new SycnCallback() {
  134 + @Override
  135 + public void callBack(Object object) {
  136 + // nothing to do
  137 + }
  138 + });
  139 + } catch (Exception e) {
  140 + e.printStackTrace();
  141 + return false;
  142 + }
  143 + return true;
  144 + }
  145 +
  146 + public static boolean pushTmp(String id, String remote, String remoteClazz, String remoteMethod,
  147 + ParamsAdpter adpter) {
  148 + ChannelData data = new ChannelData(id, remote, remoteClazz, remoteMethod, adpter.toJsonString(), false);
  149 + try {
  150 + center.buildWork().push(data, new SycnCallback() {
  151 + @Override
  152 + public void callBack(Object object) {
  153 + // nothing to do
  154 + }
  155 + });
  156 + } catch (Exception e) {
  157 + e.printStackTrace();
  158 + return false;
  159 + }
  160 + return true;
109 161 }
110 162  
111 163 }
core.sdk/src/main/java/com/lyms/sycn/channel/ChannelData.java View file @ 4e92245
... ... @@ -2,6 +2,8 @@
2 2  
3 3 import java.io.Serializable;
4 4  
  5 +import org.apache.commons.lang3.StringUtils;
  6 +
5 7 import com.lyms.util.JsonUtils;
6 8 import com.lyms.util.StrUtils;
7 9  
... ... @@ -65,6 +67,29 @@
65 67  
66 68 public ChannelData(String remote, String remoteClazz, String remoteMethod, String data, boolean loop) {
67 69 this.id = StrUtils.uuid();
  70 + this.remote = remote;
  71 + this.remoteClazz = remoteClazz;
  72 + this.remoteMethod = remoteMethod;
  73 + this.data = data;
  74 + this.ts = System.currentTimeMillis();
  75 + this.loop = loop;
  76 + }
  77 +
  78 + /**
  79 + *
  80 + * 使用不为空的ID,与业务数据保持一个相同的ID
  81 + * <p>
  82 + * 如果为空将会创建新的ID
  83 + *
  84 + * @param id
  85 + * @param remote
  86 + * @param remoteClazz
  87 + * @param remoteMethod
  88 + * @param data
  89 + * @param loop
  90 + */
  91 + public ChannelData(String id, String remote, String remoteClazz, String remoteMethod, String data, boolean loop) {
  92 + this.id = StringUtils.isBlank(id) ? StrUtils.uuid() : id;
68 93 this.remote = remote;
69 94 this.remoteClazz = remoteClazz;
70 95 this.remoteMethod = remoteMethod;
hospital.web/src/main/java/com/lyms/hospital/controller/RouterController.java View file @ 4e92245
... ... @@ -9,7 +9,7 @@
9 9 import org.springframework.web.bind.annotation.RequestMapping;
10 10 import org.springframework.web.bind.annotation.RestController;
11 11  
12   -import com.lyms.sycn.ParamsHandler;
  12 +import com.lyms.sycn.ParamsAdpter;
13 13 import com.lyms.sycn.SycnHandler;
14 14 import com.lyms.sycn.SycnUtils;
15 15 import com.lyms.sycn.channel.ChannelData;
... ... @@ -34,7 +34,7 @@
34 34 if (data.getLoop()) {
35 35 data = (ChannelData) handResult;
36 36 } else {
37   - data.setData(ParamsHandler.builder().push(handResult).toJsonString());
  37 + data.setData(ParamsAdpter.builder().push(handResult).toJsonString());
38 38 }
39 39 data.setAck(true);
40 40 return data;