Commit 610bf7d4629a9d9eae8ec8ed4b89b820366ab6cc

Authored by fangcheng
1 parent 3bb5d8370a
Exists in master

修改类名

Showing 3 changed files with 202 additions and 202 deletions

core.sdk/src/main/java/com/lyms/sync/SyncCenter.java View file @ 610bf7d
... ... @@ -4,7 +4,7 @@
4 4 import java.util.concurrent.ScheduledExecutorService;
5 5  
6 6 import com.lyms.sync.channel.ChannelData;
7   -import com.lyms.sync.queue.SycnQueue;
  7 +import com.lyms.sync.queue.SyncQueue;
8 8 import com.lyms.util.HttpUtils;
9 9  
10 10 /**
... ... @@ -28,7 +28,7 @@
28 28 /**
29 29 * 数据缓冲队列
30 30 */
31   - private SycnQueue queue;
  31 + private SyncQueue queue;
32 32  
33 33 /**
34 34 * <li>@Description:创建一个中心
35 35  
... ... @@ -238,11 +238,11 @@
238 238  
239 239 }
240 240  
241   - public SycnQueue getQueue() {
  241 + public SyncQueue getQueue() {
242 242 return queue;
243 243 }
244 244  
245   - public void setQueue(SycnQueue queue) {
  245 + public void setQueue(SyncQueue queue) {
246 246 this.queue = queue;
247 247 }
248 248  
core.sdk/src/main/java/com/lyms/sync/queue/SycnQueue.java View file @ 610bf7d
1   -package com.lyms.sync.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.sync.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/sync/queue/SyncQueue.java View file @ 610bf7d
  1 +package com.lyms.sync.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.sync.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 SyncQueue {
  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 SyncQueue(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 +}