diff --git a/center.manager/src/main/java/com/lyms/cm/controller/sys/SysRemoteController.java b/center.manager/src/main/java/com/lyms/cm/controller/sys/SysRemoteController.java new file mode 100644 index 0000000..a4d09cb --- /dev/null +++ b/center.manager/src/main/java/com/lyms/cm/controller/sys/SysRemoteController.java @@ -0,0 +1,27 @@ +package com.lyms.cm.controller.sys; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.lyms.cm.entity.sys.SysUsers; +import com.lyms.synch.entity.SyncnModel; +import com.lyms.web.controller.BaseController; + +@Controller +@RequestMapping("/remote") +public class SysRemoteController extends BaseController { + + @RequestMapping(value = "/push") + @ResponseBody + public String push(HttpServletRequest request) throws ClassNotFoundException, IOException { + SyncnModel model = conver(request); + SysUsers user = (SysUsers) model.getData(); + System.out.println(user.getName() + " " + model.getMethod()); + return "OK"; + } +} diff --git a/center.manager/src/main/resources/xml/app-shiro.xml b/center.manager/src/main/resources/xml/app-shiro.xml index 6efb25c..ab29c88 100644 --- a/center.manager/src/main/resources/xml/app-shiro.xml +++ b/center.manager/src/main/resources/xml/app-shiro.xml @@ -46,6 +46,7 @@ /login = anon + /remote/** =anon /logout = logout /static/** = anon /authenticated = authc diff --git a/center.manager/src/main/resources/xml/app-syncn.xml b/center.manager/src/main/resources/xml/app-syncn.xml new file mode 100644 index 0000000..49a7fa5 --- /dev/null +++ b/center.manager/src/main/resources/xml/app-syncn.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/center.manager/src/test/java/center/manager/test/user/WorkTest.java b/center.manager/src/test/java/center/manager/test/user/WorkTest.java new file mode 100644 index 0000000..c927da6 --- /dev/null +++ b/center.manager/src/test/java/center/manager/test/user/WorkTest.java @@ -0,0 +1,93 @@ +package center.manager.test.user; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import com.lyms.cm.entity.sys.SysUsers; +import com.lyms.synch.CenterCallback; +import com.lyms.synch.SyncnCenter; +import com.lyms.synch.SyncnCenter.Work; +import com.lyms.synch.entity.ModelType; +import com.lyms.synch.entity.SyncnModel; +import com.lyms.util.HttpUtils; +import com.lyms.util.StrUtils; + +/** + *
  • @ClassName: WorkTest + *
  • @Description: 基于spring 注入方式创建 center + *
  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +public class WorkTest extends BaseTest { + + @Autowired + public SyncnCenter center; + + @Test + public void workTest() { + SyncnModel model = new SyncnModel(); + model.setId(StrUtils.uuid()); + model.setModel("user"); + model.setMethod("post"); + model.setType(ModelType.ADD.name()); + // 推送数据远程地址 + model.setRemote("http://127.0.0.1:9090/center.manager/remote/push"); + // 对于远程获取数据,回调地址 + model.setCallBack("http://www.baidu.com"); + model.setTs(System.currentTimeMillis()); + + SysUsers user = new SysUsers(); + user.setName("乱码测试"); + model.setData(user); + + // 添加任务信息,临时任务信息 + center.buildWork().push(model, new CenterCallback() { + @Override + public void callBack(Object object) { + System.out.println("push callBack : " + object); + } + }); + + // 获取临时任务信息 + // 包含ack 回调方法 + Work work = center.buildWork(); + try { + work.pullTmp(new CenterCallback() { + @Override + public void callBack(Object object) { + // 处理实际业务 + SyncnModel model = (SyncnModel) object; + String result = HttpUtils.REMOTE.post(model); + System.out.println(result); + } + }); + } catch (Exception e) { + e.printStackTrace(); + // 业务逻辑错误,反压数据,将会输重写写入队列 + work.backPressure(); + } + // .ackGet(null); + // System.out.println("callBack: " + get); + + } + + public static void main(String[] args) { + SyncnModel model = new SyncnModel(); + model.setId(StrUtils.uuid()); + model.setModel("user"); + model.setMethod("post"); + model.setType(ModelType.ADD.name()); + // 推送数据远程地址 + model.setRemote("http://127.0.0.1:9090/center.manager/remote/push"); + // 对于远程获取数据,回调地址 + model.setCallBack("http://www.baidu.com"); + model.setTs(System.currentTimeMillis()); + + SysUsers user = new SysUsers(); + user.setName("你好"); + model.setData(user); + String result = HttpUtils.REMOTE.post(model); + System.out.println(result); + } +} diff --git a/core.sdk/src/main/java/com/lyms/spring/redis/operation/RedisLock.java b/core.sdk/src/main/java/com/lyms/spring/redis/operation/RedisLock.java index b76d105..f63e297 100644 --- a/core.sdk/src/main/java/com/lyms/spring/redis/operation/RedisLock.java +++ b/core.sdk/src/main/java/com/lyms/spring/redis/operation/RedisLock.java @@ -41,6 +41,7 @@ public class RedisLock { Long nano = System.nanoTime(); while (System.nanoTime() - nano < TimeUnit.MILLISECONDS.toNanos(waitmill)) { boolean tag = acquireLock(lockName); + // System.out.println("获取锁....."); // 获取到锁资源 if (tag) return true; diff --git a/core.sdk/src/main/java/com/lyms/synch/CenterCallback.java b/core.sdk/src/main/java/com/lyms/synch/CenterCallback.java new file mode 100644 index 0000000..4188de7 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/synch/CenterCallback.java @@ -0,0 +1,5 @@ +package com.lyms.synch; + +public interface CenterCallback { + public void callBack(Object object); +} diff --git a/core.sdk/src/main/java/com/lyms/synch/LinkedWork.java b/core.sdk/src/main/java/com/lyms/synch/LinkedWork.java new file mode 100644 index 0000000..c451929 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/synch/LinkedWork.java @@ -0,0 +1,14 @@ +package com.lyms.synch; + +/** + *
  • @ClassName: LinkedWork + *
  • @Description: 任务链表 + *

    + * 主要解决多任务循环执行的过程 + *

  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +public class LinkedWork { + +} diff --git a/core.sdk/src/main/java/com/lyms/synch/SyncnCenter.java b/core.sdk/src/main/java/com/lyms/synch/SyncnCenter.java new file mode 100644 index 0000000..b235520 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/synch/SyncnCenter.java @@ -0,0 +1,165 @@ +package com.lyms.synch; + +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; + +import org.apache.commons.lang3.StringUtils; + +import com.lyms.synch.entity.ModelType; +import com.lyms.synch.entity.SyncnModel; +import com.lyms.synch.queue.SyncnQueue; +import com.lyms.util.HttpUtils; + +/** + *
  • @ClassName: SyncnCenter + *
  • @Description: 数据同步中心,使用单例模式创建一个中心,中心依赖redis队列 + *
  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +public class SyncnCenter { + + public static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(5); + + public SyncnCenter() { + } + + private static class inner { + private static final SyncnCenter center = new SyncnCenter(); + } + + /** + * 数据缓冲队列 + */ + private SyncnQueue queue; + + /** + *
  • @Description:创建一个中心实力 + *
  • @return + *
  • 创建人:maliang + *
  • 创建时间:2017年3月13日 + *
  • 修改人: + *
  • 修改时间: + */ + public static final SyncnCenter install() { + return SyncnCenter.inner.center; + } + + /** + *
  • @Description:构建一个工作器 + *
  • @return + *
  • 创建人:maliang + *
  • 创建时间:2017年3月13日 + *
  • 修改人: + *
  • 修改时间: + */ + public Work buildWork() { + return new Work(); + } + + public class Work { + + public SyncnModel model; + + // public CenterCallback callBack; + + public Work push(SyncnModel model, CenterCallback callBack) { + this.model = model; + Boolean tag = queue.push(model); + callBack.callBack(tag); + return this; + } + + public Work pullFix(CenterCallback callBack) { + SyncnModel model = queue.pull(ModelType.GET); + this.model = model; + callBack.callBack(model); + return this; + } + + public Work pullTmp(CenterCallback callBack) { + SyncnModel model = queue.pull(null); + this.model = model; + callBack.callBack(model); + return this; + } + + /** + *
  • @Description:数据反压 + *

    + * 针对临时任务队列,如果业务逻辑处理失败,手动反压该数据,下次重新执行 + *

  • + *
  • 创建人:maliang + *
  • 创建时间:2017年3月13日 + *
  • 修改人: + *
  • 修改时间: + */ + public void backPressure() { + this.push(model, new CenterCallback() { + boolean tag = false; + + @Override + public void callBack(Object object) { + tag = (Boolean) object; + + if (!tag) { + backPressure(); + } + } + }); + } + + /** + *
  • @Description:该方法为回执方法,将会调用目标接口,返回提示对应消息处理成功 + *

    + * 使用方式 pull(type,callBack).ackGet(); + *

    + * 目前该方法对PULL 的消息回执才有实际的意义 + *

  • @return + *
  • 创建人:maliang + *
  • 创建时间:2017年3月13日 + *
  • 修改人: + *
  • 修改时间: + */ + public String ackGet(Map params) { + if (model != null && StringUtils.isNotBlank(model.getCallBack())) { + return HttpUtils.REMOTE.get(model.getCallBack(), params); + } + return null; + } + + public String ackPost(Map params) { + if (model != null && StringUtils.isNotBlank(model.getCallBack())) { + return HttpUtils.REMOTE.post(model.getCallBack(), params); + } + return null; + } + + /** + *
  • @Description:该方法主要检测是否重复执行,对应数据 + *

    + * TODO + *

    + * 使用方式 push(model,callBack).validata(); + *

  • @return + *
  • 创建人:maliang + *
  • 创建时间:2017年3月13日 + *
  • 修改人: + *
  • 修改时间: + */ + public Work validata() { + return this; + } + + } + + public SyncnQueue getQueue() { + return queue; + } + + public void setQueue(SyncnQueue queue) { + this.queue = queue; + } + +} diff --git a/core.sdk/src/main/java/com/lyms/synch/entity/Model.java b/core.sdk/src/main/java/com/lyms/synch/entity/Model.java new file mode 100644 index 0000000..8513bb0 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/synch/entity/Model.java @@ -0,0 +1,22 @@ +package com.lyms.synch.entity; + +/** + *
  • @ClassName: Model + *
  • @Description: 模块信息 + *
  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +public enum Model { + USER("用户信息"); + + private String des; + + private Model(String des) { + this.des = des; + } + + public String getDes() { + return this.des; + } +} diff --git a/core.sdk/src/main/java/com/lyms/synch/entity/ModelType.java b/core.sdk/src/main/java/com/lyms/synch/entity/ModelType.java new file mode 100644 index 0000000..9065e9f --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/synch/entity/ModelType.java @@ -0,0 +1,53 @@ +package com.lyms.synch.entity; + +import java.util.EnumSet; + +/** + *
  • @ClassName: ModelType + *
  • @Description: 数据模型类型 + *
  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +public enum ModelType { + + ADD, DEL, UPDATE, GET, ACK; + + public static ModelType exitsType(String typeName) { + for (ModelType modelType : EnumSet.allOf(ModelType.class)) { + if (modelType.name().equalsIgnoreCase(typeName)) { + return modelType; + } + } + return null; + } + + public static boolean isAdd(String type) { + return ADD.equals(exitsType(type)); + } + + public static boolean isACK(String type) { + return ACK.equals(exitsType(type)); + } + + public static boolean isDel(String type) { + return DEL.equals(exitsType(type)); + } + + public static boolean isUpdate(String type) { + return UPDATE.equals(exitsType(type)); + } + + public static boolean isGet(String type) { + return GET.equals(exitsType(type)); + } + + public static boolean isGet(ModelType type) { + return GET.equals(type); + } + + public static void main(String[] args) { + System.out.println(ModelType.isAdd("add")); + } + +} diff --git a/core.sdk/src/main/java/com/lyms/synch/entity/SyncnModel.java b/core.sdk/src/main/java/com/lyms/synch/entity/SyncnModel.java new file mode 100644 index 0000000..25fb765 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/synch/entity/SyncnModel.java @@ -0,0 +1,128 @@ +package com.lyms.synch.entity; + +import java.io.Serializable; + +import com.lyms.util.JsonUtils; + +/** + *
  • @ClassName: SynceModel + *
  • @Description: 数据模型 + *
  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +public class SyncnModel implements Serializable { + + /** + */ + private static final long serialVersionUID = -5121448640215880284L; + + /** + * 消息模型ID + */ + private String id; + + /** + * 远程地址,远程访问的服务地址 + */ + private String remote; + + /** + * 回调地址,获取数据处理的时候该参数才有实际意义 + */ + private String callBack; + + /** + * get | post + */ + private String method; + + /** + * 操作类型 + */ + private String type; + + /** + * 模块 + */ + private String model; + + /** + * 数据信息 + */ + private Object data; + + /** + * 操作时间 + */ + private Long ts; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Long getTs() { + return ts; + } + + public void setTs(Long ts) { + this.ts = ts; + } + + public String getRemote() { + return remote; + } + + public void setRemote(String remote) { + this.remote = remote; + } + + public String getCallBack() { + return callBack; + } + + public void setCallBack(String callBack) { + this.callBack = callBack; + } + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } + + public static SyncnModel conver(String body) { + return JsonUtils.jsonToBean(body, SyncnModel.class); + } + +} diff --git a/core.sdk/src/main/java/com/lyms/synch/entity/package-info.java b/core.sdk/src/main/java/com/lyms/synch/entity/package-info.java new file mode 100644 index 0000000..20d6694 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/synch/entity/package-info.java @@ -0,0 +1,8 @@ +/** + *
  • @ClassName: package-info + *
  • @Description: 数据模型 + *
  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +package com.lyms.synch.entity; \ No newline at end of file diff --git a/core.sdk/src/main/java/com/lyms/synch/package-info.java b/core.sdk/src/main/java/com/lyms/synch/package-info.java new file mode 100644 index 0000000..adae6bc --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/synch/package-info.java @@ -0,0 +1,12 @@ +/** + *
  • @ClassName: package-info + *
  • @Description: 数据同步中心 + *
  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +package com.lyms.synch; + +// SyncnCenter.push(object); +// SyncnCenter.pull(object).ack(); +// SyncnCenter.ack(object); \ No newline at end of file diff --git a/core.sdk/src/main/java/com/lyms/synch/queue/SyncnQueue.java b/core.sdk/src/main/java/com/lyms/synch/queue/SyncnQueue.java new file mode 100644 index 0000000..a3f4355 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/synch/queue/SyncnQueue.java @@ -0,0 +1,199 @@ +package com.lyms.synch.queue; + +import java.io.Serializable; + +import org.springframework.data.redis.core.RedisTemplate; + +import com.lyms.spring.redis.operation.RedisLock; +import com.lyms.synch.entity.ModelType; +import com.lyms.synch.entity.SyncnModel; + +/** + *
  • @ClassName: SyncnQueue + *
  • @Description: 数据队列 + *
  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +public class SyncnQueue { + + public RedisTemplate template; + + /** + * 构建临时任务队列,固定任务队列锁 + *

    + * 区分锁,避免高并发下锁释放的错误 + */ + public static RedisLock Fix_Lock; + + public static RedisLock Tmp_Lock; + + /** + * 针对数据获取的时候添加锁 + */ + private static final String LOCK_TMP_NAME = "TMP_lock"; + + private static final String LOCK_FIXATION_NAME = "FIXATION_lock"; + + /** + * 等待获取锁的时间10 S + */ + private static final Long WAIT_LOCK_TIME = 10000L; + + /** + * 固定任务 + */ + private static final String FIXATION_WORK = "FIXATION_WORK"; + + /** + * 临时任务 + */ + private static final String TMP_WORK = "TMP_WORK"; + + /** + * 回执消息任务 + */ + @Deprecated + private static final String ACK_WORK = "ACK_WORK"; + + public SyncnQueue(RedisTemplate template) { + this.template = template; + Fix_Lock = new RedisLock(template); + Tmp_Lock = new RedisLock(template); + } + + /** + *

  • @Description:固定任务 {@link ModelType#GET} + *

    + * 固定任务执行效率跟任务链表的长度有关系 + *

  • @param model + *
  • @return + *
  • 创建人:maliang + *
  • 创建时间:2017年3月13日 + *
  • 修改人: + *
  • 修改时间: + */ + private Boolean pushFixationWork(SyncnModel model) { + Long tag = template.opsForList().rightPush(FIXATION_WORK, model); + return tag >= 1; + } + + @SuppressWarnings("unchecked") + private SyncnModel pullFixationModel() { + // 添加锁 + try { + if (Fix_Lock.acquireLock(LOCK_FIXATION_NAME, WAIT_LOCK_TIME)) { + SyncnModel obj = (SyncnModel) template.opsForList().leftPop(FIXATION_WORK); + this.pushFixationWork(obj); + return obj; + } + } catch (Exception e) { + } finally { + try { + if (Fix_Lock != null) { + Fix_Lock.releaseLock(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + return null; + + } + + /** + *
  • @Description:临时任务 + *

    + * {@link ModelType#ADD ModelType#UPDATE ModelType#DEL} + *

  • @param model + *
  • @return + *
  • 创建人:maliang + *
  • 创建时间:2017年3月13日 + *
  • 修改人: + *
  • 修改时间: + */ + private boolean pushTempWork(SyncnModel model) { + Long tag = template.opsForList().rightPush(TMP_WORK, model); + return tag >= 1; + } + + /** + *
  • @Description:临时队列数据弹出 + *
  • @return + *
  • 创建人:maliang + *
  • 创建时间:2017年3月13日 + *
  • 修改人: + *
  • 修改时间: + */ + private SyncnModel pullTempModel() { + + try { + if (Tmp_Lock.acquireLock(LOCK_TMP_NAME, WAIT_LOCK_TIME)) { + Serializable object = template.opsForList().leftPop(TMP_WORK); + return object != null ? (SyncnModel) object : null; + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (Tmp_Lock != null) { + try { + Tmp_Lock.releaseLock(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return null; + } + + /** + *
  • @Description:将任务信息添加到队列中,任务分为固定任务,临时任务,目前拉取数据的任务为固定任务 + * {@link ModelType#GET} + *
  • @param model + *
  • @return + *
  • 创建人:maliang + *
  • 创建时间:2017年3月13日 + *
  • 修改人: + *
  • 修改时间: + */ + public Boolean push(SyncnModel model) { + if (template == null || model == null) + return Boolean.FALSE; + // 设置任务添加时间 + model.setTs(System.currentTimeMillis()); + String modelType = model.getType(); + if (ModelType.isGet(modelType)) { + return this.pushFixationWork(model); + } else { + return this.pushTempWork(model); + } + } + + /** + *
  • @Description:获取队列中的数据 + *

    + * {@link ModelType#GET} 该参数将会获取固定任务列表中的数据{@link ModelType} 其他参数将会获取临时列表中数据 + *

  • @param type + *
  • @return + *
  • 创建人:maliang + *
  • 创建时间:2017年3月13日 + *
  • 修改人: + *
  • 修改时间: + */ + public SyncnModel pull(ModelType type) { + if (ModelType.isGet(type)) { + return this.pullFixationModel(); + } else { + return this.pullTempModel(); + } + } + + public RedisTemplate getTemplate() { + return template; + } + + public void setTemplate(RedisTemplate template) { + this.template = template; + } + +} diff --git a/core.sdk/src/main/java/com/lyms/synch/queue/package-info.java b/core.sdk/src/main/java/com/lyms/synch/queue/package-info.java new file mode 100644 index 0000000..6277a44 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/synch/queue/package-info.java @@ -0,0 +1,8 @@ +/** + *
  • @ClassName: package-info + *
  • @Description: 数据中心缓冲队列 + *
  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +package com.lyms.synch.queue; \ No newline at end of file diff --git a/core.sdk/src/main/java/com/lyms/util/JsonUtils.java b/core.sdk/src/main/java/com/lyms/util/JsonUtils.java new file mode 100644 index 0000000..67cf088 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/util/JsonUtils.java @@ -0,0 +1,39 @@ +package com.lyms.util; + +import java.util.Map; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + +/** + *
  • @ClassName: JsonUtils + *
  • @Description: json 转化工具 + *
  • @author maliang + *
  • @date 2017年3月13日 + *
  • + */ +public class JsonUtils { + + public static String beanToJson(T t) { + return JSON.toJSONString(t); + } + + /* + * @SuppressWarnings("unchecked") public static Map + * beanToMap(T t) { System.out.println(beanToJson(t)); return (Map) JSON.parse(beanToJson(t)); } + */ + + public static JSONObject beanToMap(T t) { + return JSON.parseObject(beanToJson(t)); + } + + public static T jsonToBean(String json, Class clazz) { + return JSON.parseObject(json, clazz); + } + + public static T mapToBean(Map map, Class clazz) { + return JSON.parseObject(JSON.toJSONString(map), clazz); + } + +} diff --git a/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java b/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java index 78f1382..5cb8b7d 100644 --- a/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java +++ b/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java @@ -108,12 +108,13 @@ public abstract class AbsRemote { */ public HttpEntity postEntity(Map params) { MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create(); - - ContentType content = ContentType.create(MimeTypeUtils.TEXT_PLAIN_VALUE, CharsetUtils.UTF_8); - for (Entry entry : params.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - reqEntity.addTextBody(key, value, content); + if (params != null) { + ContentType content = ContentType.create(MimeTypeUtils.TEXT_PLAIN_VALUE, CharsetUtils.UTF_8); + for (Entry entry : params.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + reqEntity.addTextBody(key, value, content); + } } return reqEntity.build(); } diff --git a/core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java b/core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java index bf9a9a6..582462b 100644 --- a/core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java +++ b/core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java @@ -9,32 +9,81 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.entity.SerializableEntity; + +import com.lyms.synch.entity.SyncnModel; /** * http,https 远程调用 + * * @author maliang * */ -public class HttpRemote extends AbsRemote{ +public class HttpRemote extends AbsRemote { + + private HttpRemote() { + } - private HttpRemote(){} - - public static HttpRemote build(){ + public static HttpRemote build() { return new HttpRemote(); } - + + public String post(SyncnModel model) { + if (model == null) + return null; + + String url = model.getRemote(); + HttpClientContext httpContext = HttpClientContext.create(); + HttpPost post = new HttpPost(url); + HttpEntity entity = null; + CloseableHttpResponse response = null; + try { + + // ContentType content = + // ContentType.create(MimeTypeUtils.TEXT_PLAIN_VALUE, + // CharsetUtils.UTF_8); + + // entity = new StringEntity(JsonUtils.beanToJson(model), content); + + entity = new SerializableEntity(model); + // post.setHeader("Content-Type", "text/plain; charset=utf-8"); + + post.setEntity(entity); + response = httpclient.execute(post, httpContext); + entity = response.getEntity(); + httpContext.getCookieStore().clear(); + return responseUTF8(entity); + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + + } + /** * get 请求 - *

    该方法参数只接受String类型参数 + *

    + * 该方法参数只接受String类型参数 + * * @param url * @param params * @return */ - public String get(String url,Map params) { + public String get(String url, Map params) { HttpClientContext httpContext = HttpClientContext.create(); - //处理参数,模拟http get 请求连接方式 - url = super.handlerReqParams(url,params); - LOG.debug("remote : {}",url); + // 处理参数,模拟http get 请求连接方式 + url = super.handlerReqParams(url, params); + LOG.debug("remote : {}", url); HttpGet get = new HttpGet(url); HttpEntity entity = null; CloseableHttpResponse response = null; @@ -58,26 +107,27 @@ public class HttpRemote extends AbsRemote{ } return null; } - + /** * post 请求 - *

    目前参数只接受String类型参数 + *

    + * 目前参数只接受String类型参数 + * * @param url * @param params * @return */ - public String post(String url,Map params) { + public String post(String url, Map params) { HttpClientContext httpContext = HttpClientContext.create(); HttpPost post = new HttpPost(url); HttpEntity entity = null; CloseableHttpResponse response = null; try { - entity = super.postEntity(params); - if(entity!=null) { + if (entity != null) { post.setEntity(entity); } - + response = httpclient.execute(post, httpContext); entity = response.getEntity(); httpContext.getCookieStore().clear(); @@ -97,7 +147,5 @@ public class HttpRemote extends AbsRemote{ } return null; } - - - + } diff --git a/core.sdk/src/main/java/com/lyms/web/controller/BaseController.java b/core.sdk/src/main/java/com/lyms/web/controller/BaseController.java index a8cbba6..30a6bd8 100644 --- a/core.sdk/src/main/java/com/lyms/web/controller/BaseController.java +++ b/core.sdk/src/main/java/com/lyms/web/controller/BaseController.java @@ -1,6 +1,7 @@ package com.lyms.web.controller; import java.io.IOException; +import java.io.ObjectInputStream; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; @@ -23,6 +24,7 @@ import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.plugins.pagination.Pagination; import com.lyms.common.StringEscapeEditor; import com.lyms.exception.BusinessException; +import com.lyms.synch.entity.SyncnModel; import com.lyms.util.HttpUtils; import com.lyms.util.StrUtils; import com.lyms.web.bean.AjaxResult; @@ -52,6 +54,13 @@ public class BaseController { binder.registerCustomEditor(String.class, new StringEscapeEditor(true, false)); } + public SyncnModel conver(HttpServletRequest request) throws IOException, ClassNotFoundException { + request.setCharacterEncoding("utf-8"); + ObjectInputStream dis = new ObjectInputStream(request.getInputStream()); + SyncnModel model = (SyncnModel) dis.readObject(); + return model; + } + /** * 是否为 post 请求 */