Commit d2aa74b31eb28507a99fbfc37fbcfadd86394905
1 parent
5836d2b0f0
Exists in
master
修改文件名称,包名称
Showing 22 changed files with 787 additions and 769 deletions
- center.manager/src/test/java/center/manager/test/user/WorkTest.java
- core.sdk/src/main/java/com/lyms/sycn/ParamsHandler.java
- core.sdk/src/main/java/com/lyms/sycn/SycnCallback.java
- core.sdk/src/main/java/com/lyms/sycn/SycnCenter.java
- core.sdk/src/main/java/com/lyms/sycn/SycnHandler.java
- core.sdk/src/main/java/com/lyms/sycn/SycnUtils.java
- core.sdk/src/main/java/com/lyms/sycn/channel/ChannelData.java
- core.sdk/src/main/java/com/lyms/sycn/channel/package-info.java
- core.sdk/src/main/java/com/lyms/sycn/package-info.java
- core.sdk/src/main/java/com/lyms/sycn/queue/SycnQueue.java
- core.sdk/src/main/java/com/lyms/sycn/queue/package-info.java
- core.sdk/src/main/java/com/lyms/synch/ParamsHandler.java
- core.sdk/src/main/java/com/lyms/synch/SycnHandler.java
- core.sdk/src/main/java/com/lyms/synch/SyncCallback.java
- core.sdk/src/main/java/com/lyms/synch/SyncUtils.java
- core.sdk/src/main/java/com/lyms/synch/SyncnCenter.java
- core.sdk/src/main/java/com/lyms/synch/entity/ChannelData.java
- core.sdk/src/main/java/com/lyms/synch/entity/package-info.java
- core.sdk/src/main/java/com/lyms/synch/package-info.java
- core.sdk/src/main/java/com/lyms/synch/queue/SyncnQueue.java
- core.sdk/src/main/java/com/lyms/synch/queue/package-info.java
- core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java
center.manager/src/test/java/center/manager/test/user/WorkTest.java
View file @
d2aa74b
core.sdk/src/main/java/com/lyms/sycn/ParamsHandler.java
View file @
d2aa74b
| 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/SycnCallback.java
View file @
d2aa74b
core.sdk/src/main/java/com/lyms/sycn/SycnCenter.java
View file @
d2aa74b
| 1 | +package com.lyms.sycn; | |
| 2 | + | |
| 3 | +import java.util.concurrent.Executors; | |
| 4 | +import java.util.concurrent.ScheduledExecutorService; | |
| 5 | + | |
| 6 | +import com.lyms.sycn.channel.ChannelData; | |
| 7 | +import com.lyms.sycn.queue.SycnQueue; | |
| 8 | +import com.lyms.util.HttpUtils; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * <li>@ClassName: SyncnCenter | |
| 12 | + * <li>@Description: 数据同步中心,使用单例模式创建一个中心,中心依赖redis队列 | |
| 13 | + * <li>@author maliang | |
| 14 | + * <li>@date 2017年3月13日 | |
| 15 | + * <li> | |
| 16 | + */ | |
| 17 | +public class SycnCenter { | |
| 18 | + | |
| 19 | + public static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(5); | |
| 20 | + | |
| 21 | + public SycnCenter() { | |
| 22 | + } | |
| 23 | + | |
| 24 | + private static class inner { | |
| 25 | + private static final SycnCenter center = new SycnCenter(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * 数据缓冲队列 | |
| 30 | + */ | |
| 31 | + private SycnQueue queue; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * <li>@Description:创建一个中心 | |
| 35 | + * <li>@return | |
| 36 | + * <li>创建人:maliang | |
| 37 | + * <li>创建时间:2017年3月13日 | |
| 38 | + * <li>修改人: | |
| 39 | + * <li>修改时间: | |
| 40 | + */ | |
| 41 | + public static final SycnCenter install() { | |
| 42 | + return SycnCenter.inner.center; | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * <li>@Description:构建一个工作器 | |
| 47 | + * <li>@return | |
| 48 | + * <li>创建人:maliang | |
| 49 | + * <li>创建时间:2017年3月13日 | |
| 50 | + * <li>修改人: | |
| 51 | + * <li>修改时间: | |
| 52 | + */ | |
| 53 | + public Work buildWork() { | |
| 54 | + return new Work(); | |
| 55 | + } | |
| 56 | + | |
| 57 | + public class Work { | |
| 58 | + | |
| 59 | + public ChannelData data; | |
| 60 | + | |
| 61 | + // public CenterCallback callBack; | |
| 62 | + | |
| 63 | + public Work push(ChannelData data, SycnCallback callBack) { | |
| 64 | + // this.model = model; | |
| 65 | + Boolean tag = queue.push(data); | |
| 66 | + callBack.callBack(tag); | |
| 67 | + return this; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * <li>@Description:固定任务队列 | |
| 72 | + * <p> | |
| 73 | + * 该方法不需要业务自行反压数据 | |
| 74 | + * <li>@param callBack | |
| 75 | + * <li>@return | |
| 76 | + * <li>创建人:maliang | |
| 77 | + * <li>创建时间:2017年3月13日 | |
| 78 | + * <li>修改人: | |
| 79 | + * <li>修改时间: | |
| 80 | + */ | |
| 81 | + public Work pullFix(SycnCallback callBack) { | |
| 82 | + // SyncnModel model = queue.pull(ModelType.GET); | |
| 83 | + ChannelData data = queue.pull(true); | |
| 84 | + this.data = data; | |
| 85 | + callBack.callBack(data); | |
| 86 | + return this; | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * <li>@Description:获取临时任务信息 | |
| 91 | + * <p> | |
| 92 | + * 该方法需要业务自行判断做数据反压 | |
| 93 | + * <li>@param callBack | |
| 94 | + * <li>@return | |
| 95 | + * <li>创建人:maliang | |
| 96 | + * <li>创建时间:2017年3月13日 | |
| 97 | + * <li>修改人: | |
| 98 | + * <li>修改时间: | |
| 99 | + */ | |
| 100 | + public Work pullTmp(SycnCallback callBack) { | |
| 101 | + ChannelData data = queue.pull(false); | |
| 102 | + this.data = data; | |
| 103 | + callBack.callBack(data); | |
| 104 | + return this; | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * <li>@Description:数据反压 | |
| 109 | + * <p> | |
| 110 | + * 针对临时任务队列,如果业务逻辑处理失败,手动反压该数据,下次重新执行 | |
| 111 | + * <li> | |
| 112 | + * <li>创建人:maliang | |
| 113 | + * <li>创建时间:2017年3月13日 | |
| 114 | + * <li>修改人: | |
| 115 | + * <li>修改时间: | |
| 116 | + */ | |
| 117 | + public void backPressure() { | |
| 118 | + if (data == null) | |
| 119 | + return; | |
| 120 | + this.push(data, new SycnCallback() { | |
| 121 | + boolean tag = false; | |
| 122 | + | |
| 123 | + @Override | |
| 124 | + public void callBack(Object object) { | |
| 125 | + tag = (Boolean) object; | |
| 126 | + | |
| 127 | + if (!tag) { | |
| 128 | + backPressure(); | |
| 129 | + } | |
| 130 | + } | |
| 131 | + }); | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * <li>@Description:回调方法,调用远端接口 | |
| 136 | + * <p> | |
| 137 | + * 该方法只有跟 {@link #pullFix(SycnCallback)} {@link #pullTmp(SycnCallback)} | |
| 138 | + * 才有意义 | |
| 139 | + * <li>@return | |
| 140 | + * <li>创建人:maliang | |
| 141 | + * <li>创建时间:2017年3月14日 | |
| 142 | + * <li>修改人: | |
| 143 | + * <li>修改时间: | |
| 144 | + */ | |
| 145 | + public String ack(ChannelData ackData) { | |
| 146 | + if (ackData != null) { | |
| 147 | + HttpUtils.REMOTE.post(ackData); | |
| 148 | + } | |
| 149 | + return null; | |
| 150 | + } | |
| 151 | + | |
| 152 | + } | |
| 153 | + | |
| 154 | + public SycnQueue getQueue() { | |
| 155 | + return queue; | |
| 156 | + } | |
| 157 | + | |
| 158 | + public void setQueue(SycnQueue queue) { | |
| 159 | + this.queue = queue; | |
| 160 | + } | |
| 161 | + | |
| 162 | +} |
core.sdk/src/main/java/com/lyms/sycn/SycnHandler.java
View file @
d2aa74b
| 1 | +package com.lyms.sycn; | |
| 2 | + | |
| 3 | +import java.lang.reflect.InvocationTargetException; | |
| 4 | +import java.lang.reflect.Method; | |
| 5 | +import java.util.List; | |
| 6 | + | |
| 7 | +import org.apache.commons.lang3.StringUtils; | |
| 8 | +import org.slf4j.Logger; | |
| 9 | +import org.slf4j.LoggerFactory; | |
| 10 | + | |
| 11 | +import com.lyms.spring.SpringContextHolder; | |
| 12 | +import com.lyms.sycn.ParamsHandler.Param; | |
| 13 | +import com.lyms.sycn.channel.ChannelData; | |
| 14 | +import com.lyms.util.JsonUtils; | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * | |
| 18 | + * <li>@ClassName: SyncHandler | |
| 19 | + * <li>@Description: 处理器 | |
| 20 | + * <li>@author maliang | |
| 21 | + * <li>@date 2017年3月15日 | |
| 22 | + * <li> | |
| 23 | + */ | |
| 24 | +public class SycnHandler { | |
| 25 | + | |
| 26 | + private static Logger LOG = LoggerFactory.getLogger(SycnHandler.class); | |
| 27 | + | |
| 28 | + public static Object handler(ChannelData data) { | |
| 29 | + try { | |
| 30 | + return invoke(data); | |
| 31 | + } catch (Exception e) { | |
| 32 | + LOG.error("执行方法错误: {}", e); | |
| 33 | + return false; | |
| 34 | + } | |
| 35 | + } | |
| 36 | + | |
| 37 | + private static Object invoke(ChannelData data) | |
| 38 | + throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { | |
| 39 | + if (data == null) | |
| 40 | + return null; | |
| 41 | + | |
| 42 | + Class<?> clazz = getClazz(data.getRemoteClazz()); | |
| 43 | + if (clazz == null) | |
| 44 | + return null; | |
| 45 | + Object tagetObject = getObject(clazz); | |
| 46 | + Method method = getMethod(clazz, data.getRemoteMethod()); | |
| 47 | + | |
| 48 | + return excute(tagetObject, method, data.getData()); | |
| 49 | + } | |
| 50 | + | |
| 51 | + private static Object excute(Object object, Method method, String params) | |
| 52 | + throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException { | |
| 53 | + if (method == null || object == null) | |
| 54 | + return null; | |
| 55 | + Object[] objs = null; | |
| 56 | + if (StringUtils.isNotBlank(params)) { | |
| 57 | + ParamsHandler handler = JsonUtils.jsonToBean(params, ParamsHandler.class); | |
| 58 | + if (handler != null) { | |
| 59 | + List<Param> ps = handler.getParams(); | |
| 60 | + objs = new Object[ps.size()]; | |
| 61 | + if (ps != null && !ps.isEmpty()) { | |
| 62 | + int i = 0; | |
| 63 | + for (Param p : ps) { | |
| 64 | + String clazz = p.getClassName(); | |
| 65 | + String value = p.getObj().toString(); | |
| 66 | + if (value.equalsIgnoreCase("null")) { | |
| 67 | + objs[i] = null; | |
| 68 | + } else { | |
| 69 | + // 做数据类型匹配,尽量多的排除基本类型 | |
| 70 | + // 针对业务出现的数据类型做处理 TODO | |
| 71 | + if (clazz.equals(String.class.getName()) || clazz.equals(Integer.class.getName()) | |
| 72 | + || clazz.equals(Long.class.getName()) || clazz.equals(Boolean.class.getName())) { | |
| 73 | + objs[i] = value; | |
| 74 | + } else { | |
| 75 | + Object tmp = JsonUtils.jsonToBean(value, getClazz(clazz)); | |
| 76 | + objs[i] = tmp; | |
| 77 | + } | |
| 78 | + | |
| 79 | + } | |
| 80 | + i++; | |
| 81 | + } | |
| 82 | + } | |
| 83 | + | |
| 84 | + } | |
| 85 | + } | |
| 86 | + // System.out.println(ToStringBuilder.reflectionToString(objs)); | |
| 87 | + return method.invoke(object, objs); | |
| 88 | + | |
| 89 | + } | |
| 90 | + | |
| 91 | + private static Class<?> getClazz(String clazz) throws ClassNotFoundException { | |
| 92 | + if (StringUtils.isNotBlank(clazz)) { | |
| 93 | + return Class.forName(clazz); | |
| 94 | + } | |
| 95 | + return null; | |
| 96 | + } | |
| 97 | + | |
| 98 | + private static Object getObject(Class<?> clazz) { | |
| 99 | + return SpringContextHolder.getBean(clazz); | |
| 100 | + } | |
| 101 | + | |
| 102 | + private static Method getMethod(Class<?> clazz, String methodName) { | |
| 103 | + for (Method method : clazz.getDeclaredMethods()) { | |
| 104 | + if (method.getName().equalsIgnoreCase(methodName)) { | |
| 105 | + return method; | |
| 106 | + } | |
| 107 | + } | |
| 108 | + return null; | |
| 109 | + } | |
| 110 | + | |
| 111 | +} |
core.sdk/src/main/java/com/lyms/sycn/SycnUtils.java
View file @
d2aa74b
| 1 | +package com.lyms.sycn; | |
| 2 | + | |
| 3 | +import java.io.ObjectInputStream; | |
| 4 | + | |
| 5 | +import javax.servlet.http.HttpServletRequest; | |
| 6 | + | |
| 7 | +import com.lyms.sycn.channel.ChannelData; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * <li>@ClassName: SyncUtils | |
| 11 | + * <li>@Description: 同步辅助处理类 | |
| 12 | + * <li>@author maliang | |
| 13 | + * <li>@date 2017年3月14日 | |
| 14 | + * <li> | |
| 15 | + */ | |
| 16 | +public class SycnUtils { | |
| 17 | + | |
| 18 | + public static ChannelData conver(HttpServletRequest request) { | |
| 19 | + ChannelData model = ChannelData.emtpy(); | |
| 20 | + try { | |
| 21 | + request.setCharacterEncoding("utf-8"); | |
| 22 | + ObjectInputStream dis = new ObjectInputStream(request.getInputStream()); | |
| 23 | + model = (ChannelData) dis.readObject(); | |
| 24 | + } catch (Exception e) { | |
| 25 | + e.printStackTrace(); | |
| 26 | + } | |
| 27 | + return model; | |
| 28 | + } | |
| 29 | + | |
| 30 | +} |
core.sdk/src/main/java/com/lyms/sycn/channel/ChannelData.java
View file @
d2aa74b
| 1 | +package com.lyms.sycn.channel; | |
| 2 | + | |
| 3 | +import java.io.Serializable; | |
| 4 | + | |
| 5 | +import com.lyms.util.JsonUtils; | |
| 6 | +import com.lyms.util.StrUtils; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * <li>@ClassName: ChannelData | |
| 10 | + * <li>@Description: 通道实体 | |
| 11 | + * <li>@author maliang | |
| 12 | + * <li>@date 2017年3月15日 | |
| 13 | + * <li> | |
| 14 | + */ | |
| 15 | +public class ChannelData implements Serializable { | |
| 16 | + | |
| 17 | + /** | |
| 18 | + */ | |
| 19 | + private static final long serialVersionUID = -4448732709523865922L; | |
| 20 | + | |
| 21 | + public ChannelData() { | |
| 22 | + this.ts = System.currentTimeMillis(); | |
| 23 | + }; | |
| 24 | + | |
| 25 | + public static ChannelData emtpy() { | |
| 26 | + return new ChannelData(); | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * 数据传输ID | |
| 31 | + */ | |
| 32 | + private String id; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * remote address | |
| 36 | + */ | |
| 37 | + private String remote; | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * remote class name | |
| 41 | + */ | |
| 42 | + private String remoteClazz; | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * remote method name | |
| 46 | + */ | |
| 47 | + private String remoteMethod; | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * remote body json | |
| 51 | + */ | |
| 52 | + private String data; | |
| 53 | + | |
| 54 | + private Boolean loop = Boolean.FALSE; | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * 是否回执信息 | |
| 58 | + */ | |
| 59 | + private Boolean ack = Boolean.FALSE; | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * 创建时间 | |
| 63 | + */ | |
| 64 | + private Long ts; | |
| 65 | + | |
| 66 | + public ChannelData(String remote, String remoteClazz, String remoteMethod, String data, boolean loop) { | |
| 67 | + this.id = StrUtils.uuid(); | |
| 68 | + this.remote = remote; | |
| 69 | + this.remoteClazz = remoteClazz; | |
| 70 | + this.remoteMethod = remoteMethod; | |
| 71 | + this.data = data; | |
| 72 | + this.ts = System.currentTimeMillis(); | |
| 73 | + this.loop = loop; | |
| 74 | + } | |
| 75 | + | |
| 76 | + public String getId() { | |
| 77 | + return id; | |
| 78 | + } | |
| 79 | + | |
| 80 | + public void setId(String id) { | |
| 81 | + this.id = id; | |
| 82 | + } | |
| 83 | + | |
| 84 | + public String getRemote() { | |
| 85 | + return remote; | |
| 86 | + } | |
| 87 | + | |
| 88 | + public void setRemote(String remote) { | |
| 89 | + this.remote = remote; | |
| 90 | + } | |
| 91 | + | |
| 92 | + public String getRemoteClazz() { | |
| 93 | + return remoteClazz; | |
| 94 | + } | |
| 95 | + | |
| 96 | + public void setRemoteClazz(String remoteClazz) { | |
| 97 | + this.remoteClazz = remoteClazz; | |
| 98 | + } | |
| 99 | + | |
| 100 | + public String getRemoteMethod() { | |
| 101 | + return remoteMethod; | |
| 102 | + } | |
| 103 | + | |
| 104 | + public void setRemoteMethod(String remoteMethod) { | |
| 105 | + this.remoteMethod = remoteMethod; | |
| 106 | + } | |
| 107 | + | |
| 108 | + public String getData() { | |
| 109 | + return data; | |
| 110 | + } | |
| 111 | + | |
| 112 | + public void setData(String data) { | |
| 113 | + this.data = data; | |
| 114 | + } | |
| 115 | + | |
| 116 | + public Long getTs() { | |
| 117 | + return ts; | |
| 118 | + } | |
| 119 | + | |
| 120 | + public void setTs(Long ts) { | |
| 121 | + this.ts = ts; | |
| 122 | + } | |
| 123 | + | |
| 124 | + public Boolean getLoop() { | |
| 125 | + return loop; | |
| 126 | + } | |
| 127 | + | |
| 128 | + public void setLoop(Boolean loop) { | |
| 129 | + this.loop = loop; | |
| 130 | + } | |
| 131 | + | |
| 132 | + public Boolean getAck() { | |
| 133 | + return ack; | |
| 134 | + } | |
| 135 | + | |
| 136 | + public void setAck(Boolean ack) { | |
| 137 | + this.ack = ack; | |
| 138 | + } | |
| 139 | + | |
| 140 | + public String toJsonString() { | |
| 141 | + return JsonUtils.beanToJson(this); | |
| 142 | + } | |
| 143 | + | |
| 144 | +} |
core.sdk/src/main/java/com/lyms/sycn/channel/package-info.java
View file @
d2aa74b
| 1 | +/** | |
| 2 | + * <li>@Title: package-info.java | |
| 3 | + * <li>@Package com.lyms.sycn.channel | |
| 4 | + * <li>@Description: TODO(文件描述) | |
| 5 | + * <li>@author maliang | |
| 6 | + * <li>@date 2017年3月16日 | |
| 7 | + */ | |
| 8 | +/** | |
| 9 | + * <li>@ClassName: package-info | |
| 10 | + * <li>@Description: TODO(类描述) | |
| 11 | + * <li>@author maliang | |
| 12 | + * <li>@date 2017年3月16日 | |
| 13 | + * <li> | |
| 14 | + */ | |
| 15 | +package com.lyms.sycn.channel; |
core.sdk/src/main/java/com/lyms/sycn/package-info.java
View file @
d2aa74b
| 1 | +/** | |
| 2 | + * <li>@Title: package-info.java | |
| 3 | + * <li>@Package com.lyms.sycn | |
| 4 | + * <li>@Description: TODO(文件描述) | |
| 5 | + * <li>@author maliang | |
| 6 | + * <li>@date 2017年3月16日 | |
| 7 | + */ | |
| 8 | +/** | |
| 9 | + * <li>@ClassName: package-info | |
| 10 | + * <li>@Description: TODO(类描述) | |
| 11 | + * <li>@author maliang | |
| 12 | + * <li>@date 2017年3月16日 | |
| 13 | + * <li> | |
| 14 | + */ | |
| 15 | +package com.lyms.sycn; |
core.sdk/src/main/java/com/lyms/sycn/queue/SycnQueue.java
View file @
d2aa74b
| 1 | +package com.lyms.sycn.queue; | |
| 2 | + | |
| 3 | +import java.io.Serializable; | |
| 4 | + | |
| 5 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 6 | + | |
| 7 | +import com.lyms.spring.redis.operation.RedisLock; | |
| 8 | +import com.lyms.sycn.channel.ChannelData; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * <li>@ClassName: SyncnQueue | |
| 12 | + * <li>@Description: 数据队列 | |
| 13 | + * <li>@author maliang | |
| 14 | + * <li>@date 2017年3月13日 | |
| 15 | + * <li> | |
| 16 | + */ | |
| 17 | +public class SycnQueue { | |
| 18 | + | |
| 19 | + public RedisTemplate<Serializable, Serializable> template; | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * 构建临时任务队列,固定任务队列锁 | |
| 23 | + * <p> | |
| 24 | + * 区分锁,避免高并发下锁释放的错误 | |
| 25 | + */ | |
| 26 | + public static RedisLock Fix_Lock; | |
| 27 | + | |
| 28 | + public static RedisLock Tmp_Lock; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * 针对数据获取的时候添加锁 | |
| 32 | + */ | |
| 33 | + private static final String LOCK_TMP_NAME = "TMP_lock"; | |
| 34 | + | |
| 35 | + private static final String LOCK_FIXATION_NAME = "FIXATION_lock"; | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * 等待获取锁的时间5 S | |
| 39 | + */ | |
| 40 | + private static final Long WAIT_LOCK_TIME = 5000L; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * 固定任务 | |
| 44 | + */ | |
| 45 | + private static final String FIXATION_WORK = "FIXATION_WORK"; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * 临时任务 | |
| 49 | + */ | |
| 50 | + private static final String TMP_WORK = "TMP_WORK"; | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * 回执消息任务 | |
| 54 | + */ | |
| 55 | + @Deprecated | |
| 56 | + private static final String ACK_WORK = "ACK_WORK"; | |
| 57 | + | |
| 58 | + public SycnQueue(RedisTemplate<Serializable, Serializable> template) { | |
| 59 | + this.template = template; | |
| 60 | + Fix_Lock = new RedisLock(template); | |
| 61 | + Tmp_Lock = new RedisLock(template); | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * <li>@Description:固定任务 {@link ModelType#GET} | |
| 66 | + * <p> | |
| 67 | + * 固定任务执行效率跟任务链表的长度有关系 | |
| 68 | + * <li>@param model | |
| 69 | + * <li>@return | |
| 70 | + * <li>创建人:maliang | |
| 71 | + * <li>创建时间:2017年3月13日 | |
| 72 | + * <li>修改人: | |
| 73 | + * <li>修改时间: | |
| 74 | + */ | |
| 75 | + private Boolean pushFixationWork(ChannelData data) { | |
| 76 | + Long tag = template.opsForList().leftPush(FIXATION_WORK, data); | |
| 77 | + return tag >= 1; | |
| 78 | + } | |
| 79 | + | |
| 80 | + private ChannelData pullFixationData() { | |
| 81 | + // 添加锁 | |
| 82 | + try { | |
| 83 | + if (Fix_Lock.acquireLock(LOCK_FIXATION_NAME, WAIT_LOCK_TIME)) { | |
| 84 | + ChannelData obj = (ChannelData) template.opsForList().rightPopAndLeftPush(FIXATION_WORK, FIXATION_WORK); | |
| 85 | + return obj; | |
| 86 | + } | |
| 87 | + } catch (Exception e) { | |
| 88 | + e.printStackTrace(); | |
| 89 | + } finally { | |
| 90 | + try { | |
| 91 | + if (Fix_Lock != null) { | |
| 92 | + Fix_Lock.releaseLock(); | |
| 93 | + } | |
| 94 | + } catch (Exception e) { | |
| 95 | + e.printStackTrace(); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + return null; | |
| 99 | + | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * <li>@Description:临时任务 | |
| 104 | + * <p> | |
| 105 | + * {@link ModelType#ADD ModelType#UPDATE ModelType#DEL} | |
| 106 | + * <li>@param model | |
| 107 | + * <li>@return | |
| 108 | + * <li>创建人:maliang | |
| 109 | + * <li>创建时间:2017年3月13日 | |
| 110 | + * <li>修改人: | |
| 111 | + * <li>修改时间: | |
| 112 | + */ | |
| 113 | + private boolean pushTempWork(ChannelData data) { | |
| 114 | + Long tag = template.opsForList().leftPush(TMP_WORK, data); | |
| 115 | + return tag >= 1; | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * <li>@Description:临时队列数据弹出 | |
| 120 | + * <li>@return | |
| 121 | + * <li>创建人:maliang | |
| 122 | + * <li>创建时间:2017年3月13日 | |
| 123 | + * <li>修改人: | |
| 124 | + * <li>修改时间: | |
| 125 | + */ | |
| 126 | + private ChannelData pullTempData() { | |
| 127 | + | |
| 128 | + try { | |
| 129 | + if (Tmp_Lock.acquireLock(LOCK_TMP_NAME, WAIT_LOCK_TIME)) { | |
| 130 | + Serializable object = template.opsForList().rightPop(TMP_WORK); | |
| 131 | + return object != null ? (ChannelData) object : null; | |
| 132 | + } | |
| 133 | + } catch (Exception e) { | |
| 134 | + e.printStackTrace(); | |
| 135 | + } finally { | |
| 136 | + if (Tmp_Lock != null) { | |
| 137 | + try { | |
| 138 | + Tmp_Lock.releaseLock(); | |
| 139 | + } catch (Exception e) { | |
| 140 | + e.printStackTrace(); | |
| 141 | + } | |
| 142 | + } | |
| 143 | + } | |
| 144 | + return null; | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * <li>@Description:将任务信息添加到队列中,任务分为固定任务,临时任务,目前拉取数据的任务为固定任务 | |
| 149 | + * {@link ModelType#GET} | |
| 150 | + * <li>@param model | |
| 151 | + * <li>@return | |
| 152 | + * <li>创建人:maliang | |
| 153 | + * <li>创建时间:2017年3月13日 | |
| 154 | + * <li>修改人: | |
| 155 | + * <li>修改时间: | |
| 156 | + */ | |
| 157 | + public Boolean push(ChannelData data) { | |
| 158 | + if (template == null || data == null) | |
| 159 | + return Boolean.FALSE; | |
| 160 | + // 设置任务添加时间 | |
| 161 | + /* | |
| 162 | + * model.setTs(System.currentTimeMillis()); String modelType = | |
| 163 | + * model.getType(); if (ModelType.isGet(modelType)) { return | |
| 164 | + * this.pushFixationWork(model); } else { return | |
| 165 | + * this.pushTempWork(model); } | |
| 166 | + */ | |
| 167 | + boolean loop = data.getLoop(); | |
| 168 | + return loop ? this.pushFixationWork(data) : this.pushTempWork(data); | |
| 169 | + } | |
| 170 | + | |
| 171 | + /** | |
| 172 | + * <li>@Description:获取队列中的数据 | |
| 173 | + * <p> | |
| 174 | + * <li>@param type | |
| 175 | + * <li>@return | |
| 176 | + * <li>创建人:maliang | |
| 177 | + * <li>创建时间:2017年3月13日 | |
| 178 | + * <li>修改人: | |
| 179 | + * <li>修改时间: | |
| 180 | + */ | |
| 181 | + public ChannelData pull(boolean loop) { | |
| 182 | + // boolean loop = data != null && data.getLoop(); | |
| 183 | + return loop ? this.pullFixationData() : this.pullTempData(); | |
| 184 | + /* | |
| 185 | + * if (ModelType.isGet(type)) { return this.pullFixationModel(); } else | |
| 186 | + * { return this.pullTempModel(); } | |
| 187 | + */ | |
| 188 | + } | |
| 189 | + | |
| 190 | + public RedisTemplate<Serializable, Serializable> getTemplate() { | |
| 191 | + return template; | |
| 192 | + } | |
| 193 | + | |
| 194 | + public void setTemplate(RedisTemplate<Serializable, Serializable> template) { | |
| 195 | + this.template = template; | |
| 196 | + } | |
| 197 | + | |
| 198 | +} |
core.sdk/src/main/java/com/lyms/sycn/queue/package-info.java
View file @
d2aa74b
| 1 | +/** | |
| 2 | + * <li>@Title: package-info.java | |
| 3 | + * <li>@Package com.lyms.sycn.queue | |
| 4 | + * <li>@Description: TODO(文件描述) | |
| 5 | + * <li>@author maliang | |
| 6 | + * <li>@date 2017年3月16日 | |
| 7 | + */ | |
| 8 | +/** | |
| 9 | + * <li>@ClassName: package-info | |
| 10 | + * <li>@Description: TODO(类描述) | |
| 11 | + * <li>@author maliang | |
| 12 | + * <li>@date 2017年3月16日 | |
| 13 | + * <li> | |
| 14 | + */ | |
| 15 | +package com.lyms.sycn.queue; |
core.sdk/src/main/java/com/lyms/synch/ParamsHandler.java
View file @
d2aa74b
| 1 | -package com.lyms.synch; | |
| 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/synch/SycnHandler.java
View file @
d2aa74b
| 1 | -package com.lyms.synch; | |
| 2 | - | |
| 3 | -import java.lang.reflect.InvocationTargetException; | |
| 4 | -import java.lang.reflect.Method; | |
| 5 | -import java.util.List; | |
| 6 | - | |
| 7 | -import org.apache.commons.lang3.StringUtils; | |
| 8 | -import org.slf4j.Logger; | |
| 9 | -import org.slf4j.LoggerFactory; | |
| 10 | - | |
| 11 | -import com.lyms.spring.SpringContextHolder; | |
| 12 | -import com.lyms.synch.ParamsHandler.Param; | |
| 13 | -import com.lyms.synch.entity.ChannelData; | |
| 14 | -import com.lyms.util.JsonUtils; | |
| 15 | - | |
| 16 | -/** | |
| 17 | - * | |
| 18 | - * <li>@ClassName: SyncHandler | |
| 19 | - * <li>@Description: 处理器 | |
| 20 | - * <li>@author maliang | |
| 21 | - * <li>@date 2017年3月15日 | |
| 22 | - * <li> | |
| 23 | - */ | |
| 24 | -public class SycnHandler { | |
| 25 | - | |
| 26 | - private static Logger LOG = LoggerFactory.getLogger(SycnHandler.class); | |
| 27 | - | |
| 28 | - public static Object handler(ChannelData data) { | |
| 29 | - try { | |
| 30 | - return invoke(data); | |
| 31 | - } catch (Exception e) { | |
| 32 | - LOG.error("执行方法错误: {}", e); | |
| 33 | - return false; | |
| 34 | - } | |
| 35 | - } | |
| 36 | - | |
| 37 | - private static Object invoke(ChannelData data) | |
| 38 | - throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { | |
| 39 | - if (data == null) | |
| 40 | - return null; | |
| 41 | - | |
| 42 | - Class<?> clazz = getClazz(data.getRemoteClazz()); | |
| 43 | - if (clazz == null) | |
| 44 | - return null; | |
| 45 | - Object tagetObject = getObject(clazz); | |
| 46 | - Method method = getMethod(clazz, data.getRemoteMethod()); | |
| 47 | - | |
| 48 | - return excute(tagetObject, method, data.getData()); | |
| 49 | - } | |
| 50 | - | |
| 51 | - private static Object excute(Object object, Method method, String params) | |
| 52 | - throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException { | |
| 53 | - if (method == null || object == null) | |
| 54 | - return null; | |
| 55 | - Object[] objs = null; | |
| 56 | - if (StringUtils.isNotBlank(params)) { | |
| 57 | - ParamsHandler handler = JsonUtils.jsonToBean(params, ParamsHandler.class); | |
| 58 | - if (handler != null) { | |
| 59 | - List<Param> ps = handler.getParams(); | |
| 60 | - objs = new Object[ps.size()]; | |
| 61 | - if (ps != null && !ps.isEmpty()) { | |
| 62 | - int i = 0; | |
| 63 | - for (Param p : ps) { | |
| 64 | - String clazz = p.getClassName(); | |
| 65 | - String value = p.getObj().toString(); | |
| 66 | - if (value.equalsIgnoreCase("null")) { | |
| 67 | - objs[i] = null; | |
| 68 | - } else { | |
| 69 | - // 做数据类型匹配,尽量多的排除基本类型 | |
| 70 | - // 针对业务出现的数据类型做处理 TODO | |
| 71 | - if (clazz.equals(String.class.getName()) || clazz.equals(Integer.class.getName()) | |
| 72 | - || clazz.equals(Long.class.getName()) || clazz.equals(Boolean.class.getName())) { | |
| 73 | - objs[i] = value; | |
| 74 | - } else { | |
| 75 | - Object tmp = JsonUtils.jsonToBean(value, getClazz(clazz)); | |
| 76 | - objs[i] = tmp; | |
| 77 | - } | |
| 78 | - | |
| 79 | - } | |
| 80 | - i++; | |
| 81 | - } | |
| 82 | - } | |
| 83 | - | |
| 84 | - } | |
| 85 | - } | |
| 86 | - // System.out.println(ToStringBuilder.reflectionToString(objs)); | |
| 87 | - return method.invoke(object, objs); | |
| 88 | - | |
| 89 | - } | |
| 90 | - | |
| 91 | - private static Class<?> getClazz(String clazz) throws ClassNotFoundException { | |
| 92 | - if (StringUtils.isNotBlank(clazz)) { | |
| 93 | - return Class.forName(clazz); | |
| 94 | - } | |
| 95 | - return null; | |
| 96 | - } | |
| 97 | - | |
| 98 | - private static Object getObject(Class<?> clazz) { | |
| 99 | - return SpringContextHolder.getBean(clazz); | |
| 100 | - } | |
| 101 | - | |
| 102 | - private static Method getMethod(Class<?> clazz, String methodName) { | |
| 103 | - for (Method method : clazz.getDeclaredMethods()) { | |
| 104 | - if (method.getName().equalsIgnoreCase(methodName)) { | |
| 105 | - return method; | |
| 106 | - } | |
| 107 | - } | |
| 108 | - return null; | |
| 109 | - } | |
| 110 | - | |
| 111 | -} |
core.sdk/src/main/java/com/lyms/synch/SyncCallback.java
View file @
d2aa74b
core.sdk/src/main/java/com/lyms/synch/SyncUtils.java
View file @
d2aa74b
| 1 | -package com.lyms.synch; | |
| 2 | - | |
| 3 | -import java.io.ObjectInputStream; | |
| 4 | - | |
| 5 | -import javax.servlet.http.HttpServletRequest; | |
| 6 | - | |
| 7 | -import com.lyms.synch.entity.ChannelData; | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * <li>@ClassName: SyncUtils | |
| 11 | - * <li>@Description: 同步辅助处理类 | |
| 12 | - * <li>@author maliang | |
| 13 | - * <li>@date 2017年3月14日 | |
| 14 | - * <li> | |
| 15 | - */ | |
| 16 | -public class SyncUtils { | |
| 17 | - | |
| 18 | - public static ChannelData conver(HttpServletRequest request) { | |
| 19 | - ChannelData model = ChannelData.emtpy(); | |
| 20 | - try { | |
| 21 | - request.setCharacterEncoding("utf-8"); | |
| 22 | - ObjectInputStream dis = new ObjectInputStream(request.getInputStream()); | |
| 23 | - model = (ChannelData) dis.readObject(); | |
| 24 | - } catch (Exception e) { | |
| 25 | - e.printStackTrace(); | |
| 26 | - } | |
| 27 | - return model; | |
| 28 | - } | |
| 29 | - | |
| 30 | -} |
core.sdk/src/main/java/com/lyms/synch/SyncnCenter.java
View file @
d2aa74b
| 1 | -package com.lyms.synch; | |
| 2 | - | |
| 3 | -import java.util.concurrent.Executors; | |
| 4 | -import java.util.concurrent.ScheduledExecutorService; | |
| 5 | - | |
| 6 | -import com.lyms.synch.entity.ChannelData; | |
| 7 | -import com.lyms.synch.queue.SyncnQueue; | |
| 8 | -import com.lyms.util.HttpUtils; | |
| 9 | - | |
| 10 | -/** | |
| 11 | - * <li>@ClassName: SyncnCenter | |
| 12 | - * <li>@Description: 数据同步中心,使用单例模式创建一个中心,中心依赖redis队列 | |
| 13 | - * <li>@author maliang | |
| 14 | - * <li>@date 2017年3月13日 | |
| 15 | - * <li> | |
| 16 | - */ | |
| 17 | -public class SyncnCenter { | |
| 18 | - | |
| 19 | - public static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(5); | |
| 20 | - | |
| 21 | - public SyncnCenter() { | |
| 22 | - } | |
| 23 | - | |
| 24 | - private static class inner { | |
| 25 | - private static final SyncnCenter center = new SyncnCenter(); | |
| 26 | - } | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * 数据缓冲队列 | |
| 30 | - */ | |
| 31 | - private SyncnQueue queue; | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * <li>@Description:创建一个中心 | |
| 35 | - * <li>@return | |
| 36 | - * <li>创建人:maliang | |
| 37 | - * <li>创建时间:2017年3月13日 | |
| 38 | - * <li>修改人: | |
| 39 | - * <li>修改时间: | |
| 40 | - */ | |
| 41 | - public static final SyncnCenter install() { | |
| 42 | - return SyncnCenter.inner.center; | |
| 43 | - } | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * <li>@Description:构建一个工作器 | |
| 47 | - * <li>@return | |
| 48 | - * <li>创建人:maliang | |
| 49 | - * <li>创建时间:2017年3月13日 | |
| 50 | - * <li>修改人: | |
| 51 | - * <li>修改时间: | |
| 52 | - */ | |
| 53 | - public Work buildWork() { | |
| 54 | - return new Work(); | |
| 55 | - } | |
| 56 | - | |
| 57 | - public class Work { | |
| 58 | - | |
| 59 | - public ChannelData data; | |
| 60 | - | |
| 61 | - // public CenterCallback callBack; | |
| 62 | - | |
| 63 | - public Work push(ChannelData data, SyncCallback callBack) { | |
| 64 | - // this.model = model; | |
| 65 | - Boolean tag = queue.push(data); | |
| 66 | - callBack.callBack(tag); | |
| 67 | - return this; | |
| 68 | - } | |
| 69 | - | |
| 70 | - /** | |
| 71 | - * <li>@Description:固定任务队列 | |
| 72 | - * <p> | |
| 73 | - * 该方法不需要业务自行反压数据 | |
| 74 | - * <li>@param callBack | |
| 75 | - * <li>@return | |
| 76 | - * <li>创建人:maliang | |
| 77 | - * <li>创建时间:2017年3月13日 | |
| 78 | - * <li>修改人: | |
| 79 | - * <li>修改时间: | |
| 80 | - */ | |
| 81 | - public Work pullFix(SyncCallback callBack) { | |
| 82 | - // SyncnModel model = queue.pull(ModelType.GET); | |
| 83 | - ChannelData data = queue.pull(true); | |
| 84 | - this.data = data; | |
| 85 | - callBack.callBack(data); | |
| 86 | - return this; | |
| 87 | - } | |
| 88 | - | |
| 89 | - /** | |
| 90 | - * <li>@Description:获取临时任务信息 | |
| 91 | - * <p> | |
| 92 | - * 该方法需要业务自行判断做数据反压 | |
| 93 | - * <li>@param callBack | |
| 94 | - * <li>@return | |
| 95 | - * <li>创建人:maliang | |
| 96 | - * <li>创建时间:2017年3月13日 | |
| 97 | - * <li>修改人: | |
| 98 | - * <li>修改时间: | |
| 99 | - */ | |
| 100 | - public Work pullTmp(SyncCallback callBack) { | |
| 101 | - ChannelData data = queue.pull(false); | |
| 102 | - this.data = data; | |
| 103 | - callBack.callBack(data); | |
| 104 | - return this; | |
| 105 | - } | |
| 106 | - | |
| 107 | - /** | |
| 108 | - * <li>@Description:数据反压 | |
| 109 | - * <p> | |
| 110 | - * 针对临时任务队列,如果业务逻辑处理失败,手动反压该数据,下次重新执行 | |
| 111 | - * <li> | |
| 112 | - * <li>创建人:maliang | |
| 113 | - * <li>创建时间:2017年3月13日 | |
| 114 | - * <li>修改人: | |
| 115 | - * <li>修改时间: | |
| 116 | - */ | |
| 117 | - public void backPressure() { | |
| 118 | - if (data == null) | |
| 119 | - return; | |
| 120 | - this.push(data, new SyncCallback() { | |
| 121 | - boolean tag = false; | |
| 122 | - | |
| 123 | - @Override | |
| 124 | - public void callBack(Object object) { | |
| 125 | - tag = (Boolean) object; | |
| 126 | - | |
| 127 | - if (!tag) { | |
| 128 | - backPressure(); | |
| 129 | - } | |
| 130 | - } | |
| 131 | - }); | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * <li>@Description:回调方法,调用远端接口 | |
| 136 | - * <p> | |
| 137 | - * 该方法只有跟 {@link #pullFix(SyncCallback)} {@link #pullTmp(SyncCallback)} | |
| 138 | - * 才有意义 | |
| 139 | - * <li>@return | |
| 140 | - * <li>创建人:maliang | |
| 141 | - * <li>创建时间:2017年3月14日 | |
| 142 | - * <li>修改人: | |
| 143 | - * <li>修改时间: | |
| 144 | - */ | |
| 145 | - public String ack(ChannelData ackData) { | |
| 146 | - if (ackData != null) { | |
| 147 | - HttpUtils.REMOTE.post(ackData); | |
| 148 | - } | |
| 149 | - return null; | |
| 150 | - } | |
| 151 | - | |
| 152 | - } | |
| 153 | - | |
| 154 | - public SyncnQueue getQueue() { | |
| 155 | - return queue; | |
| 156 | - } | |
| 157 | - | |
| 158 | - public void setQueue(SyncnQueue queue) { | |
| 159 | - this.queue = queue; | |
| 160 | - } | |
| 161 | - | |
| 162 | -} |
core.sdk/src/main/java/com/lyms/synch/entity/ChannelData.java
View file @
d2aa74b
| 1 | -package com.lyms.synch.entity; | |
| 2 | - | |
| 3 | -import java.io.Serializable; | |
| 4 | - | |
| 5 | -import com.lyms.util.JsonUtils; | |
| 6 | -import com.lyms.util.StrUtils; | |
| 7 | - | |
| 8 | -/** | |
| 9 | - * <li>@ClassName: ChannelData | |
| 10 | - * <li>@Description: 通道实体 | |
| 11 | - * <li>@author maliang | |
| 12 | - * <li>@date 2017年3月15日 | |
| 13 | - * <li> | |
| 14 | - */ | |
| 15 | -public class ChannelData implements Serializable { | |
| 16 | - | |
| 17 | - /** | |
| 18 | - */ | |
| 19 | - private static final long serialVersionUID = -4448732709523865922L; | |
| 20 | - | |
| 21 | - public ChannelData() { | |
| 22 | - }; | |
| 23 | - | |
| 24 | - public static ChannelData emtpy() { | |
| 25 | - return new ChannelData(); | |
| 26 | - } | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * 数据传输ID | |
| 30 | - */ | |
| 31 | - private String id; | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * remote address | |
| 35 | - */ | |
| 36 | - private String remote; | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * remote class name | |
| 40 | - */ | |
| 41 | - private String remoteClazz; | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * remote method name | |
| 45 | - */ | |
| 46 | - private String remoteMethod; | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * remote body json | |
| 50 | - */ | |
| 51 | - private String data; | |
| 52 | - | |
| 53 | - private Boolean loop = Boolean.FALSE; | |
| 54 | - | |
| 55 | - /** | |
| 56 | - * 是否回执信息 | |
| 57 | - */ | |
| 58 | - private Boolean ack = Boolean.FALSE; | |
| 59 | - | |
| 60 | - /** | |
| 61 | - * 创建时间 | |
| 62 | - */ | |
| 63 | - private Long ts; | |
| 64 | - | |
| 65 | - public ChannelData(String remote, String remoteClazz, String remoteMethod, String data, boolean loop) { | |
| 66 | - this.id = StrUtils.uuid(); | |
| 67 | - this.remote = remote; | |
| 68 | - this.remoteClazz = remoteClazz; | |
| 69 | - this.remoteMethod = remoteMethod; | |
| 70 | - this.data = data; | |
| 71 | - this.ts = System.currentTimeMillis(); | |
| 72 | - this.loop = loop; | |
| 73 | - } | |
| 74 | - | |
| 75 | - public String getId() { | |
| 76 | - return id; | |
| 77 | - } | |
| 78 | - | |
| 79 | - public void setId(String id) { | |
| 80 | - this.id = id; | |
| 81 | - } | |
| 82 | - | |
| 83 | - public String getRemote() { | |
| 84 | - return remote; | |
| 85 | - } | |
| 86 | - | |
| 87 | - public void setRemote(String remote) { | |
| 88 | - this.remote = remote; | |
| 89 | - } | |
| 90 | - | |
| 91 | - public String getRemoteClazz() { | |
| 92 | - return remoteClazz; | |
| 93 | - } | |
| 94 | - | |
| 95 | - public void setRemoteClazz(String remoteClazz) { | |
| 96 | - this.remoteClazz = remoteClazz; | |
| 97 | - } | |
| 98 | - | |
| 99 | - public String getRemoteMethod() { | |
| 100 | - return remoteMethod; | |
| 101 | - } | |
| 102 | - | |
| 103 | - public void setRemoteMethod(String remoteMethod) { | |
| 104 | - this.remoteMethod = remoteMethod; | |
| 105 | - } | |
| 106 | - | |
| 107 | - public String getData() { | |
| 108 | - return data; | |
| 109 | - } | |
| 110 | - | |
| 111 | - public void setData(String data) { | |
| 112 | - this.data = data; | |
| 113 | - } | |
| 114 | - | |
| 115 | - public Long getTs() { | |
| 116 | - return ts; | |
| 117 | - } | |
| 118 | - | |
| 119 | - public void setTs(Long ts) { | |
| 120 | - this.ts = ts; | |
| 121 | - } | |
| 122 | - | |
| 123 | - public Boolean getLoop() { | |
| 124 | - return loop; | |
| 125 | - } | |
| 126 | - | |
| 127 | - public void setLoop(Boolean loop) { | |
| 128 | - this.loop = loop; | |
| 129 | - } | |
| 130 | - | |
| 131 | - public Boolean getAck() { | |
| 132 | - return ack; | |
| 133 | - } | |
| 134 | - | |
| 135 | - public void setAck(Boolean ack) { | |
| 136 | - this.ack = ack; | |
| 137 | - } | |
| 138 | - | |
| 139 | - public String toJsonString() { | |
| 140 | - return JsonUtils.beanToJson(this); | |
| 141 | - } | |
| 142 | - | |
| 143 | -} |
core.sdk/src/main/java/com/lyms/synch/entity/package-info.java
View file @
d2aa74b
core.sdk/src/main/java/com/lyms/synch/package-info.java
View file @
d2aa74b
core.sdk/src/main/java/com/lyms/synch/queue/SyncnQueue.java
View file @
d2aa74b
| 1 | -package com.lyms.synch.queue; | |
| 2 | - | |
| 3 | -import java.io.Serializable; | |
| 4 | - | |
| 5 | -import org.springframework.data.redis.core.RedisTemplate; | |
| 6 | - | |
| 7 | -import com.lyms.spring.redis.operation.RedisLock; | |
| 8 | -import com.lyms.synch.entity.ChannelData; | |
| 9 | - | |
| 10 | -/** | |
| 11 | - * <li>@ClassName: SyncnQueue | |
| 12 | - * <li>@Description: 数据队列 | |
| 13 | - * <li>@author maliang | |
| 14 | - * <li>@date 2017年3月13日 | |
| 15 | - * <li> | |
| 16 | - */ | |
| 17 | -public class SyncnQueue { | |
| 18 | - | |
| 19 | - public RedisTemplate<Serializable, Serializable> template; | |
| 20 | - | |
| 21 | - /** | |
| 22 | - * 构建临时任务队列,固定任务队列锁 | |
| 23 | - * <p> | |
| 24 | - * 区分锁,避免高并发下锁释放的错误 | |
| 25 | - */ | |
| 26 | - public static RedisLock Fix_Lock; | |
| 27 | - | |
| 28 | - public static RedisLock Tmp_Lock; | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * 针对数据获取的时候添加锁 | |
| 32 | - */ | |
| 33 | - private static final String LOCK_TMP_NAME = "TMP_lock"; | |
| 34 | - | |
| 35 | - private static final String LOCK_FIXATION_NAME = "FIXATION_lock"; | |
| 36 | - | |
| 37 | - /** | |
| 38 | - * 等待获取锁的时间5 S | |
| 39 | - */ | |
| 40 | - private static final Long WAIT_LOCK_TIME = 5000L; | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * 固定任务 | |
| 44 | - */ | |
| 45 | - private static final String FIXATION_WORK = "FIXATION_WORK"; | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * 临时任务 | |
| 49 | - */ | |
| 50 | - private static final String TMP_WORK = "TMP_WORK"; | |
| 51 | - | |
| 52 | - /** | |
| 53 | - * 回执消息任务 | |
| 54 | - */ | |
| 55 | - @Deprecated | |
| 56 | - private static final String ACK_WORK = "ACK_WORK"; | |
| 57 | - | |
| 58 | - public SyncnQueue(RedisTemplate<Serializable, Serializable> template) { | |
| 59 | - this.template = template; | |
| 60 | - Fix_Lock = new RedisLock(template); | |
| 61 | - Tmp_Lock = new RedisLock(template); | |
| 62 | - } | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * <li>@Description:固定任务 {@link ModelType#GET} | |
| 66 | - * <p> | |
| 67 | - * 固定任务执行效率跟任务链表的长度有关系 | |
| 68 | - * <li>@param model | |
| 69 | - * <li>@return | |
| 70 | - * <li>创建人:maliang | |
| 71 | - * <li>创建时间:2017年3月13日 | |
| 72 | - * <li>修改人: | |
| 73 | - * <li>修改时间: | |
| 74 | - */ | |
| 75 | - private Boolean pushFixationWork(ChannelData data) { | |
| 76 | - Long tag = template.opsForList().leftPush(FIXATION_WORK, data); | |
| 77 | - return tag >= 1; | |
| 78 | - } | |
| 79 | - | |
| 80 | - private ChannelData pullFixationData() { | |
| 81 | - // 添加锁 | |
| 82 | - try { | |
| 83 | - if (Fix_Lock.acquireLock(LOCK_FIXATION_NAME, WAIT_LOCK_TIME)) { | |
| 84 | - ChannelData obj = (ChannelData) template.opsForList().rightPopAndLeftPush(FIXATION_WORK, FIXATION_WORK); | |
| 85 | - return obj; | |
| 86 | - } | |
| 87 | - } catch (Exception e) { | |
| 88 | - e.printStackTrace(); | |
| 89 | - } finally { | |
| 90 | - try { | |
| 91 | - if (Fix_Lock != null) { | |
| 92 | - Fix_Lock.releaseLock(); | |
| 93 | - } | |
| 94 | - } catch (Exception e) { | |
| 95 | - e.printStackTrace(); | |
| 96 | - } | |
| 97 | - } | |
| 98 | - return null; | |
| 99 | - | |
| 100 | - } | |
| 101 | - | |
| 102 | - /** | |
| 103 | - * <li>@Description:临时任务 | |
| 104 | - * <p> | |
| 105 | - * {@link ModelType#ADD ModelType#UPDATE ModelType#DEL} | |
| 106 | - * <li>@param model | |
| 107 | - * <li>@return | |
| 108 | - * <li>创建人:maliang | |
| 109 | - * <li>创建时间:2017年3月13日 | |
| 110 | - * <li>修改人: | |
| 111 | - * <li>修改时间: | |
| 112 | - */ | |
| 113 | - private boolean pushTempWork(ChannelData data) { | |
| 114 | - Long tag = template.opsForList().leftPush(TMP_WORK, data); | |
| 115 | - return tag >= 1; | |
| 116 | - } | |
| 117 | - | |
| 118 | - /** | |
| 119 | - * <li>@Description:临时队列数据弹出 | |
| 120 | - * <li>@return | |
| 121 | - * <li>创建人:maliang | |
| 122 | - * <li>创建时间:2017年3月13日 | |
| 123 | - * <li>修改人: | |
| 124 | - * <li>修改时间: | |
| 125 | - */ | |
| 126 | - private ChannelData pullTempData() { | |
| 127 | - | |
| 128 | - try { | |
| 129 | - if (Tmp_Lock.acquireLock(LOCK_TMP_NAME, WAIT_LOCK_TIME)) { | |
| 130 | - Serializable object = template.opsForList().rightPop(TMP_WORK); | |
| 131 | - return object != null ? (ChannelData) object : null; | |
| 132 | - } | |
| 133 | - } catch (Exception e) { | |
| 134 | - e.printStackTrace(); | |
| 135 | - } finally { | |
| 136 | - if (Tmp_Lock != null) { | |
| 137 | - try { | |
| 138 | - Tmp_Lock.releaseLock(); | |
| 139 | - } catch (Exception e) { | |
| 140 | - e.printStackTrace(); | |
| 141 | - } | |
| 142 | - } | |
| 143 | - } | |
| 144 | - return null; | |
| 145 | - } | |
| 146 | - | |
| 147 | - /** | |
| 148 | - * <li>@Description:将任务信息添加到队列中,任务分为固定任务,临时任务,目前拉取数据的任务为固定任务 | |
| 149 | - * {@link ModelType#GET} | |
| 150 | - * <li>@param model | |
| 151 | - * <li>@return | |
| 152 | - * <li>创建人:maliang | |
| 153 | - * <li>创建时间:2017年3月13日 | |
| 154 | - * <li>修改人: | |
| 155 | - * <li>修改时间: | |
| 156 | - */ | |
| 157 | - public Boolean push(ChannelData data) { | |
| 158 | - if (template == null || data == null) | |
| 159 | - return Boolean.FALSE; | |
| 160 | - // 设置任务添加时间 | |
| 161 | - /* | |
| 162 | - * model.setTs(System.currentTimeMillis()); String modelType = | |
| 163 | - * model.getType(); if (ModelType.isGet(modelType)) { return | |
| 164 | - * this.pushFixationWork(model); } else { return | |
| 165 | - * this.pushTempWork(model); } | |
| 166 | - */ | |
| 167 | - boolean loop = data.getLoop(); | |
| 168 | - return loop ? this.pushFixationWork(data) : this.pushTempWork(data); | |
| 169 | - } | |
| 170 | - | |
| 171 | - /** | |
| 172 | - * <li>@Description:获取队列中的数据 | |
| 173 | - * <p> | |
| 174 | - * <li>@param type | |
| 175 | - * <li>@return | |
| 176 | - * <li>创建人:maliang | |
| 177 | - * <li>创建时间:2017年3月13日 | |
| 178 | - * <li>修改人: | |
| 179 | - * <li>修改时间: | |
| 180 | - */ | |
| 181 | - public ChannelData pull(boolean loop) { | |
| 182 | - // boolean loop = data != null && data.getLoop(); | |
| 183 | - return loop ? this.pullFixationData() : this.pullTempData(); | |
| 184 | - /* | |
| 185 | - * if (ModelType.isGet(type)) { return this.pullFixationModel(); } else | |
| 186 | - * { return this.pullTempModel(); } | |
| 187 | - */ | |
| 188 | - } | |
| 189 | - | |
| 190 | - public RedisTemplate<Serializable, Serializable> getTemplate() { | |
| 191 | - return template; | |
| 192 | - } | |
| 193 | - | |
| 194 | - public void setTemplate(RedisTemplate<Serializable, Serializable> template) { | |
| 195 | - this.template = template; | |
| 196 | - } | |
| 197 | - | |
| 198 | -} |
core.sdk/src/main/java/com/lyms/synch/queue/package-info.java
View file @
d2aa74b
core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java
View file @
d2aa74b