├── .gitignore ├── .travis.yml ├── LICENSE.txt ├── README.md ├── distributed-lock ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── kingsoft │ └── wps │ └── mail │ └── distributed │ └── lock │ └── DistributedLock.java ├── pom.xml ├── queue-core ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── kingsoft │ │ └── wps │ │ └── mail │ │ ├── exception │ │ ├── ErrorMessage.java │ │ └── NestedException.java │ │ ├── queue │ │ ├── KMQueueAdapter.java │ │ ├── KMQueueManager.java │ │ ├── RedisTaskQueue.java │ │ ├── Task.java │ │ ├── TaskHandler.java │ │ ├── TaskQueue.java │ │ ├── backup │ │ │ ├── BackupQueue.java │ │ │ └── RedisBackupQueue.java │ │ └── config │ │ │ └── Constant.java │ │ └── utils │ │ ├── Assert.java │ │ └── KMQUtils.java │ └── test │ └── java │ └── com │ └── kingsoft │ └── wps │ └── mail │ ├── JedisTest.java │ ├── MyTaskHandler.java │ └── QueueTest.java ├── queue-extension ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── kingsoft │ │ └── wps │ │ └── mail │ │ └── queue │ │ └── extension │ │ └── monitor │ │ ├── AliveDetectHandler.java │ │ ├── BackupQueueMonitor.java │ │ └── Pipeline.java │ └── test │ └── java │ └── com │ └── kingsoft │ └── wps │ └── mail │ ├── MonitorTest.java │ ├── MyAliveDetectHandler.java │ └── MyPipeline.java └── 基于Redis的分布式消息队列设计.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | .idea/ 3 | *.iml 4 | target/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | script: mvn clean install -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KMQueue 2 | 3 | [![Build Status](https://travis-ci.org/fnpac/KMQueue.svg?branch=master)](https://travis-ci.org/fnpac/KMQueue) 4 | 5 | 该框架是基于redis实现的分布式队列,简单灵活。 6 | 7 | 下面简单介绍下该队列的一些设计,如果还有其他不懂得地方可以参考源码和注释,代码中我加入了详尽的注释。 8 | 9 | 还有其他问题可以提issue。 10 | 11 | **更新历史**: 12 | 13 | 2018年1月23日:新增健康检测 14 | 防止执行耗时久的任务被备份队列监听检测到并当作失败任务重试。 15 | 但需要用户自行实现健康检测的逻辑,后续考虑通过zookeeper实现健康上报。 16 | 17 | ## 设计 18 | 19 | ### 序列图 20 | 21 | ![基于Redis的分布式消息队列设计.png](./基于Redis的分布式消息队列设计.png) 22 | 23 | ### 队列模式 24 | 25 | KMQueue队列分为两种模式: 26 | 27 | - `default` - 简单队列 28 | - `safe` - 安全队列 29 | 30 | 其中默认为`default`。 31 | 32 | 可以以`queueName:queueMode`格式设置队列的模式。 33 | 34 | - queueName 队列名称 35 | 36 | default 为默认队列,可以不指定,默认值。 37 | 特性:队列任务可能会丢失,队列任务没有超时限制。 38 | 39 | - queueMode 队列模式,可选值有:default、safe。 40 | 41 | safe 为安全队列,任务有重试策略,达到重试次数依旧失败或者任务存活超时(这里说的超时是指AliveTimeout)(这两者都称为最终失败),Monitor会发出通知, 42 | 这样可以根据业务做一些处理,推荐将这些失败的任务持久化到数据库作为日志记录。当然或许你还有更好的处理方式。 43 | 44 | >注意:需要开启备份队列监听程序BackupQueueMonitor,否则安全队列中最终失败的任务只会存储在备份队列中,而没有消费者去消费处理,这是很危险的行为 45 | 46 | ```java 47 | new KMQueueManager.Builder("127.0.0.1", 6379, "worker1_queue", "worker2_queue:safe") 48 | ... 49 | ``` 50 | 51 | `worker1_queue`为简单队列,`worker2_queue`为安全队列。 52 | 53 | >注意:为了更好的支持业务(将已存在的某个队列的`DEFAULT`改为`SAFE`,并重启服务的情况),做如下处理: 54 | 当`new KMQueueManager.Builder`的**队列名称参数**中,只要有一个队列指定了`SAFE`模式,就会创建备份队列(用于队列任务监控,设置任务超时、失败任务重试等), 55 | 并且该备份队列的名称基于传入的**所有队列名称**生成(无论其队列是否是`SAFE`模式)。 56 | 57 | 上面的例子中,备份队列的生成策略为: 58 | 59 | ```text 60 | base64(md5("worker1_queue" + "worker2_queue")) 61 | ``` 62 | 63 | ### Task(任务) 64 | 65 | 构造方法声明如下: 66 | 67 | ```java 68 | public Task(String queue, 69 | String uid, 70 | boolean isUnique, 71 | String type, 72 | String data, 73 | Task.TaskStatus status) 74 | ``` 75 | 76 | - uid:如果业务需要区分队列任务的唯一性,请自行生成uid参数, 77 | 否则队列默认使用uuid生成策略,这会导致即使data数据完全相同的任务也会被当作两个不同的任务处理。 78 | 79 | - 是否是唯一任务,即队列中同一时刻只存在一个该任务。 80 | 81 | - type:用于业务逻辑的处理,你可以根据不同的type任务类型,调用不同的handler去处理,可以不传。 82 | 83 | ### KMQueueManager(队列管理器) 84 | 85 | 有三种方式获取Redis连接,详情查看`KMQueueManager.Builder`构造方法的三种重载形式。 86 | 如果你使用spring,建议获取spring中配置的redis连接池对象,并通过如下构造方法创建队列管理器: 87 | 88 | ```java 89 | public Builder(Pool pool, String... queues) 90 | ``` 91 | 92 | ### RedisTaskQueue(任务队列) 93 | 94 | - 1.采用阻塞队列,以阻塞的方式(brpop)获取任务队列中的任务; 95 | - 2.判断任务存活时间是否超时(对应的是大于`aliveTimeout`); 96 | - 3.更新任务的执行时间戳,放入备份队列的队首(lpush); 97 | 98 | ### BackupQueueMonitor(备份队列监控) 99 | 100 | 因为初始化备份队列时设置了**循环标记**; 101 | 所以Monitor这里采用定时Job策略,使用`brpoplpush backupQueue backupQueue`循环遍历备份队列,遇到循环标记结束循环遍历。 102 | 对执行超时(对应的是大于`protectedTimeout`)或者存活时间超时(对应的是大于`aliveTimeout`)的任务做处理。 103 | 104 | 分为两种情况: 105 | 106 | - 任务存活时间超时 || (任务执行超时&任务重试次数大于RetryTimes):任务不再重试从备份队列删除该任务。 107 | 相应的可以通过实现`Pipeline`,决定这些任务的一些额外处理,比如持久化到数据库做日志记录。 108 | ```text 109 | // 任务彻底失败后的处理,需要实现Pipeline接口,自行实现处理逻辑 110 | TaskPipeline taskPipeline = new TaskPipeline(); 111 | BackupQueueMonitor backupQueueMonitor = new BackupQueueMonitor.Builder("127.0.0.1", 6379, backUpQueueName) 112 | ... 113 | .setPipeline(taskPipeline).build(); 114 | ``` 115 | - 任务执行超时&任务重试次数小于RetryTimes:即超时并且重复执行次数小于`RetryTimes`的任务重新放回任务队列执行,同时更新任务状态: 116 | - 放入任务队列,优先处理(); 117 | - 任务state标记为"retry"; 118 | - 重试次数+1; 119 | 120 | #### 健康检查 121 | 122 | 使用方式: 123 | 124 | ```java 125 | // 健康检测 126 | MyAliveDetectHandler detectHandler = new MyAliveDetectHandler(); 127 | ... 128 | // 构造Monitor监听器 129 | BackupQueueMonitor backupQueueMonitor = new BackupQueueMonitor.Builder("127.0.0.1", 6379, backUpQueueName) 130 | ... 131 | .registerAliveDetectHandler(detectHandler) 132 | .build(); 133 | // 执行监听 134 | backupQueueMonitor.monitor(); 135 | ``` 136 | 137 | `registerAliveDetectHandler()`方法可以设置Null,则不会启用健康检测。 138 | 139 | 检查正在执行的任务是否还在执行(存活), 140 | 141 | 为了防止耗时比较久的任务(任务的执行时间超出了通过队列管理器配置的任务执行超时时间 - 默认值:com.kingsoft.wps.mail.queue.config.Constant.PROTECTED_TIMEOUT) 142 | 会被备份队列监听器检测到并重新放入任务队列执行(因为备份队列监听器会把超出通过队列管理器配置的任务执行超时时间的任务当作是失败的任务(参考 什么是失败任务?)并进行重试)。 143 | 144 | 通过这种检测机制,可以保证check(Task)返回为true的任务(任务还在执行)不会被备份队列监听器重新放入任务队列重试。 145 | 这里只是提供一个接口,用户需要自己实现执行任务的健康检测。 146 | 147 | 目前健康检测机制还只是处于初步阶段,核心的检测逻辑还需要用户自行实现,这里只是提供一个接口。 148 | 149 | 一个比较简单的实现方式就是起一个定时job,每隔n毫秒检查线程中正在执行任务的状态,在redis中以 "任务的id + ALIVE_KEY_SUFFIX" 为key,ttl 为 n+m 毫秒(m < n, m用于保证两次job的空窗期),标记正在执行的任务。 150 | 然后AliveDetectHandler的实现类根据task去检查redis中是否存在该key,如果存在,返回true 151 | 152 | ## 使用Demo 153 | 154 | ### 生产任务 155 | 156 | ```java 157 | @Test 158 | public void pushTaskTest() { 159 | KMQueueManager kmQueueManager = new KMQueueManager.Builder("127.0.0.1", 6379, "worker1_queue", "worker2_queue:safe") 160 | .setMaxWaitMillis(-1L) 161 | .setMaxTotal(600) 162 | .setMaxIdle(300) 163 | .setAliveTimeout(Constant.ALIVE_TIMEOUT) 164 | .build(); 165 | // 初始化队列 166 | kmQueueManager.init(); 167 | 168 | // 1.获取队列 169 | TaskQueue taskQueue = kmQueueManager.getTaskQueue("worker2_queue"); 170 | // 2.创建任务 171 | JSONObject ob = new JSONObject(); 172 | ob.put("data", "mail proxy task"); 173 | String data = JSON.toJSONString(ob); 174 | // 参数 uid:如果业务需要区分队列任务的唯一性,请自行生成uid参数, 175 | // 否则队列默认使用uuid生成策略,这会导致即使data数据完全相同的任务也会被当作两个不同的任务处理。 176 | // 参数 isUnique:是否是唯一任务,即队列中同一时刻只存在一个该任务。 177 | // 只有当该任务所属队列是安全队列时才生效;如果为true,则通过uid判断任务的唯一性。 178 | // 参数 type:用于业务逻辑的处理,你可以根据不同的type任务类型,调用不同的handler去处理,可以不传。 179 | Task task = new Task(taskQueue.getName(), "", true, "", data, new Task.TaskStatus()); 180 | // 3.将任务加入队列 181 | taskQueue.pushTask(task); 182 | } 183 | ``` 184 | 185 | ### 消费任务 186 | 187 | ```java 188 | @Test 189 | public void popTaskTest() { 190 | KMQueueManager kmQueueManager = new KMQueueManager.Builder("127.0.0.1", 6379, "worker1_queue", "worker2_queue:safe") 191 | .setMaxWaitMillis(-1L) 192 | .setMaxTotal(600) 193 | .setMaxIdle(300) 194 | .setAliveTimeout(Constant.ALIVE_TIMEOUT) 195 | .build(); 196 | // 初始化队列 197 | kmQueueManager.init(); 198 | 199 | // 1.获取队列 200 | TaskQueue taskQueue = kmQueueManager.getTaskQueue("worker1_queue"); 201 | // 2.获取任务 202 | Task task = taskQueue.popTask(); 203 | // 业务处理放到TaskConsumersHandler里 204 | if (task != null) { 205 | task.doTask(kmQueueManager, MyTaskHandler.class); 206 | } 207 | } 208 | ``` 209 | 210 | 你可以自行实现`TaskHandler`接口,创建适合你自己业务逻辑的任务处理类,并通过下面代码执行任务处理。 211 | 212 | ```java 213 | task.doTask(kmQueueManager, TaskHandler.class) 214 | ``` 215 | 216 | 此外,`doTask`方法还支持业务传参,通过第三个参数实现`params`。 217 | 218 | ```java 219 | task.doTask(kmQueueManager, TaskHandler.class, params) 220 | ``` 221 | 222 | _**如果业务处理抛出异常,队列也将其当作任务执行完成处理,**_ 223 | 224 | 并通过`taskQueue.finishTask(this)`完成任务。 225 | 226 | ```java 227 | public void doTask(KMQueueManager kmQueueManager, Class clazz, Object... params) { 228 | 229 | // 获取任务所属队列 230 | TaskQueue taskQueue = kmQueueManager.getTaskQueue(this.getQueue()); 231 | String queueMode = taskQueue.getMode(); 232 | if (KMQueueManager.SAFE.equals(queueMode)) {// 安全队列 233 | try { 234 | handleTask(clazz, params); 235 | } catch (Throwable e) { 236 | e.printStackTrace(); 237 | } 238 | // 任务执行完成,删除备份队列的相应任务 239 | taskQueue.finishTask(this); 240 | } else {// 普通队列 241 | handleTask(clazz); 242 | } 243 | } 244 | ``` 245 | 246 | _**不会再进行任务重试操作。**_ 247 | 248 | 这点可能不太容易理解,为什么任务抛出异常失败了,队列不会执行重试呢? 249 | 250 | 因为任务执行抛出异常是业务级的错误,队列不做干预。 251 | 252 | 队列的重试只是针对消费任务的线程被kill掉或者服务器宕机等情况,此时该任务还没执行完,任务的消费者还没告诉队列任务执行完成了。 253 | 此时备份队列监控会执行任务的重试。 254 | 255 | 如果你想在任务抛出异常失败时执行任务重试,可以不使用`task.doTask`,当任务抛出异常时,不执行任务的`taskQueue.finishTask(this)`操作。 256 | 这样备份队列监控会在下一个job对该任务进行检查处理。 257 | 258 | >`taskQueue.finishTask(this)`是一个非常方便的工具方法。 259 | 260 | ### 备份队列监控 261 | 262 | ```java 263 | @Test 264 | public void monitorTaskTest() { 265 | 266 | // 健康检测 267 | MyAliveDetectHandler detectHandler = new MyAliveDetectHandler(); 268 | // 任务彻底失败后的处理,需要实现Pipeline接口,自行实现处理逻辑 269 | MyPipeline pipeline = new MyPipeline(); 270 | // 根据任务队列的名称构造备份队列的名称,注意:这里的任务队列参数一定要和KMQueueManager构造时传入的一一对应。 271 | String backUpQueueName = KMQUtils.genBackUpQueueName("worker1_queue", "worker2_queue:safe"); 272 | // 构造Monitor监听器 273 | BackupQueueMonitor backupQueueMonitor = new BackupQueueMonitor.Builder("127.0.0.1", 6379, backUpQueueName) 274 | .setMaxWaitMillis(-1L) 275 | .setMaxTotal(600) 276 | .setMaxIdle(300) 277 | .setAliveTimeout(Constant.ALIVE_TIMEOUT) 278 | .setProtectedTimeout(Constant.PROTECTED_TIMEOUT) 279 | .setRetryTimes(Constant.RETRY_TIMES) 280 | .registerAliveDetectHandler(detectHandler) 281 | .setPipeline(pipeline).build(); 282 | // 执行监听 283 | backupQueueMonitor.monitor(); 284 | } 285 | ``` 286 | 287 | 重要的事情说三遍: 288 | 289 | 如果指定了队列的模式为安全队列,一定要开启**备份队列监控**!!!一定要开启**备份队列监控**!!!一定要开启**备份队列监控**!!! 290 | 291 | ## QA 292 | 293 | ### 什么是失败任务? 294 | 295 | 任务执行抛出异常是业务级的错误,队列不做干预,队列依旧把它当作是成功的任务。 296 | 297 | 队列的重试只是针对消费任务的线程被kill掉或者服务器宕机等情况,此时该任务还没执行完,任务的消费者还没告诉队列任务执行完成了。 298 | 此时备份队列监控会执行任务的重试。在这种情况下,任务才能定义为失败任务。 299 | -------------------------------------------------------------------------------- /distributed-lock/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | queue 7 | com.kingsoft.wps.mail 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | distributed-lock 13 | 14 | 15 | -------------------------------------------------------------------------------- /distributed-lock/src/main/java/com/kingsoft/wps/mail/distributed/lock/DistributedLock.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.distributed.lock; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | import java.util.UUID; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | /** 9 | * Redis分布式锁 10 | * 11 | * @author liuchunlong 12 | */ 13 | public class DistributedLock { 14 | 15 | private static final Lock NO_LOCK = new Lock(new UUID(0l, 0l), 0l);//超时锁 uuid:00000000-0000-0000-0000-000000000000 expiryTime:0 16 | 17 | private static final int ONE_SECOND = 1000;//1秒 18 | private static final int default_acquire_timeout_millis = Integer.getInteger("distribute.lock.default.acquire.timeout.millis", 10 * ONE_SECOND);//默认锁的请求超时时长 19 | private static final int default_expiry_millis = Integer.getInteger("distribute.lock.default.expiry.millis", 60 * ONE_SECOND);//默认锁的过期时长 20 | private static final int default_acquire_resolution_millis = Integer.getInteger("distribute.lock.default.acquire.resolution.millis", 100);//循环请求分布式锁线程休眠时长 21 | 22 | private Lock lock = null;//当前持有得锁 23 | 24 | private final Jedis jedis; 25 | private final String lockKey;//锁在Redis中的Key标记 (ex. distribute::lock, ...) 26 | private final int lockExpiryInMillis;//锁的过期时长 27 | private final int acquireTimeoutInMillis;//锁的请求超时时长 28 | private final UUID lockUUID;//锁的唯一标识 29 | 30 | public DistributedLock(String lockKey) { 31 | this(null, lockKey, default_acquire_timeout_millis, default_expiry_millis); 32 | } 33 | 34 | /** 35 | * 构造方法:
36 | * - 使用默认锁的请求超时时长;
37 | * - 使用默认锁的过期时长;
38 | * - 锁的唯一标识UUID采用随机生成策略;
39 | * 40 | * @param jedis Jedis对象 41 | * @param lockKey 锁在Redis中的Key标记 (ex. distribute::lock, ...) 42 | */ 43 | public DistributedLock(Jedis jedis, String lockKey) { 44 | this(jedis, lockKey, default_acquire_timeout_millis, default_expiry_millis); 45 | } 46 | 47 | /** 48 | * 构造方法:
49 | * - 使用默认锁的过期时长;
50 | * - 锁的唯一标识UUID采用随机生成策略;
51 | * 52 | * @param jedis Jedis对象 53 | * @param lockKey 锁在Redis中的Key标记 (ex. distribute::lock, ...) 54 | * @param acquireTimeoutInMillis 请求超时时长(单位:毫秒) 55 | */ 56 | public DistributedLock(Jedis jedis, String lockKey, int acquireTimeoutInMillis) { 57 | this(jedis, lockKey, acquireTimeoutInMillis, default_expiry_millis); 58 | } 59 | 60 | /** 61 | * 构造方法:
62 | * - 锁的唯一标识UUID采用随机生成策略 63 | * 64 | * @param jedis Jedis对象 65 | * @param lockKey 锁在Redis中的Key标记 (ex. distribute::lock, ...) 66 | * @param acquireTimeoutInMillis 请求超时时长(单位:毫秒) 67 | * @param lockExpiryInMillis 锁的过期时长(单位:毫秒) 68 | */ 69 | public DistributedLock(Jedis jedis, String lockKey, int acquireTimeoutInMillis, int lockExpiryInMillis) { 70 | this(jedis, lockKey, acquireTimeoutInMillis, lockExpiryInMillis, UUID.randomUUID()); 71 | } 72 | 73 | /** 74 | * 构造方法 75 | * 76 | * @param jedis Jedis对象 77 | * @param lockKey 锁在Redis中的Key标记 (ex. distribute::lock, ...) 78 | * @param acquireTimeoutInMillis 请求超时时长(单位:毫秒) 79 | * @param lockExpiryInMillis 锁的过期时长(单位:毫秒) 80 | * @param uuid 锁的唯一标识 81 | */ 82 | public DistributedLock(Jedis jedis, String lockKey, int acquireTimeoutInMillis, int lockExpiryInMillis, UUID uuid) { 83 | this.jedis = jedis; 84 | this.lockKey = lockKey; 85 | this.acquireTimeoutInMillis = acquireTimeoutInMillis; 86 | this.lockExpiryInMillis = lockExpiryInMillis; 87 | this.lockUUID = uuid; 88 | } 89 | 90 | /** 91 | * 获取锁的唯一标识UUID 92 | * 93 | * @return lock uuid 94 | */ 95 | public UUID getLockUUID() { 96 | return this.lockUUID; 97 | } 98 | 99 | /** 100 | * 获取锁在Redis中的Key标记 101 | * 102 | * @return lock key 103 | */ 104 | public String getLockKey() { 105 | return this.lockKey; 106 | } 107 | 108 | /** 109 | * 锁的过期时长 110 | * 111 | * @return 112 | */ 113 | public int getLockExpiryInMillis() { 114 | return lockExpiryInMillis; 115 | } 116 | 117 | /** 118 | * 锁的请求超时时长 119 | * 120 | * @return 121 | */ 122 | public int getAcquireTimeoutInMillis() { 123 | return acquireTimeoutInMillis; 124 | } 125 | 126 | /** 127 | * 请求分布式锁 128 | * 129 | * @return 请求到锁返回true, 超时返回false 130 | * @throws InterruptedException 线程中断异常 131 | */ 132 | public synchronized boolean acquire() throws InterruptedException { 133 | return acquire(jedis); 134 | } 135 | 136 | /** 137 | * 请求分布式锁 138 | * 139 | * @param jedis Jedis对象 140 | * @return 请求到锁返回true, 超时返回false 141 | * @throws InterruptedException 线程中断异常 142 | */ 143 | public synchronized boolean acquire(Jedis jedis) throws InterruptedException { 144 | 145 | //采用"自旋获取锁"的方式,每次循环线程休眠100毫秒,直至请求锁超时 146 | int timeout = acquireTimeoutInMillis;//锁的请求超时时长 147 | 148 | while (timeout >= 0) { 149 | //创建一个新锁 150 | final Lock tLock = new Lock(lockUUID, System.currentTimeMillis() + lockExpiryInMillis); 151 | /** 152 | * 将当前锁(tLock)写入Redis中 153 | * 如果成功写入,Redis中不存在锁,获取锁成功; 154 | * 否则,Redis中已存在锁,获取锁失败; 155 | */ 156 | if (jedis.setnx(lockKey, tLock.toString()) == 1) { 157 | this.lock = tLock; 158 | return true; 159 | } 160 | 161 | /** 162 | * 至此,Redis中已存在锁,获取锁失败,则需要进行如下操作: 163 | * 判断Redis中已存在的锁是否过期,如果过期则直接获取锁; 164 | * 否则,通过自旋获取锁 165 | */ 166 | final String currentLockValue = jedis.get(lockKey);//获取Redis中已存在的锁的值 167 | final Lock currentLock = Lock.fromString(currentLockValue);//Redis中已存在的锁 168 | 169 | //如果Redis中已存在的锁(原始锁)已超时或者是当前线程的,则重新获取锁 170 | if (currentLock.isExpiredOrMine(lockUUID)) { 171 | String originLockValue = jedis.getSet(lockKey, tLock.toString()); 172 | /** 173 | * 这里还有个前置条件: 174 | * 会对原始锁进行校验,jedis.get()和jedis.getSet()获取的锁必须是同一锁,重新获取锁才成功 175 | */ 176 | //特别的,当jedis.getSet()获取原始锁originLockValue为空时,应直接获取锁成功 177 | if (originLockValue == null) { 178 | this.lock = tLock; 179 | return true; 180 | } 181 | if (originLockValue != null && originLockValue.equals(currentLockValue)) { 182 | this.lock = tLock; 183 | return true; 184 | } 185 | } 186 | 187 | timeout -= default_acquire_resolution_millis; 188 | TimeUnit.MILLISECONDS.sleep(default_acquire_resolution_millis); 189 | } 190 | return false; 191 | } 192 | 193 | /** 194 | * 重新获取锁 195 | * 196 | * @return 如果获得锁返回true,否则返回false 197 | * @throws InterruptedException 线程中断异常 198 | */ 199 | public boolean renew() throws InterruptedException { 200 | final Lock lock = Lock.fromString(jedis.get(lockKey));//获取Redis中已存在的锁 201 | if (!lock.isExpiredOrMine(lockUUID)) {//如果Redis中已存在的锁(原始锁)已超时或者是当前线程的,则重新获取锁 202 | return false; 203 | } 204 | return acquire(jedis); 205 | } 206 | 207 | /** 208 | * 释放锁 209 | */ 210 | public synchronized void release() { 211 | release(jedis); 212 | } 213 | 214 | public synchronized void release(Jedis jedis) { 215 | if (isLocked()) { 216 | //存在一种情况,当前线程阻塞很长时间后再次执行,此时该线程持有的锁已经超时,并且其它线程获取了锁。这时当前线程就不应该再删除该锁 217 | if (this.lock.isExpired()) {//当前线程持有的锁已经超时 218 | final Lock lock = Lock.fromString(jedis.get(lockKey));//获取Redis中已存在的锁 219 | final UUID uuid = lock.getUUID(); 220 | if (!this.lock.isMine(uuid)) {//如果Redis中已存在的锁(原始锁)不是当前线程的,则直接返回,不再释放锁 221 | return; 222 | } 223 | } 224 | jedis.del(lockKey); 225 | this.lock = null; 226 | } 227 | } 228 | 229 | /** 230 | * 判断当前是否获取锁 231 | * 232 | * @return 返回布尔类型的值 233 | */ 234 | public synchronized boolean isLocked() { 235 | return this.lock != null; 236 | } 237 | 238 | public synchronized long getLockExpiryTimeInMillis() { 239 | return this.lock.getExpiryTime(); 240 | } 241 | 242 | /** 243 | * 锁 244 | */ 245 | protected static class Lock { 246 | 247 | private UUID uuid;//锁的唯一标识uuid 248 | private long expiryTime;//锁的过期时间,注意,不是过期时长 249 | 250 | protected Lock(UUID uuid, long expiryTimeInMillis) { 251 | this.uuid = uuid; 252 | this.expiryTime = expiryTimeInMillis; 253 | } 254 | 255 | /** 256 | * 解析字符串,根据解析出的uuid和过期时间构造Lock 257 | * 258 | * @param text 字符串参数,参数格式:"*:*" 259 | * @return Lock 字符串转化的锁对象 260 | */ 261 | protected static Lock fromString(String text) { 262 | try { 263 | String[] parts = text.split(":"); 264 | UUID theUUID = UUID.fromString(parts[0]); 265 | long theTime = Long.parseLong(parts[1]); 266 | return new Lock(theUUID, theTime); 267 | } catch (Exception e) { 268 | return NO_LOCK; 269 | } 270 | } 271 | 272 | public UUID getUUID() { 273 | return uuid; 274 | } 275 | 276 | public long getExpiryTime() { 277 | return expiryTime; 278 | } 279 | 280 | @Override 281 | public String toString() { 282 | return uuid.toString() + ":" + expiryTime; 283 | } 284 | 285 | /** 286 | * 判断锁是否超时,如果锁的过期时间小于当前系统时间,则判定锁超时 287 | * 288 | * @return 返回布尔类型的值 289 | */ 290 | boolean isExpired() { 291 | return getExpiryTime() < System.currentTimeMillis(); 292 | } 293 | 294 | /** 295 | * 判断锁是否是当前线程拥有的锁 296 | * 297 | * @param otherUUID 298 | * @return 299 | */ 300 | boolean isMine(UUID otherUUID) { 301 | return this.getUUID().equals(otherUUID); 302 | } 303 | 304 | /** 305 | * 判断锁是否超时或者锁是当前线程拥有的锁 306 | * 307 | * @param otherUUID 锁的唯一标识uuid 308 | * @return 返回布尔类型的值 309 | */ 310 | boolean isExpiredOrMine(UUID otherUUID) { 311 | return this.isExpired() || this.getUUID().equals(otherUUID); 312 | } 313 | } 314 | } 315 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.kingsoft.wps.mail 8 | queue 9 | pom 10 | 1.0-SNAPSHOT 11 | 12 | queue-core 13 | queue-extension 14 | distributed-lock 15 | 16 | 17 | 18 | 19 | 3.5.1 20 | 2.12.4 21 | 1.8 22 | UTF-8 23 | 2.9.0 24 | 2.7.1 25 | 1.2.16 26 | 3.0.0 27 | 28 | 29 | 30 | 31 | 32 | redis.clients 33 | jedis 34 | ${jedis.version} 35 | 36 | 37 | 38 | com.fasterxml.jackson.core 39 | jackson-databind 40 | ${jackson.version} 41 | 42 | 43 | com.fasterxml.jackson.jaxrs 44 | jackson-jaxrs-json-provider 45 | ${jackson.version} 46 | 47 | 48 | 49 | com.alibaba 50 | fastjson 51 | ${alibaba.fastjson.version} 52 | 53 | 54 | junit 55 | junit 56 | 4.12 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.apache.maven.plugins 65 | maven-compiler-plugin 66 | ${compiler.plugin.version} 67 | 68 | ${jdk.version} 69 | ${jdk.version} 70 | ${project.build.sourceEncoding} 71 | true 72 | 73 | 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-source-plugin 78 | ${source.plugin.version} 79 | 80 | true 81 | 82 | 83 | 84 | compile 85 | 86 | jar 87 | 88 | 89 | 90 | 91 | 92 | org.apache.maven.plugins 93 | maven-surefire-plugin 94 | ${maven-surefire-plugin.version} 95 | 96 | true 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /queue-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | queue 7 | com.kingsoft.wps.mail 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | queue-core 13 | 14 | 15 | 16 | 17 | com.kingsoft.wps.mail 18 | distributed-lock 19 | 1.0-SNAPSHOT 20 | 21 | 22 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/exception/ErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.exception; 2 | 3 | import com.fasterxml.jackson.annotation.JsonInclude; 4 | 5 | /** 6 | * Created by 刘春龙 on 2017/6/12. 7 | */ 8 | @JsonInclude(value = JsonInclude.Include.NON_NULL) 9 | public class ErrorMessage { 10 | private String errorCode;//错误码 11 | private String errorMsg;//错误描述 12 | 13 | public ErrorMessage() { 14 | } 15 | 16 | 17 | public String getErrorCode() { 18 | return errorCode; 19 | } 20 | 21 | public ErrorMessage setErrorCode(String errorCode) { 22 | this.errorCode = errorCode; 23 | return this; 24 | } 25 | 26 | public String getErrorMsg() { 27 | return errorMsg; 28 | } 29 | 30 | public ErrorMessage setErrorMsg(String errorMsg) { 31 | this.errorMsg = errorMsg; 32 | return this; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/exception/NestedException.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.exception; 2 | 3 | /** 4 | * Created by 刘春龙 on 2017/6/6. 5 | */ 6 | public class NestedException extends RuntimeException { 7 | 8 | private static final long serialVersionUID = 1L; 9 | 10 | private ErrorMessage errorMessage; 11 | 12 | public NestedException() { 13 | super(); 14 | } 15 | 16 | public NestedException(String message) { 17 | super(message); 18 | } 19 | 20 | public NestedException(String message, Throwable cause) { 21 | super(message, cause); 22 | } 23 | 24 | public NestedException(Throwable cause) { 25 | super(cause); 26 | } 27 | 28 | public NestedException(ErrorMessage errorMessage) { 29 | super(errorMessage.getErrorMsg()); 30 | this.errorMessage = errorMessage; 31 | } 32 | 33 | public NestedException(ErrorMessage errorMessage, String message) { 34 | super(message); 35 | this.errorMessage = errorMessage; 36 | } 37 | 38 | public NestedException(ErrorMessage errorMessage, String message, Throwable cause) { 39 | super(message, cause); 40 | this.errorMessage = errorMessage; 41 | } 42 | 43 | public NestedException(ErrorMessage errorMessage, Throwable cause) { 44 | super(cause); 45 | this.errorMessage = errorMessage; 46 | } 47 | 48 | public ErrorMessage getErrorMessage() { 49 | return errorMessage; 50 | } 51 | 52 | public void setErrorMessage(ErrorMessage errorMessage) { 53 | this.errorMessage = errorMessage; 54 | } 55 | 56 | public Throwable getRootCause() { 57 | Throwable t = this; 58 | while (true) { 59 | Throwable cause = t.getCause(); 60 | if (cause != null) { 61 | t = cause; 62 | } else { 63 | break; 64 | } 65 | } 66 | return t; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/queue/KMQueueAdapter.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue; 2 | 3 | import com.kingsoft.wps.mail.utils.Assert; 4 | import redis.clients.jedis.Jedis; 5 | import redis.clients.util.Pool; 6 | 7 | /** 8 | * Created by 刘春龙 on 2018/1/19. 9 | */ 10 | public abstract class KMQueueAdapter { 11 | 12 | // 队列模式:DEFAULT - 简单队列,SAFE - 安全队列 13 | public static final String DEFAULT = "default"; 14 | public static final String SAFE = "safe"; 15 | public static String BACK_UP_QUEUE_PREFIX = "back_up_queue_";// 备份队列名称前缀 16 | 17 | /** 18 | * 备份队列名称 19 | */ 20 | protected String backUpQueueName; 21 | 22 | /** 23 | * redis连接池 24 | */ 25 | protected Pool pool; 26 | 27 | /** 28 | * 获取备份队列的名称 29 | * 30 | * @return 备份队列的名称 31 | */ 32 | public String getBackUpQueueName() { 33 | return this.backUpQueueName; 34 | } 35 | 36 | public abstract long getAliveTimeout(); 37 | 38 | /** 39 | * 获取Jedis对象 40 | *

41 | * 使用完成后,必须归还到连接池中 42 | * 43 | * @return Jedis对象 44 | */ 45 | public synchronized Jedis getResource() { 46 | Jedis jedis = this.pool.getResource(); 47 | Assert.notNull(jedis, "Get jedis client failed"); 48 | return jedis; 49 | } 50 | 51 | /** 52 | * 获取Jedis对象 53 | *

54 | * 使用完成后,必须归还到连接池中 55 | * 56 | * @param db Redis数据库序号 57 | * @return Jedis对象 58 | */ 59 | public synchronized Jedis getResource(int db) { 60 | Jedis jedis = this.pool.getResource(); 61 | Assert.notNull(jedis, "Get jedis client failed"); 62 | jedis.select(db); 63 | return jedis; 64 | } 65 | 66 | /** 67 | * 归还Redis连接到连接池 68 | * 69 | * @param jedis Jedis对象 70 | */ 71 | public synchronized void returnResource(Jedis jedis) { 72 | if (jedis != null) { 73 | // pool.returnResource(jedis); 74 | // from Jedis 3.0 75 | jedis.close(); 76 | } 77 | } 78 | 79 | public synchronized void destroy() throws Exception { 80 | pool.destroy(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/queue/KMQueueManager.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue; 2 | 3 | import com.kingsoft.wps.mail.exception.NestedException; 4 | import com.kingsoft.wps.mail.queue.backup.BackupQueue; 5 | import com.kingsoft.wps.mail.queue.backup.RedisBackupQueue; 6 | import com.kingsoft.wps.mail.utils.Assert; 7 | import com.kingsoft.wps.mail.utils.KMQUtils; 8 | import redis.clients.jedis.Jedis; 9 | import redis.clients.jedis.JedisPool; 10 | import redis.clients.jedis.JedisPoolConfig; 11 | import redis.clients.jedis.JedisSentinelPool; 12 | import redis.clients.util.Pool; 13 | import sun.misc.BASE64Encoder; 14 | 15 | import java.io.UnsupportedEncodingException; 16 | import java.security.MessageDigest; 17 | import java.security.NoSuchAlgorithmException; 18 | import java.util.*; 19 | import java.util.concurrent.ConcurrentHashMap; 20 | import java.util.logging.Logger; 21 | 22 | /** 23 | * Created by 刘春龙 on 2018/1/17. 24 | *

25 | * 队列管理器,redis线程池的初始化,队列的初始化 26 | */ 27 | public class KMQueueManager extends KMQueueAdapter { 28 | 29 | private final static Logger logger = Logger.getLogger(KMQueueManager.class.getName()); 30 | 31 | private Map queueMap = new ConcurrentHashMap<>(); 32 | 33 | /** 34 | * 待创建的队列的名称集合 35 | */ 36 | private List queues; 37 | 38 | /** 39 | * 任务的存活超时时间。单位:ms 40 | *

41 | * 注意,该时间是任务从创建({@code new Task(...)})到销毁的总时间 42 | *

43 | * 该值只针对安全队列起作用 44 | *

45 | * 不设置默认为 Long.MAX_VALUE 46 | */ 47 | private long aliveTimeout; 48 | 49 | /** 50 | * 构造方法私有化,防止外部调用 51 | */ 52 | private KMQueueManager() { 53 | } 54 | 55 | /** 56 | * 根据名称获取任务队列 57 | * 58 | * @param name 队列名称 59 | * @return 任务队列 60 | */ 61 | public TaskQueue getTaskQueue(String name) { 62 | Object queue = this.queueMap.get(name); 63 | if (queue != null && queue instanceof TaskQueue) { 64 | return (TaskQueue) queue; 65 | } 66 | return null; 67 | } 68 | 69 | /** 70 | * 获取任务存活超时时间。注意,该时间是任务从创建({@code new Task(...)})到销毁的总时间。单位:ms 71 | * 72 | * @return 73 | */ 74 | public long getAliveTimeout() { 75 | return this.aliveTimeout; 76 | } 77 | 78 | /** 79 | * 初始化队列 80 | */ 81 | public void init() { 82 | // 生成备份队列名称 83 | backUpQueueName = KMQUtils.genBackUpQueueName(this.queues); 84 | 85 | logger.info("Initializing the queues"); 86 | 87 | boolean hasSq = false; 88 | 89 | for (String queue : this.queues) { 90 | String[] qInfos = queue.trim().split(":"); 91 | String qName = qInfos[0].trim();// 队列名称 92 | String qMode = null;// 队列模式 93 | if (qInfos.length == 2) { 94 | qMode = qInfos[1].trim(); 95 | } 96 | 97 | if (qMode != null && !"".equals(qMode) && !qMode.equals(DEFAULT) && !qMode.equals(SAFE)) { 98 | throw new NestedException("The current queue mode is invalid, the queue name:" + qName); 99 | } 100 | 101 | if (!"".equals(qName)) { 102 | if (!queueMap.containsKey(qName)) { 103 | if (qMode != null && qMode.equals(SAFE)) { 104 | hasSq = true;// 标记存在安全队列 105 | } 106 | 107 | queueMap.put(qName, new RedisTaskQueue(this, qName, qMode)); 108 | logger.info("Creating a task queue:" + qName); 109 | } else { 110 | logger.info("The current queue already exists. Do not create the queue name repeatedly:" + qName); 111 | } 112 | } else { 113 | throw new NestedException("The current queue name is empty!"); 114 | } 115 | } 116 | 117 | // 添加备份队列 118 | if (hasSq) { 119 | BackupQueue backupQueue = new RedisBackupQueue(this); 120 | backupQueue.initQueue(); 121 | queueMap.put(backUpQueueName, backupQueue); 122 | logger.info("Initializing backup queue"); 123 | } 124 | } 125 | 126 | /** 127 | * 构建器,用于设置初始化参数,执行初始化操作 128 | */ 129 | public static class Builder { 130 | 131 | /** 132 | * redis连接方式: 133 | *

    134 | *
  • default
  • 135 | *
  • single
  • 136 | *
  • sentinel
  • 137 | *
138 | */ 139 | private final String REDIS_CONN_DEFAULT = "default"; 140 | private final String REDIS_CONN_SINGLE = "single"; 141 | private final String REDIS_CONN_SENTINEL = "sentinel"; 142 | private String REDIS_CONN_MODE; 143 | 144 | /** 145 | * redis连接池 146 | */ 147 | private Pool pool; 148 | 149 | /** 150 | * 待创建的队列的名称集合 151 | */ 152 | private List queues; 153 | 154 | /** 155 | * redis host 156 | */ 157 | private String host; 158 | 159 | /** 160 | * redis port 161 | */ 162 | private int port; 163 | 164 | /** 165 | * 主从复制集 166 | */ 167 | private Set sentinels; 168 | 169 | /** 170 | * 连接池最大分配的连接数 171 | */ 172 | private Integer poolMaxTotal; 173 | 174 | /** 175 | * 连接池的最大空闲连接数 176 | */ 177 | private Integer poolMaxIdle; 178 | 179 | /** 180 | * redis获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常,小于零:阻塞不确定的时间,默认-1 181 | */ 182 | private Long poolMaxWaitMillis; 183 | 184 | /** 185 | * 任务的存活超时时间。注意,该时间是任务从创建({@code new Task(...)})到销毁的总时间。单位:ms 186 | *

187 | * 该值只针对安全队列起作用 188 | *

189 | * 不设置默认为 Long.MAX_VALUE 190 | */ 191 | private long aliveTimeout; 192 | 193 | /** 194 | * 创建Builder对象 195 | *

196 | * 使用指定的redis连接池 197 | * 198 | * @param pool redis连接池 199 | * @param queues 所要创建的队列名称,可以传多个 200 | */ 201 | public Builder(Pool pool, String... queues) { 202 | Assert.notNull(pool, "Param pool can't null"); 203 | 204 | this.aliveTimeout = Long.MAX_VALUE; 205 | this.pool = pool; 206 | this.queues = Arrays.asList(queues); 207 | this.REDIS_CONN_MODE = this.REDIS_CONN_DEFAULT; 208 | } 209 | 210 | /** 211 | * 创建Builder对象 212 | * 213 | * @param host redis host 214 | * @param port redis port 215 | * @param queues 所要创建的队列名称,可以传多个 216 | */ 217 | public Builder(String host, int port, String... queues) { 218 | Assert.notNull(host, "Param host can't null"); 219 | Assert.notNull(port, "Param port can't null"); 220 | 221 | this.aliveTimeout = Long.MAX_VALUE; 222 | this.host = host; 223 | this.port = port; 224 | this.queues = Arrays.asList(queues); 225 | this.REDIS_CONN_MODE = this.REDIS_CONN_SINGLE; 226 | } 227 | 228 | /** 229 | * 创建Builder对象 230 | *

231 | * 采用主从复制的方式创建redis连接池 232 | * 233 | * @param hostPort 逗号分隔的 host:port 列表 234 | * @param isSentinel 是否是主从复制 235 | * @param queues 所要创建的队列名称,可以传多个 236 | */ 237 | public Builder(String hostPort, boolean isSentinel, String... queues) { 238 | Assert.isTrue(isSentinel, "Param isSentinel invalid"); 239 | Assert.notNull(hostPort, "Param hostPort can't null"); 240 | 241 | this.aliveTimeout = Long.MAX_VALUE; 242 | this.sentinels = new HashSet<>(); 243 | sentinels.addAll(Arrays.asList(hostPort.split(","))); 244 | this.queues = Arrays.asList(queues); 245 | this.REDIS_CONN_MODE = this.REDIS_CONN_SENTINEL; 246 | } 247 | 248 | /** 249 | * 设置redis连接池最大分配的连接数 250 | *

251 | * 对使用{@link #Builder(Pool, String...)}构造的Builder不起作用 252 | * 253 | * @param poolMaxTotal 连接池最大分配的连接数 254 | * @return 返回Builder 255 | */ 256 | public Builder setMaxTotal(Integer poolMaxTotal) { 257 | Assert.greaterThanEquals(poolMaxTotal, 0, "Param poolMaxTotal is negative"); 258 | this.poolMaxTotal = poolMaxTotal; 259 | return this; 260 | } 261 | 262 | /** 263 | * 设置redis连接池的最大空闲连接数 264 | *

265 | * 对使用{@link #Builder(Pool, String...)}构造的Builder不起作用 266 | * 267 | * @param poolMaxIdle 连接池的最大空闲连接数 268 | * @return 返回Builder 269 | */ 270 | public Builder setMaxIdle(Integer poolMaxIdle) { 271 | Assert.greaterThanEquals(poolMaxIdle, 0, "Param poolMaxIdle is negative"); 272 | this.poolMaxIdle = poolMaxIdle; 273 | return this; 274 | } 275 | 276 | /** 277 | * 设置redis获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted), 278 | * 如果超时就抛异常,小于零则阻塞不确定的时间,默认-1 279 | *

280 | * 对使用{@link #Builder(Pool, String...)}构造的Builder不起作用 281 | * 282 | * @param poolMaxWaitMillis 获取连接时的最大等待毫秒数 283 | * @return 返回Builder 284 | */ 285 | public Builder setMaxWaitMillis(Long poolMaxWaitMillis) { 286 | this.poolMaxWaitMillis = poolMaxWaitMillis; 287 | return this; 288 | } 289 | 290 | /** 291 | * 设置任务的存活超时时间,传0 则采用默认值: Long.MAX_VALUE 292 | * 293 | * @param aliveTimeout 任务的存活时间 294 | * @return 返回Builder 295 | */ 296 | public Builder setAliveTimeout(long aliveTimeout) { 297 | Assert.greaterThanEquals(aliveTimeout, 0, "Param aliveTimeout is negative"); 298 | if (aliveTimeout == 0) { 299 | aliveTimeout = Long.MAX_VALUE; 300 | } 301 | this.aliveTimeout = aliveTimeout; 302 | return this; 303 | } 304 | 305 | public KMQueueManager build() { 306 | 307 | KMQueueManager queueManager = new KMQueueManager(); 308 | 309 | JedisPoolConfig jedisPoolConfig = null; 310 | switch (REDIS_CONN_MODE) { 311 | case REDIS_CONN_DEFAULT: 312 | break; 313 | case REDIS_CONN_SINGLE: 314 | jedisPoolConfig = new JedisPoolConfig(); 315 | jedisPoolConfig.setMaxTotal(this.poolMaxTotal); 316 | jedisPoolConfig.setMaxIdle(this.poolMaxIdle); 317 | jedisPoolConfig.setMaxWaitMillis(this.poolMaxWaitMillis); 318 | this.pool = new JedisPool(jedisPoolConfig, host, port); 319 | break; 320 | case REDIS_CONN_SENTINEL: 321 | jedisPoolConfig = new JedisPoolConfig(); 322 | jedisPoolConfig.setMaxTotal(this.poolMaxTotal); 323 | jedisPoolConfig.setMaxIdle(this.poolMaxIdle); 324 | jedisPoolConfig.setMaxWaitMillis(this.poolMaxWaitMillis); 325 | this.pool = new JedisSentinelPool("master", sentinels, jedisPoolConfig); 326 | break; 327 | } 328 | queueManager.pool = this.pool; 329 | queueManager.queues = this.queues; 330 | queueManager.aliveTimeout = this.aliveTimeout; 331 | return queueManager; 332 | } 333 | } 334 | } 335 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/queue/RedisTaskQueue.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.kingsoft.wps.mail.distributed.lock.DistributedLock; 5 | import com.kingsoft.wps.mail.queue.config.Constant; 6 | import redis.clients.jedis.Jedis; 7 | 8 | import java.util.List; 9 | import java.util.logging.Logger; 10 | 11 | /** 12 | * Created by 刘春龙 on 2017/3/3. 13 | *

14 | * 任务队列Redis实现
15 | */ 16 | public class RedisTaskQueue extends TaskQueue { 17 | 18 | private static final Logger logger = Logger.getLogger(RedisTaskQueue.class.getName()); 19 | 20 | private static final int REDIS_DB_IDX = 0; 21 | 22 | /** 23 | * 任务队列名称 24 | */ 25 | private final String name; 26 | 27 | /** 28 | * 队列模式:DEFAULT - 简单队列,SAFE - 安全队列 29 | */ 30 | private final String mode; 31 | 32 | /** 33 | * 队列管理器 34 | */ 35 | private KMQueueAdapter kmQueueAdapter; 36 | 37 | /** 38 | * 构造函数 39 | * 40 | * @param kmQueueAdapter 队列管理器 41 | * @param name 任务队列名称 42 | * @param mode 队列模式 43 | */ 44 | public RedisTaskQueue(KMQueueAdapter kmQueueAdapter, String name, String mode) { 45 | this.kmQueueAdapter = kmQueueAdapter; 46 | if (mode == null || "".equals(mode)) { 47 | mode = KMQueueManager.DEFAULT; 48 | } 49 | this.name = name; 50 | this.mode = mode; 51 | } 52 | 53 | @Override 54 | public String getName() { 55 | return this.name; 56 | } 57 | 58 | @Override 59 | public String getMode() { 60 | return this.mode; 61 | } 62 | 63 | /** 64 | * 向任务队列中插入任务 65 | *

66 | * 如果插入任务成功,则返回该任务,失败,则返回null 67 | *

68 | * 特别的,对于唯一性任务,如果该任务在队列已经存在,则返回null 69 | * 70 | * @param task 队列任务 71 | * @return 插入的任务 72 | */ 73 | @Override 74 | public Task pushTask(Task task) { 75 | Jedis jedis = null; 76 | try { 77 | jedis = kmQueueAdapter.getResource(REDIS_DB_IDX); 78 | 79 | // 队列任务唯一性校验 80 | if (this.getMode().equals(KMQueueAdapter.SAFE) && task.isUnique()) {// 唯一性任务 81 | 82 | // Integer reply, specifically: 1 if the new element was added 0 if the element was already a member of the set 83 | Long isExist = jedis.sadd(this.name + Constant.UNIQUE_SUFFIX, task.getId()); 84 | if (isExist == 0) { 85 | return null; 86 | } 87 | } 88 | 89 | String taskJson = JSON.toJSONString(task); 90 | jedis.lpush(this.name, taskJson); 91 | return task; 92 | } catch (Throwable e) { 93 | logger.info(e.getMessage()); 94 | e.printStackTrace(); 95 | } finally { 96 | if (jedis != null) { 97 | kmQueueAdapter.returnResource(jedis); 98 | } 99 | } 100 | return null; 101 | } 102 | 103 | @Override 104 | public void pushTaskToHeader(Task task) { 105 | 106 | Jedis jedis = null; 107 | try { 108 | jedis = kmQueueAdapter.getResource(REDIS_DB_IDX); 109 | String taskJson = JSON.toJSONString(task); 110 | jedis.rpush(this.name, taskJson); 111 | } catch (Throwable e) { 112 | logger.info(e.getMessage()); 113 | e.printStackTrace(); 114 | } finally { 115 | if (jedis != null) { 116 | kmQueueAdapter.returnResource(jedis); 117 | } 118 | } 119 | 120 | } 121 | 122 | /** 123 | * 1.采用阻塞队列,以阻塞的方式(brpop)获取任务队列中的任务;
124 | * 2.判断任务存活时间是否超时(对应的是大于`aliveTimeout`);
125 | * 3.更新任务的执行时间戳,放入备份队列的队首;
126 | *

127 | * 任务状态不变,默认值为`normal` 128 | * 129 | * @return 130 | */ 131 | @Override 132 | public Task popTask() { 133 | Jedis jedis = null; 134 | Task task = null; 135 | try { 136 | jedis = kmQueueAdapter.getResource(REDIS_DB_IDX); 137 | 138 | // 判断队列模式 139 | if (KMQueueManager.SAFE.equals(getMode())) {// 安全队列 140 | // 1.采用阻塞队列,获取任务队列中的任务(brpop); 141 | List result = jedis.brpop(0, getName()); 142 | task = JSON.parseObject(result.get(1), Task.class); 143 | 144 | // 2.判断任务存活时间是否超时(对应的是大于`aliveTimeout`); 145 | Task.TaskStatus status = task.getTaskStatus();// 获取任务状态 146 | long taskGenTimeMillis = status.getGenTimestamp();// 任务生成的时间戳 147 | long currentTimeMillis = System.currentTimeMillis();// 当前时间戳 148 | long intervalTimeMillis = currentTimeMillis - taskGenTimeMillis;// 任务的存活时间 149 | if (intervalTimeMillis <= kmQueueAdapter.getAliveTimeout()) {// 如果大于存活超时时间,则不再执行 150 | // 3.更新任务的执行时间戳,放入备份队列的队首; 151 | task.getTaskStatus().setExcTimestamp(System.currentTimeMillis());// 更新任务的执行时间戳 152 | jedis.lpush(kmQueueAdapter.getBackUpQueueName(), JSON.toJSONString(task)); 153 | } 154 | } else if (KMQueueManager.DEFAULT.equals(getMode())) {// 简单队列 155 | List result = jedis.brpop(0, getName()); 156 | String taskJson = result.get(1); 157 | task = JSON.parseObject(taskJson, Task.class); 158 | } 159 | } catch (Throwable e) { 160 | logger.info(e.getMessage()); 161 | e.printStackTrace(); 162 | } finally { 163 | if (jedis != null) { 164 | kmQueueAdapter.returnResource(jedis); 165 | } 166 | } 167 | return task; 168 | } 169 | 170 | @Override 171 | public void finishTask(Task task) { 172 | if (KMQueueManager.SAFE.equals(getMode())) { 173 | // 安全队列 174 | Jedis jedis = null; 175 | try { 176 | jedis = kmQueueAdapter.getResource(REDIS_DB_IDX); 177 | String taskJson = JSON.toJSONString(task); 178 | 179 | // 删除备份队列中的任务 180 | jedis.lrem(kmQueueAdapter.getBackUpQueueName(), 0, taskJson); 181 | 182 | // 删除该任务的存在标记 183 | jedis.srem(this.name + Constant.UNIQUE_SUFFIX, task.getId()); 184 | } catch (Throwable e) { 185 | logger.info(e.getMessage()); 186 | e.printStackTrace(); 187 | } finally { 188 | if (jedis != null) { 189 | kmQueueAdapter.returnResource(jedis); 190 | } 191 | } 192 | } 193 | } 194 | 195 | } 196 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/queue/Task.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue; 2 | 3 | import com.kingsoft.wps.mail.queue.config.Constant; 4 | 5 | import java.io.Serializable; 6 | import java.util.UUID; 7 | 8 | /** 9 | * Created by 刘春龙 on 2018/1/18. 10 | */ 11 | public class Task implements Serializable { 12 | 13 | /** 14 | * 任务队列名称 15 | */ 16 | private String queue; 17 | 18 | /** 19 | * 任务唯一标识,默认采用UUID 20 | * 21 | * 如果业务需要区分队列任务的唯一性,请自行生成uid参数, 22 | */ 23 | private String id; 24 | 25 | /** 26 | * 任务类型 27 | */ 28 | private String type; 29 | 30 | /** 31 | * 任务数据 32 | */ 33 | private String data; 34 | 35 | /** 36 | * 队列中任务是否可以存在多个相同任务,以id作为同一任务的标识 37 | */ 38 | private boolean isUnique; 39 | 40 | /** 41 | * 任务状态 42 | */ 43 | private TaskStatus status; 44 | 45 | private Task() { 46 | } 47 | 48 | /** 49 | * 构造任务实体 50 | * 51 | * @param queue 任务队列 52 | * @param uid 任务的唯一标识,传null,则默认使用uuid生成策略 53 | * 如果业务需要区分队列任务的唯一性,请自行生成uid参数, 54 | * 否则队列默认使用uuid生成策略,这会导致即使data数据完全相同的任务也会被当作两个不同的任务处理。 55 | * @param type 任务类型,用于业务逻辑的处理,你可以根据不同的type任务类型,调用不同的handler去处理,可以不传。 56 | * @param data 任务数据 57 | * @param status 任务状态 58 | */ 59 | public Task(String queue, String uid, String type, String data, TaskStatus status) { 60 | this(queue, uid, false, type, data, status); 61 | } 62 | 63 | /** 64 | * 构造任务实体 65 | * 66 | * @param queue 任务队列 67 | * @param uid 任务的唯一标识,传null,则默认使用uuid生成策略 68 | * 如果业务需要区分队列任务的唯一性,请自行生成uid参数, 69 | * 否则队列默认使用uuid生成策略,这会导致即使data数据完全相同的任务也会被当作两个不同的任务处理。 70 | * @param isUnique 是否是唯一任务,即队列中同一时刻只存在一个该任务。 71 | * 只有当该任务所属队列是安全队列时才生效; 72 | * 如果为true,则通过uid判断任务的唯一性。 73 | * @param type 任务类型,用于业务逻辑的处理,你可以根据不同的type任务类型,调用不同的handler去处理,可以不传。 74 | * @param data 任务数据 75 | * @param status 任务状态 76 | */ 77 | public Task(String queue, String uid, boolean isUnique, String type, String data, TaskStatus status) { 78 | this.queue = queue; 79 | if (uid == null || "".equals(uid)) { 80 | uid = UUID.randomUUID().toString(); 81 | } 82 | this.id = uid; 83 | this.type = type; 84 | this.isUnique = isUnique; 85 | this.data = data; 86 | this.status = status; 87 | } 88 | 89 | /** 90 | * 队列中任务是否可以存在多个相同任务,以id作为同一任务的标识 91 | * 92 | * @return 是否是唯一性任务 93 | */ 94 | public boolean isUnique() { 95 | return isUnique; 96 | } 97 | 98 | /** 99 | * 队列中任务是否可以存在多个相同任务,以id作为同一任务的标识 100 | * 101 | * @param isUnique 是否是唯一性任务 102 | */ 103 | public void setUnique(boolean isUnique) { 104 | this.isUnique = isUnique; 105 | } 106 | 107 | /** 108 | * 获取任务队列名称 109 | * 110 | * @return 任务队列名称 111 | */ 112 | public String getQueue() { 113 | return queue; 114 | } 115 | 116 | public void setQueue(String queue) { 117 | this.queue = queue; 118 | } 119 | 120 | public String getId() { 121 | return id; 122 | } 123 | 124 | public void setId(String id) { 125 | this.id = id; 126 | } 127 | 128 | public String getType() { 129 | return type; 130 | } 131 | 132 | public void setType(String type) { 133 | this.type = type; 134 | } 135 | 136 | public String getData() { 137 | return data; 138 | } 139 | 140 | public void setData(String data) { 141 | this.data = data; 142 | } 143 | 144 | public TaskStatus getTaskStatus() { 145 | return status; 146 | } 147 | 148 | public void setTaskStatus(TaskStatus status) { 149 | this.status = status; 150 | } 151 | 152 | public static class TaskStatus { 153 | /** 154 | * 任务状态state,normal or retry 155 | */ 156 | private String state; 157 | 158 | /** 159 | * 任务生成的时间戳,每次重试不会重置 160 | */ 161 | private long genTimestamp; 162 | 163 | /** 164 | * 任务执行的时间戳,每次重试时,都会在该任务从任务队列中取出后(开始执行前)重新设置为当前时间 165 | */ 166 | private long excTimestamp; 167 | 168 | /** 169 | * 任务超时后重试的次数 170 | */ 171 | private int retry; 172 | 173 | public TaskStatus() { 174 | this.state = Constant.NORMAL; 175 | this.genTimestamp = System.currentTimeMillis(); 176 | this.excTimestamp = 0; 177 | this.retry = 0; 178 | } 179 | 180 | public String getState() { 181 | return state; 182 | } 183 | 184 | public void setState(String state) { 185 | this.state = state; 186 | } 187 | 188 | /** 189 | * 获取任务生成的时间戳,每次重试不会重置 190 | * 191 | * @return 任务生成的时间戳 192 | */ 193 | public long getGenTimestamp() { 194 | return genTimestamp; 195 | } 196 | 197 | public void setGenTimestamp(long genTimestamp) { 198 | this.genTimestamp = genTimestamp; 199 | } 200 | 201 | /** 202 | * 获取任务执行的时间戳,每次重试时,都会在该任务从任务队列中取出后(开始执行前)重新设置为当前时间 203 | * 204 | * @return 任务的执行的时间戳 205 | */ 206 | public long getExcTimestamp() { 207 | return excTimestamp; 208 | } 209 | 210 | public void setExcTimestamp(long excTimestamp) { 211 | this.excTimestamp = excTimestamp; 212 | } 213 | 214 | public int getRetry() { 215 | return retry; 216 | } 217 | 218 | public void setRetry(int retry) { 219 | this.retry = retry; 220 | } 221 | } 222 | 223 | /** 224 | * 执行任务 225 | *

226 | * 任务状态state不变 227 | * 228 | * @param clazz 任务处理器class 229 | * @param params 业务参数 230 | */ 231 | public void doTask(KMQueueManager kmQueueManager, Class clazz, Object... params) { 232 | 233 | // 获取任务所属队列 234 | TaskQueue taskQueue = kmQueueManager.getTaskQueue(this.getQueue()); 235 | String queueMode = taskQueue.getMode(); 236 | if (KMQueueManager.SAFE.equals(queueMode)) {// 安全队列 237 | try { 238 | handleTask(clazz, params); 239 | } catch (Throwable e) { 240 | e.printStackTrace(); 241 | } 242 | // 任务执行完成,删除备份队列的相应任务 243 | taskQueue.finishTask(this); 244 | } else {// 普通队列 245 | handleTask(clazz); 246 | } 247 | } 248 | 249 | /** 250 | * 执行任务 251 | * 252 | * @param clazz 任务执行器 253 | */ 254 | private void handleTask(Class clazz, Object... params) { 255 | try { 256 | TaskHandler handler = (TaskHandler) clazz.newInstance(); 257 | handler.handle(this.data, params); 258 | } catch (InstantiationException | IllegalAccessException e) { 259 | e.printStackTrace(); 260 | } 261 | } 262 | } 263 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/queue/TaskHandler.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue; 2 | 3 | /** 4 | * Created by 刘春龙 on 2017/3/6. 5 | */ 6 | public interface TaskHandler { 7 | 8 | /** 9 | * 业务处理 10 | * @param data task任务数据 11 | * @param params 业务自定义参数 12 | */ 13 | void handle(String data, Object... params); 14 | } 15 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/queue/TaskQueue.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue; 2 | 3 | /** 4 | * Created by 刘春龙 on 2017/3/3. 5 | */ 6 | public abstract class TaskQueue { 7 | 8 | /** 9 | * 获取队列名 10 | * 11 | * @return 队列名 12 | */ 13 | public abstract String getName(); 14 | 15 | /** 16 | * 获取队列的模式:安全队列 or 默认的普通队列 17 | * 18 | * @return 队列模式 19 | */ 20 | public abstract String getMode(); 21 | 22 | /** 23 | * 往队列中添加任务 24 | * 25 | * @param task 队列任务 26 | */ 27 | public abstract Task pushTask(Task task); 28 | 29 | /** 30 | * 往队首添加任务 31 | * 32 | * @param task 队列任务 33 | */ 34 | public abstract void pushTaskToHeader(Task task); 35 | 36 | /** 37 | * 从任务队列里取任务 38 | *

39 | * 任务状态state不变,默认值为`normal` 40 | * 41 | * @return 队列任务 42 | */ 43 | public abstract Task popTask(); 44 | 45 | /** 46 | * 队列任务完成 47 | * 48 | * @param task 队列任务 49 | */ 50 | public abstract void finishTask(Task task); 51 | } 52 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/queue/backup/BackupQueue.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue.backup; 2 | 3 | import com.kingsoft.wps.mail.queue.Task; 4 | 5 | /** 6 | * Created by 刘春龙 on 2017/3/5. 7 | *

8 | * 安全队列对应的备份队列 9 | */ 10 | public abstract class BackupQueue { 11 | 12 | /** 13 | * 初始化备份队列,添加备份队列循环标记 14 | */ 15 | public abstract void initQueue(); 16 | 17 | /** 18 | * 获取队列名 19 | * 20 | * @return 队列名 21 | */ 22 | public abstract String getName(); 23 | 24 | /** 25 | * 从队尾取一个任务,然后再将其放入队首 26 | * 27 | * @return 任务 28 | */ 29 | public abstract Task popTask(); 30 | 31 | /** 32 | * 备份队列的任务完成,删除备份队列中的该任务 33 | * 34 | * @param task 超时任务 35 | */ 36 | public abstract void finishTask(Task task); 37 | } 38 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/queue/backup/RedisBackupQueue.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue.backup; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.kingsoft.wps.mail.queue.KMQueueAdapter; 5 | import com.kingsoft.wps.mail.queue.Task; 6 | import com.kingsoft.wps.mail.queue.config.Constant; 7 | import redis.clients.jedis.Jedis; 8 | import redis.clients.jedis.Transaction; 9 | 10 | import java.util.List; 11 | import java.util.logging.Logger; 12 | 13 | /** 14 | * Created by 刘春龙 on 2017/3/5. 15 | *

16 | * 备份队列 17 | */ 18 | public class RedisBackupQueue extends BackupQueue { 19 | 20 | private static final Logger logger = Logger.getLogger(RedisBackupQueue.class.getName()); 21 | 22 | private static final int REDIS_DB_IDX = 0; 23 | public static final String MARKER = "marker"; 24 | 25 | /** 26 | * 备份队列的名称 27 | */ 28 | private final String name; 29 | 30 | /** 31 | * 队列管理器 32 | */ 33 | private KMQueueAdapter kmQueueAdapter; 34 | 35 | public RedisBackupQueue(KMQueueAdapter kmQueueAdapter) { 36 | this.kmQueueAdapter = kmQueueAdapter; 37 | this.name = kmQueueAdapter.getBackUpQueueName(); 38 | } 39 | 40 | /** 41 | * 初始化备份队列,添加备份队列循环标记 42 | */ 43 | @Override 44 | public void initQueue() { 45 | Jedis jedis = null; 46 | try { 47 | jedis = kmQueueAdapter.getResource(REDIS_DB_IDX); 48 | 49 | // 创建备份队列循环标记 50 | Task.TaskStatus state = new Task.TaskStatus(); 51 | Task task = new Task(this.name, null, RedisBackupQueue.MARKER, null, state); 52 | 53 | String taskJson = JSON.toJSONString(task); 54 | 55 | // 注意分布式问题,防止备份队列添加多个循环标记 56 | // 这里使用redis的事务&乐观锁 57 | jedis.watch(this.name);// 监视当前队列 58 | boolean isExists = jedis.exists(this.name);// 查询当前队列是否存在 59 | 60 | List backQueueData = jedis.lrange(this.name, 0, -1); 61 | logger.info("========================================"); 62 | logger.info("Backup queue already exists! Queue name:" + this.name); 63 | logger.info("Backup queue[" + this.name + "]data:"); 64 | backQueueData.forEach(logger::info); 65 | logger.info("========================================"); 66 | 67 | Transaction multi = jedis.multi();// 开启事务 68 | if (!isExists) {// 只有当前队列不存在,才执行lpush 69 | multi.lpush(this.name, taskJson); 70 | List results = multi.exec(); 71 | logger.info("Thread[" + Thread.currentThread().getName() + "] - (Add backup queue loop tag) Transaction execution result:" + ((results != null && results.size() > 0) ? results.get(0) : "Fail")); 72 | } 73 | } catch (Throwable e) { 74 | logger.info(e.getMessage()); 75 | e.printStackTrace(); 76 | } finally { 77 | if (jedis != null) { 78 | kmQueueAdapter.returnResource(jedis); 79 | } 80 | } 81 | } 82 | 83 | @Override 84 | public String getName() { 85 | return name; 86 | } 87 | 88 | @Override 89 | public Task popTask() { 90 | Jedis jedis = null; 91 | Task task = null; 92 | try { 93 | jedis = kmQueueAdapter.getResource(REDIS_DB_IDX); 94 | 95 | /** 96 | * 循环取出备份队列的一个元素:从队尾取出元素,并将其放置队首 97 | */ 98 | String taskValue = jedis.rpoplpush(this.name, this.name); 99 | task = JSON.parseObject(taskValue, Task.class); 100 | } catch (Throwable e) { 101 | logger.info(e.getMessage()); 102 | e.printStackTrace(); 103 | } finally { 104 | if (jedis != null) { 105 | kmQueueAdapter.returnResource(jedis); 106 | } 107 | } 108 | return task; 109 | } 110 | 111 | @Override 112 | public void finishTask(Task task) { 113 | Jedis jedis = null; 114 | try { 115 | jedis = kmQueueAdapter.getResource(REDIS_DB_IDX); 116 | String taskJson = JSON.toJSONString(task); 117 | 118 | // 删除备份队列中的任务 119 | jedis.lrem(this.name, 0, taskJson); 120 | 121 | // 删除该任务的存在标记 122 | jedis.srem(task.getQueue() + Constant.UNIQUE_SUFFIX, task.getId()); 123 | } catch (Throwable e) { 124 | logger.info(e.getMessage()); 125 | e.printStackTrace(); 126 | } finally { 127 | if (jedis != null) { 128 | kmQueueAdapter.returnResource(jedis); 129 | } 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/queue/config/Constant.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue.config; 2 | 3 | /** 4 | * Created by 刘春龙 on 2017/3/5. 5 | */ 6 | public class Constant { 7 | 8 | // private static final String DISTR_LOCK_SUFFIX = "_lock"; 9 | // 用于队列任务唯一性标记,redis set key 10 | public static final String UNIQUE_SUFFIX = "_unique"; 11 | 12 | /** 13 | * 标记任务为正常执行状态 14 | */ 15 | public static final String NORMAL = "normal"; 16 | 17 | /** 18 | * 标记任务为重试执行状态 19 | */ 20 | public static final String RETRY = "retry"; 21 | 22 | /** 23 | * 任务的存活时间。单位:ms 24 | *

25 | * 注意,该时间是任务从创建({@code new Task(...)})到销毁的总时间 26 | *

27 | * 该值只针对安全队列起作用 28 | */ 29 | @Deprecated 30 | public static final long ALIVE_TIMEOUT = 10 * 60 * 1000; 31 | 32 | /** 33 | * 任务执行的超时时间(一次执行)。单位:ms 34 | *

35 | * 该值只针对安全队列起作用 36 | *

37 | * TODO 后续会加入心跳健康检测 38 | */ 39 | @Deprecated 40 | public static final long PROTECTED_TIMEOUT = 3 * 60 * 1000; 41 | 42 | /** 43 | * 任务重试次数 44 | */ 45 | @Deprecated 46 | public static final int RETRY_TIMES = 3; 47 | } 48 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/utils/Assert.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.utils; 2 | 3 | /** 4 | * Created by 刘春龙 on 2018/1/17. 5 | */ 6 | public class Assert { 7 | 8 | /** 9 | * Assert a boolean expression, throwing {@code IllegalArgumentException} 10 | * if the test result is {@code false}. 11 | *

Assert.isTrue(i > 0, "The value must be greater than zero");
12 | * 13 | * @param expression a boolean expression 14 | * @param message the exception message to use if the assertion fails 15 | * @throws IllegalArgumentException if expression is {@code false} 16 | */ 17 | public static void isTrue(boolean expression, String message) { 18 | if (!expression) { 19 | throw new IllegalArgumentException(message); 20 | } 21 | } 22 | 23 | /** 24 | * Assert a boolean expression, throwing {@code IllegalArgumentException} 25 | * if the test result is {@code false}. 26 | *
Assert.isTrue(i > 0);
27 | * 28 | * @param expression a boolean expression 29 | * @throws IllegalArgumentException if expression is {@code false} 30 | */ 31 | public static void isTrue(boolean expression) { 32 | isTrue(expression, "[Assertion failed] - this expression must be true"); 33 | } 34 | 35 | /** 36 | * Assert that an object is {@code null} . 37 | *
Assert.isNull(value, "The value must be null");
38 | * 39 | * @param object the object to check 40 | * @param message the exception message to use if the assertion fails 41 | * @throws IllegalArgumentException if the object is not {@code null} 42 | */ 43 | public static void isNull(Object object, String message) { 44 | if (object != null) { 45 | throw new IllegalArgumentException(message); 46 | } 47 | } 48 | 49 | /** 50 | * Assert that an object is {@code null} . 51 | *
Assert.isNull(value);
52 | * 53 | * @param object the object to check 54 | * @throws IllegalArgumentException if the object is not {@code null} 55 | */ 56 | public static void isNull(Object object) { 57 | isNull(object, "[Assertion failed] - the object argument must be null"); 58 | } 59 | 60 | /** 61 | * Assert that an object is not {@code null} . 62 | *
Assert.notNull(clazz, "The class must not be null");
63 | * 64 | * @param object the object to check 65 | * @param message the exception message to use if the assertion fails 66 | * @throws IllegalArgumentException if the object is {@code null} 67 | */ 68 | public static void notNull(Object object, String message) { 69 | if (object == null) { 70 | throw new IllegalArgumentException(message); 71 | } 72 | } 73 | 74 | /** 75 | * Assert that an object is not {@code null} . 76 | *
Assert.notNull(clazz);
77 | * 78 | * @param object the object to check 79 | * @throws IllegalArgumentException if the object is {@code null} 80 | */ 81 | public static void notNull(Object object) { 82 | notNull(object, "[Assertion failed] - this argument is required; it must not be null"); 83 | } 84 | 85 | /** 86 | * Assert that an array has elements; that is, it must not be 87 | * {@code null} and must have at least one element. 88 | *
Assert.notEmpty(array, "The array must have elements");
89 | * 90 | * @param array the array to check 91 | * @param message the exception message to use if the assertion fails 92 | * @throws IllegalArgumentException if the object array is {@code null} or has no elements 93 | */ 94 | public static void notEmpty(Object[] array, String message) { 95 | if (array == null || array.length == 0) { 96 | throw new IllegalArgumentException(message); 97 | } 98 | } 99 | 100 | /** 101 | * Assert that an array has elements; that is, it must not be 102 | * {@code null} and must have at least one element. 103 | *
Assert.notEmpty(array);
104 | * 105 | * @param array the array to check 106 | * @throws IllegalArgumentException if the object array is {@code null} or has no elements 107 | */ 108 | public static void notEmpty(Object[] array) { 109 | notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element"); 110 | } 111 | 112 | /** 113 | * 第一个参数必须大于第二个参数 114 | * 115 | * @param value 校验的值 116 | * @param minValue 最小值 117 | * @param message 错误提示 118 | */ 119 | public static void greaterThanEquals(int value, int minValue, String message) { 120 | if (value < minValue) { 121 | throw new IllegalArgumentException(message); 122 | } 123 | } 124 | 125 | /** 126 | * 第一个参数必须大于第二个参数 127 | * 128 | * @param value 校验的值 129 | * @param minValue 最小值 130 | */ 131 | public static void greaterThanEquals(int value, int minValue) { 132 | greaterThanEquals(value, minValue, "The first parameter must be greater than the second parameter"); 133 | } 134 | 135 | /** 136 | * 第一个参数必须大于第二个参数 137 | * 138 | * @param value 校验的值 139 | * @param minValue 最小值 140 | * @param message 错误提示 141 | */ 142 | public static void greaterThanEquals(long value, long minValue, String message) { 143 | if (value < minValue) { 144 | throw new IllegalArgumentException(message); 145 | } 146 | } 147 | 148 | /** 149 | * 第一个参数必须大于第二个参数 150 | * 151 | * @param value 校验的值 152 | * @param minValue 最小值 153 | */ 154 | public static void greaterThanEquals(long value, long minValue) { 155 | greaterThanEquals(value, minValue, "The first parameter must be greater than the second parameter"); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /queue-core/src/main/java/com/kingsoft/wps/mail/utils/KMQUtils.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.utils; 2 | 3 | import com.kingsoft.wps.mail.queue.KMQueueAdapter; 4 | import sun.misc.BASE64Encoder; 5 | 6 | import java.io.UnsupportedEncodingException; 7 | import java.security.MessageDigest; 8 | import java.security.NoSuchAlgorithmException; 9 | import java.util.Arrays; 10 | import java.util.List; 11 | 12 | /** 13 | * Created by 刘春龙 on 2018/1/22. 14 | */ 15 | public class KMQUtils { 16 | 17 | /** 18 | * 生成备份队列的名称 19 | * 20 | * @param queues 备份队列所对应的任务队列 21 | * @return 备份队列的名称 22 | */ 23 | public static String genBackUpQueueName(String ...queues) { 24 | // 生成备份队列名称 25 | try { 26 | MessageDigest md5Digest = MessageDigest.getInstance("MD5"); 27 | BASE64Encoder base64Encoder = new BASE64Encoder(); 28 | 29 | // 获取队列名称 30 | StringBuilder queueNameMulti = new StringBuilder(); 31 | // Stream 是支持并发操作的,为了避免竞争,对于reduce线程都会有独立的result,combiner的作用在于合并每个线程的result得到最终结果 32 | queueNameMulti = Arrays.stream(queues) 33 | .map(s -> s.trim().split(":")[0]) 34 | .reduce(queueNameMulti, 35 | StringBuilder::append, 36 | StringBuilder::append); 37 | try { 38 | return KMQueueAdapter.BACK_UP_QUEUE_PREFIX + base64Encoder.encode(md5Digest.digest(queueNameMulti.toString().getBytes("UTF-8"))); 39 | } catch (UnsupportedEncodingException e) { 40 | e.printStackTrace(); 41 | } 42 | } catch (NoSuchAlgorithmException e) { 43 | e.printStackTrace(); 44 | } 45 | return ""; 46 | } 47 | 48 | public static String genBackUpQueueName(List queues) { 49 | return genBackUpQueueName((String[]) queues.toArray()); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /queue-core/src/test/java/com/kingsoft/wps/mail/JedisTest.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | import redis.clients.jedis.Jedis; 6 | import redis.clients.jedis.JedisPool; 7 | import redis.clients.jedis.JedisPoolConfig; 8 | import redis.clients.util.Pool; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * Created by 刘春龙 on 2018/1/19. 14 | */ 15 | public class JedisTest { 16 | 17 | private Pool pool; 18 | 19 | @Before 20 | public void init() { 21 | JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 22 | this.pool = new JedisPool(jedisPoolConfig, "127.0.0.1", 6379); 23 | } 24 | 25 | @Test 26 | public void brpopTest() { 27 | // List rs = this.pool.getResource().brpop(0, "testList"); 28 | // System.out.println(rs);// [testList, liucl] 29 | } 30 | 31 | @Test 32 | public void sremTest() { 33 | Long rs = this.pool.getResource().srem("worker1_queue_unique", "a509bd99-1071-4de1-9220-a280b0a4f47a"); 34 | System.out.println(rs); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /queue-core/src/test/java/com/kingsoft/wps/mail/MyTaskHandler.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail; 2 | 3 | import com.kingsoft.wps.mail.queue.TaskHandler; 4 | 5 | /** 6 | * Created by 刘春龙 on 2018/1/19. 7 | */ 8 | public class MyTaskHandler implements TaskHandler { 9 | @Override 10 | public void handle(String data, Object... params) { 11 | System.out.println("获取任务数据:" + data); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /queue-core/src/test/java/com/kingsoft/wps/mail/QueueTest.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONObject; 5 | import com.kingsoft.wps.mail.queue.KMQueueManager; 6 | import com.kingsoft.wps.mail.queue.Task; 7 | import com.kingsoft.wps.mail.queue.TaskQueue; 8 | import com.kingsoft.wps.mail.queue.config.Constant; 9 | import org.junit.Test; 10 | 11 | import java.util.logging.Logger; 12 | 13 | /** 14 | * Created by 刘春龙 on 2018/1/19. 15 | */ 16 | public class QueueTest { 17 | 18 | private static final Logger logger = Logger.getLogger(QueueTest.class.getName()); 19 | 20 | @Test 21 | public void pushTaskTest() { 22 | KMQueueManager kmQueueManager = new KMQueueManager.Builder("127.0.0.1", 6379, "worker1_queue", "worker2_queue:safe") 23 | .setMaxWaitMillis(-1L) 24 | .setMaxTotal(600) 25 | .setMaxIdle(300) 26 | .setAliveTimeout(Constant.ALIVE_TIMEOUT) 27 | .build(); 28 | // 初始化队列 29 | kmQueueManager.init(); 30 | 31 | // 1.获取队列 32 | TaskQueue taskQueue = kmQueueManager.getTaskQueue("worker1_queue"); 33 | // 2.创建任务 34 | JSONObject ob = new JSONObject(); 35 | ob.put("data", "mail proxy task"); 36 | String data = JSON.toJSONString(ob); 37 | // 参数 uid:如果业务需要区分队列任务的唯一性,请自行生成uid参数, 38 | // 否则队列默认使用uuid生成策略,这会导致即使data数据完全相同的任务也会被当作两个不同的任务处理。 39 | // 参数 type:用于业务逻辑的处理,你可以根据不同的type任务类型,调用不同的handler去处理,可以不传。 40 | Task task = new Task(taskQueue.getName(), "a509bd99-1071-4de1-9220-a280b0a4f47a", true, "", data, new Task.TaskStatus()); 41 | // 3.将任务加入队列 42 | Task rs = taskQueue.pushTask(task); 43 | logger.info("pushTask result:" + JSON.toJSONString(rs)); 44 | } 45 | 46 | @Test 47 | public void popTaskTest() { 48 | KMQueueManager kmQueueManager = new KMQueueManager.Builder("127.0.0.1", 6379, "worker1_queue", "worker2_queue:safe") 49 | .setMaxWaitMillis(-1L) 50 | .setMaxTotal(600) 51 | .setMaxIdle(300) 52 | .setAliveTimeout(Constant.ALIVE_TIMEOUT) 53 | .build(); 54 | // 初始化队列 55 | kmQueueManager.init(); 56 | 57 | // 1.获取队列 58 | TaskQueue taskQueue = kmQueueManager.getTaskQueue("worker1_queue"); 59 | // 2.获取任务 60 | Task task = taskQueue.popTask(); 61 | // 业务处理放到TaskConsumersHandler里 62 | if (task != null) { 63 | task.doTask(kmQueueManager, MyTaskHandler.class); 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /queue-extension/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | queue 7 | com.kingsoft.wps.mail 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | queue-extension 13 | 14 | 15 | 16 | com.kingsoft.wps.mail 17 | queue-core 18 | 1.0-SNAPSHOT 19 | 20 | 21 | -------------------------------------------------------------------------------- /queue-extension/src/main/java/com/kingsoft/wps/mail/queue/extension/monitor/AliveDetectHandler.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue.extension.monitor; 2 | 3 | import com.kingsoft.wps.mail.queue.Task; 4 | 5 | /** 6 | * Created by 刘春龙 on 2018/1/23. 7 | *

8 | * 健康检查 9 | *

10 | * 检查正在执行的任务是否还在执行(存活), 11 | * 为了防止耗时比较久的任务(任务的执行时间超出了通过队列管理器配置的任务执行超时时间 - 默认值:{@link com.kingsoft.wps.mail.queue.config.Constant#PROTECTED_TIMEOUT}) 12 | * 会被备份队列监听器检测到并重新放入任务队列执行(因为备份队列监听器会把超出通过队列管理器配置的任务执行超时时间的任务当作是失败的任务(参考 https://github.com/fnpac/KMQueue#什么是失败任务?)并进行重试)。 13 | *

14 | * 通过这种检测机制,可以保证{@link #check(Task)}返回为true的任务(任务还在执行)不会被备份队列监听器重新放入任务队列重试。 15 | *

16 | * 这里只是提供一个接口,用户需要自己实现执行任务的健康检测。 17 | * 一个比较简单的实现方式就是起一个定时job,每隔n毫秒检查线程中正在执行任务的状态,在redis中以 "任务的id + {@link AliveDetectHandler#ALIVE_KEY_SUFFIX}" 为key,ttl 为 n+m 毫秒(m < n, m用于保证两次job的空窗期),标记正在执行的任务。 18 | * 然后{@link AliveDetectHandler}的实现类根据task去检查redis中是否存在该key,如果存在,返回true 19 | */ 20 | public interface AliveDetectHandler { 21 | 22 | public static final String ALIVE_KEY_SUFFIX = "_alive"; 23 | 24 | /** 25 | * 健康检查 26 | *

27 | * 任务正在执行返回true,否则(任务挂了、任务执行完成)返回false 28 | * 29 | * @param monitor 份队列监听器 30 | * @param task 要检查的任务 31 | * @return 检查结果,任务正在执行返回true,否则(任务挂了、任务执行完成)返回false 32 | */ 33 | boolean check(BackupQueueMonitor monitor, Task task); 34 | } 35 | -------------------------------------------------------------------------------- /queue-extension/src/main/java/com/kingsoft/wps/mail/queue/extension/monitor/BackupQueueMonitor.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue.extension.monitor; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.kingsoft.wps.mail.queue.*; 5 | import com.kingsoft.wps.mail.queue.backup.BackupQueue; 6 | import com.kingsoft.wps.mail.queue.backup.RedisBackupQueue; 7 | import com.kingsoft.wps.mail.queue.config.Constant; 8 | import com.kingsoft.wps.mail.utils.Assert; 9 | import redis.clients.jedis.Jedis; 10 | import redis.clients.jedis.JedisPool; 11 | import redis.clients.jedis.JedisPoolConfig; 12 | import redis.clients.jedis.JedisSentinelPool; 13 | import redis.clients.util.Pool; 14 | 15 | import java.text.DateFormat; 16 | import java.text.SimpleDateFormat; 17 | import java.util.Arrays; 18 | import java.util.Date; 19 | import java.util.HashSet; 20 | import java.util.Set; 21 | import java.util.logging.Logger; 22 | 23 | /** 24 | * Created by 刘春龙 on 2017/3/5. 25 | *

26 | * 备份队列监控 27 | *

28 | * 超时任务重试 29 | */ 30 | public class BackupQueueMonitor extends KMQueueAdapter { 31 | 32 | private static final Logger logger = Logger.getLogger(BackupQueueMonitor.class.getName()); 33 | 34 | /** 35 | * 任务超时重试次数 36 | */ 37 | private int retryTimes; 38 | 39 | /** 40 | * 失败任务(重试三次失败)的处理方式 41 | */ 42 | private Pipeline pipeline; 43 | 44 | /** 45 | * 任务的存活超时时间。注意,该时间是任务从创建({@code new Task(...)})到销毁的总时间。单位:ms 46 | *

47 | * 该值只针对安全队列起作用 48 | *

49 | * 不设置默认为 Long.MAX_VALUE 50 | */ 51 | private long aliveTimeout; 52 | 53 | /** 54 | * 任务执行的超时时间(一次执行),单位:ms 55 | *

56 | * 该值只针对安全队列起作用 57 | *

58 | * 不设置默认为 Long.MAX_VALUE 59 | */ 60 | private long protectedTimeout; 61 | 62 | /** 63 | * 健康检查 64 | */ 65 | private AliveDetectHandler aliveDetectHandler; 66 | 67 | /** 68 | * 备份队列 69 | */ 70 | private BackupQueue backupQueue; 71 | 72 | /** 73 | * 构造方法私有化,防止外部调用 74 | */ 75 | private BackupQueueMonitor() { 76 | } 77 | 78 | public int getRetryTimes() { 79 | return retryTimes; 80 | } 81 | 82 | /** 83 | * 任务的存活超时时间。注意,该时间是任务从创建({@code new Task(...)})到销毁的总时间。单位:ms 84 | *

85 | * 该值只针对安全队列起作用 86 | *

87 | * 不设置默认为 Long.MAX_VALUE 88 | * 89 | * @return 任务的存活时间 90 | */ 91 | public long getAliveTimeout() { 92 | return aliveTimeout; 93 | } 94 | 95 | /** 96 | * 任务执行的超时时间(一次执行),单位:ms 97 | *

98 | * 该值只针对安全队列起作用 99 | *

100 | * 不设置默认为 Long.MAX_VALUE 101 | * 102 | * @return 任务执行的超时时间 103 | */ 104 | public long getProtectedTimeout() { 105 | return protectedTimeout; 106 | } 107 | 108 | /** 109 | * 启动监控 110 | */ 111 | public void monitor() { 112 | Task task; 113 | try { 114 | String backUpQueueName = this.getBackUpQueueName(); 115 | DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 116 | logger.info("Backup queue[" + backUpQueueName + "]Monitoring begins:" + format.format(new Date())); 117 | task = backupQueue.popTask(); 118 | while (task != null && 119 | !backUpQueueName.equals(task.getQueue()) && 120 | !RedisBackupQueue.MARKER.equals(task.getType())) { 121 | 122 | /** 123 | * 判断任务状态,分别处理 124 | * 1. 任务执行超时,且重试次数大于等于retry指定次数,则持久化到数据库 125 | * 2. 任务执行超时,且重试次数小于retry指定次数,则重新放入任务队列 126 | * 最后,如果满足以上条件,同时删除备份队列中的该任务 127 | */ 128 | TaskQueue taskQueue = new RedisTaskQueue(this, task.getQueue(), KMQueueManager.SAFE); 129 | // 获取任务状态 130 | Task.TaskStatus status = task.getTaskStatus(); 131 | 132 | long currentTimeMillis = System.currentTimeMillis();// 当前时间戳 133 | long taskGenTimeMillis = status.getGenTimestamp();// 任务生成的时间戳 134 | long intervalTimeMillis = currentTimeMillis - taskGenTimeMillis;// 任务的存活时间 135 | if (intervalTimeMillis > this.aliveTimeout) { 136 | if (pipeline != null) { 137 | pipeline.process(taskQueue, task);// 彻底失败任务的处理 138 | } 139 | // 删除备份队列中的该任务 140 | backupQueue.finishTask(task); 141 | } 142 | 143 | long taskExcTimeMillis = status.getExcTimestamp();// 任务执行的时间戳 144 | intervalTimeMillis = currentTimeMillis - taskExcTimeMillis;// 任务此次执行时间 145 | 146 | if (intervalTimeMillis > this.protectedTimeout) {// 任务执行超时 147 | 148 | // 增加心跳健康检测 149 | if (aliveDetectHandler != null) { 150 | 151 | boolean isAlive = aliveDetectHandler.check(this, task); 152 | if (isAlive) {// 当前任务还在执行 153 | // 继续从备份队列中取出任务,进入下一次循环 154 | task = backupQueue.popTask(); 155 | continue; 156 | } 157 | } 158 | 159 | Task originTask = JSON.parseObject(JSON.toJSONString(task), Task.class);// 保留原任务数据,用于删除该任务 160 | 161 | if (status.getRetry() < this.getRetryTimes()) { 162 | // 重新放入任务队列 163 | // 更新状态标记为retry 164 | status.setState(Constant.RETRY); 165 | // 更新重试次数retry + 1 166 | status.setRetry(status.getRetry() + 1); 167 | task.setTaskStatus(status); 168 | // 放入任务队列的队首,优先处理 169 | taskQueue.pushTaskToHeader(task); 170 | } else { 171 | if (pipeline != null) { 172 | pipeline.process(taskQueue, task);// 彻底失败任务的处理 173 | } 174 | } 175 | 176 | // 删除备份队列中的该任务 177 | backupQueue.finishTask(originTask); 178 | } 179 | // 继续从备份队列中取出任务,进入下一次循环 180 | task = backupQueue.popTask(); 181 | } 182 | 183 | } catch (Throwable e) { 184 | logger.info(e.getMessage()); 185 | e.printStackTrace(); 186 | } 187 | 188 | } 189 | 190 | /** 191 | * 构建器,用于设置初始化参数,执行初始化操作 192 | */ 193 | public static class Builder { 194 | 195 | /** 196 | * redis连接方式: 197 | *

    198 | *
  • default
  • 199 | *
  • single
  • 200 | *
  • sentinel
  • 201 | *
202 | */ 203 | private final String REDIS_CONN_DEFAULT = "default"; 204 | private final String REDIS_CONN_SINGLE = "single"; 205 | private final String REDIS_CONN_SENTINEL = "sentinel"; 206 | private String REDIS_CONN_MODE; 207 | 208 | /** 209 | * 备份队列名称 210 | */ 211 | private String backUpQueueName; 212 | 213 | /** 214 | * redis连接池 215 | */ 216 | private Pool pool; 217 | 218 | /** 219 | * redis host 220 | */ 221 | private String host; 222 | 223 | /** 224 | * redis port 225 | */ 226 | private int port; 227 | 228 | /** 229 | * 主从复制集 230 | */ 231 | private Set sentinels; 232 | 233 | /** 234 | * 连接池最大分配的连接数 235 | */ 236 | private Integer poolMaxTotal; 237 | 238 | /** 239 | * 连接池的最大空闲连接数 240 | */ 241 | private Integer poolMaxIdle; 242 | 243 | /** 244 | * redis获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常,小于零则阻塞不确定的时间,默认-1 245 | */ 246 | private Long poolMaxWaitMillis; 247 | 248 | /** 249 | * 任务超时重试次数,默认3次 250 | */ 251 | private int retryTimes; 252 | 253 | /** 254 | * 失败任务(重试三次失败)的处理方式 255 | */ 256 | private Pipeline pipeline; 257 | 258 | /** 259 | * 任务的存活超时时间。注意,该时间是任务从创建({@code new Task(...)})到销毁的总时间。单位:ms 260 | *

261 | * 该值只针对安全队列起作用 262 | *

263 | * 不设置默认为 Long.MAX_VALUE 264 | */ 265 | private long aliveTimeout; 266 | 267 | /** 268 | * 任务执行的超时时间(一次执行),单位:ms 269 | *

270 | * 该值只针对安全队列起作用 271 | *

272 | * 不设置默认为 Long.MAX_VALUE 273 | */ 274 | private long protectedTimeout; 275 | 276 | /** 277 | * 健康检查 278 | */ 279 | private AliveDetectHandler aliveDetectHandler; 280 | 281 | /** 282 | * 创建Builder对象 283 | *

284 | * 使用指定的redis连接池 285 | * 286 | * @param pool redis连接池 287 | * @param backUpQueueName 备份队列名称 288 | */ 289 | public Builder(Pool pool, String backUpQueueName) { 290 | Assert.notNull(pool, "Param pool can't null"); 291 | Assert.notNull(backUpQueueName, "Param backUpQueueName can't null"); 292 | 293 | this.retryTimes = 3; 294 | this.aliveTimeout = Long.MAX_VALUE; 295 | this.protectedTimeout = Long.MAX_VALUE; 296 | this.pool = pool; 297 | this.backUpQueueName = backUpQueueName; 298 | this.REDIS_CONN_MODE = this.REDIS_CONN_DEFAULT; 299 | } 300 | 301 | /** 302 | * 创建Builder对象 303 | * 304 | * @param host redis host 305 | * @param port redis port 306 | * @param backUpQueueName 备份队列名称 307 | */ 308 | public Builder(String host, int port, String backUpQueueName) { 309 | Assert.notNull(host, "Param host can't null"); 310 | Assert.notNull(port, "Param port can't null"); 311 | Assert.notNull(backUpQueueName, "Param backUpQueueName can't null"); 312 | 313 | this.retryTimes = 3; 314 | this.aliveTimeout = Long.MAX_VALUE; 315 | this.protectedTimeout = Long.MAX_VALUE; 316 | this.host = host; 317 | this.port = port; 318 | this.backUpQueueName = backUpQueueName; 319 | this.REDIS_CONN_MODE = this.REDIS_CONN_SINGLE; 320 | } 321 | 322 | /** 323 | * 创建Builder对象 324 | *

325 | * 采用主从复制的方式创建redis连接池 326 | * 327 | * @param hostPort 逗号分隔的 host:port 列表 328 | * @param isSentinel 是否是主从复制 329 | * @param backUpQueueName 备份队列名称 330 | */ 331 | public Builder(String hostPort, boolean isSentinel, String backUpQueueName) { 332 | Assert.isTrue(isSentinel, "Param isSentinel invalid"); 333 | Assert.notNull(hostPort, "Param hostPort can't null"); 334 | Assert.notNull(backUpQueueName, "Param backUpQueueName can't null"); 335 | 336 | this.retryTimes = 3; 337 | this.aliveTimeout = Long.MAX_VALUE; 338 | this.protectedTimeout = Long.MAX_VALUE; 339 | this.sentinels = new HashSet<>(); 340 | sentinels.addAll(Arrays.asList(hostPort.split(","))); 341 | this.backUpQueueName = backUpQueueName; 342 | this.REDIS_CONN_MODE = this.REDIS_CONN_SENTINEL; 343 | } 344 | 345 | /** 346 | * 设置redis连接池最大分配的连接数 347 | *

348 | * 对使用{@link #Builder(Pool, String)}构造的Builder不起作用 349 | * 350 | * @param poolMaxTotal 连接池最大分配的连接数 351 | * @return 返回Builder 352 | */ 353 | public Builder setMaxTotal(Integer poolMaxTotal) { 354 | if (poolMaxTotal <= 0) { 355 | throw new IllegalArgumentException("Param poolMaxTotal invalid"); 356 | } 357 | this.poolMaxTotal = poolMaxTotal; 358 | return this; 359 | } 360 | 361 | /** 362 | * 设置redis连接池的最大空闲连接数 363 | *

364 | * 对使用{@link #Builder(Pool, String)}构造的Builder不起作用 365 | * 366 | * @param poolMaxIdle 连接池的最大空闲连接数 367 | * @return 返回Builder 368 | */ 369 | public Builder setMaxIdle(Integer poolMaxIdle) { 370 | if (poolMaxIdle < 0) { 371 | throw new IllegalArgumentException("Param poolMaxIdle invalid"); 372 | } 373 | this.poolMaxIdle = poolMaxIdle; 374 | return this; 375 | } 376 | 377 | /** 378 | * 设置redis获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常,小于零:阻塞不确定的时间,默认-1 379 | *

380 | * 对使用{@link #Builder(Pool, String)}构造的Builder不起作用 381 | * 382 | * @param poolMaxWaitMillis 获取连接时的最大等待毫秒数 383 | * @return 返回Builder 384 | */ 385 | public Builder setMaxWaitMillis(Long poolMaxWaitMillis) { 386 | this.poolMaxWaitMillis = poolMaxWaitMillis; 387 | return this; 388 | } 389 | 390 | /** 391 | * 设置失败任务(重试三次失败)的处理方式 392 | * 393 | * @param pipeline 失败任务(重试三次失败)的处理方式 394 | * @return 返回Builder 395 | */ 396 | public Builder setPipeline(Pipeline pipeline) { 397 | this.pipeline = pipeline; 398 | return this; 399 | } 400 | 401 | /** 402 | * 任务超时重试次数,默认3次 403 | * 404 | * @param retryTimes 任务超时重试次数 405 | * @return 返回Builder 406 | */ 407 | public Builder setRetryTimes(int retryTimes) { 408 | Assert.greaterThanEquals(retryTimes, 0, "Param retryTimes is negative"); 409 | this.retryTimes = retryTimes; 410 | return this; 411 | } 412 | 413 | /** 414 | * 设置任务的存活超时时间。单位:ms 415 | *

416 | * 注意,该时间是任务从创建({@code new Task(...)})到销毁的总时间 417 | *

418 | * 传0 则采用默认值: Long.MAX_VALUE 419 | *

420 | * 该值只针对安全队列起作用 421 | * 422 | * @param aliveTimeout 任务的存活时间 423 | * @return 返回Builder 424 | */ 425 | public Builder setAliveTimeout(long aliveTimeout) { 426 | Assert.greaterThanEquals(aliveTimeout, 0, "Param aliveTimeout is negative"); 427 | if (aliveTimeout == 0) { 428 | aliveTimeout = Long.MAX_VALUE; 429 | } 430 | this.aliveTimeout = aliveTimeout; 431 | return this; 432 | } 433 | 434 | /** 435 | * 任务执行的超时时间(一次执行)。单位:ms 436 | *

437 | * 传0 则采用默认值: Long.MAX_VALUE 438 | *

439 | * 该值只针对安全队列起作用 440 | *

441 | * 不设置默认为 Long.MAX_VALUE 442 | * 443 | * @param protectedTimeout 任务执行的超时时间 444 | * @return 返回Builder 445 | */ 446 | public Builder setProtectedTimeout(long protectedTimeout) { 447 | Assert.greaterThanEquals(protectedTimeout, 0, "Param protectedTimeout is negative"); 448 | if (protectedTimeout == 0) { 449 | protectedTimeout = Long.MAX_VALUE; 450 | } 451 | this.protectedTimeout = protectedTimeout; 452 | return this; 453 | } 454 | 455 | /** 456 | * 注册健康检查 457 | * 458 | * @param aliveDetectHandler 健康检测实现 459 | * @return 返回Builder 460 | */ 461 | public Builder registerAliveDetectHandler(AliveDetectHandler aliveDetectHandler) { 462 | this.aliveDetectHandler = aliveDetectHandler; 463 | return this; 464 | } 465 | 466 | public BackupQueueMonitor build() { 467 | 468 | BackupQueueMonitor queueMonitor = new BackupQueueMonitor(); 469 | 470 | JedisPoolConfig jedisPoolConfig = null; 471 | switch (REDIS_CONN_MODE) { 472 | case REDIS_CONN_DEFAULT: 473 | break; 474 | case REDIS_CONN_SINGLE: 475 | jedisPoolConfig = new JedisPoolConfig(); 476 | jedisPoolConfig.setMaxTotal(this.poolMaxTotal); 477 | jedisPoolConfig.setMaxIdle(this.poolMaxIdle); 478 | jedisPoolConfig.setMaxWaitMillis(this.poolMaxWaitMillis); 479 | this.pool = new JedisPool(jedisPoolConfig, host, port); 480 | break; 481 | case REDIS_CONN_SENTINEL: 482 | jedisPoolConfig = new JedisPoolConfig(); 483 | jedisPoolConfig.setMaxTotal(this.poolMaxTotal); 484 | jedisPoolConfig.setMaxIdle(this.poolMaxIdle); 485 | jedisPoolConfig.setMaxWaitMillis(this.poolMaxWaitMillis); 486 | this.pool = new JedisSentinelPool("master", sentinels, jedisPoolConfig); 487 | break; 488 | } 489 | queueMonitor.pool = this.pool; 490 | queueMonitor.backUpQueueName = this.backUpQueueName; 491 | queueMonitor.retryTimes = this.retryTimes; 492 | queueMonitor.pipeline = this.pipeline; 493 | queueMonitor.aliveTimeout = this.aliveTimeout; 494 | queueMonitor.protectedTimeout = this.protectedTimeout; 495 | queueMonitor.aliveDetectHandler = this.aliveDetectHandler; 496 | 497 | queueMonitor.backupQueue = new RedisBackupQueue(queueMonitor);// 备份队列 498 | 499 | return queueMonitor; 500 | } 501 | } 502 | } 503 | -------------------------------------------------------------------------------- /queue-extension/src/main/java/com/kingsoft/wps/mail/queue/extension/monitor/Pipeline.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail.queue.extension.monitor; 2 | 3 | import com.kingsoft.wps.mail.queue.Task; 4 | import com.kingsoft.wps.mail.queue.TaskQueue; 5 | 6 | /** 7 | * Created by 刘春龙 on 2018/1/19. 8 | * 9 | * 失败任务(重试三次失败)的处理 10 | */ 11 | public interface Pipeline { 12 | 13 | /** 14 | * 失败任务的处理 15 | * 16 | * @param taskQueue 任务所属队列 17 | * @param task 任务 18 | */ 19 | public void process(TaskQueue taskQueue, Task task); 20 | } 21 | -------------------------------------------------------------------------------- /queue-extension/src/test/java/com/kingsoft/wps/mail/MonitorTest.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail; 2 | 3 | import com.kingsoft.wps.mail.queue.config.Constant; 4 | import com.kingsoft.wps.mail.queue.extension.monitor.BackupQueueMonitor; 5 | import com.kingsoft.wps.mail.utils.KMQUtils; 6 | import org.junit.Test; 7 | 8 | /** 9 | * Created by 刘春龙 on 2018/1/22. 10 | */ 11 | public class MonitorTest { 12 | 13 | @Test 14 | public void monitorTaskTest() { 15 | 16 | // 健康检测 17 | MyAliveDetectHandler detectHandler = new MyAliveDetectHandler(); 18 | // 任务彻底失败后的处理,需要实现Pipeline接口,自行实现处理逻辑 19 | MyPipeline pipeline = new MyPipeline(); 20 | // 根据任务队列的名称构造备份队列的名称,注意:这里的任务队列参数一定要和KMQueueManager构造时传入的一一对应。 21 | String backUpQueueName = KMQUtils.genBackUpQueueName("worker1_queue", "worker2_queue:safe"); 22 | // 构造Monitor监听器 23 | BackupQueueMonitor backupQueueMonitor = new BackupQueueMonitor.Builder("127.0.0.1", 6379, backUpQueueName) 24 | .setMaxWaitMillis(-1L) 25 | .setMaxTotal(600) 26 | .setMaxIdle(300) 27 | .setAliveTimeout(Constant.ALIVE_TIMEOUT) 28 | .setProtectedTimeout(Constant.PROTECTED_TIMEOUT) 29 | .setRetryTimes(Constant.RETRY_TIMES) 30 | .registerAliveDetectHandler(detectHandler) 31 | .setPipeline(pipeline).build(); 32 | // 执行监听 33 | backupQueueMonitor.monitor(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /queue-extension/src/test/java/com/kingsoft/wps/mail/MyAliveDetectHandler.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail; 2 | 3 | import com.kingsoft.wps.mail.queue.Task; 4 | import com.kingsoft.wps.mail.queue.extension.monitor.AliveDetectHandler; 5 | import com.kingsoft.wps.mail.queue.extension.monitor.BackupQueueMonitor; 6 | import redis.clients.jedis.Jedis; 7 | 8 | /** 9 | * Created by 刘春龙 on 2018/1/23. 10 | */ 11 | public class MyAliveDetectHandler implements AliveDetectHandler { 12 | 13 | @Override 14 | public boolean check(BackupQueueMonitor monitor, Task task) { 15 | Jedis jedis = monitor.getResource(); 16 | String value = jedis.get(task.getId() + ALIVE_KEY_SUFFIX); 17 | return value != null && !"".equals(value.trim()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /queue-extension/src/test/java/com/kingsoft/wps/mail/MyPipeline.java: -------------------------------------------------------------------------------- 1 | package com.kingsoft.wps.mail; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.kingsoft.wps.mail.queue.Task; 5 | import com.kingsoft.wps.mail.queue.TaskQueue; 6 | import com.kingsoft.wps.mail.queue.extension.monitor.Pipeline; 7 | 8 | /** 9 | * Created by 刘春龙 on 2018/1/22. 10 | */ 11 | public class MyPipeline implements Pipeline { 12 | @Override 13 | public void process(TaskQueue taskQueue, Task task) { 14 | System.out.println("Task is timeout,task - " + JSON.toJSON(task)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /基于Redis的分布式消息队列设计.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trekkiii/KMQueue/424136867c8dfaaaf6306a3ae3493381f9c82486/基于Redis的分布式消息队列设计.png --------------------------------------------------------------------------------