Commit 069be1ded11a40944a8308dc2bbf3f445b60f1ab
1 parent
a5123a44ba
Exists in
master
and in
1 other branch
update
Showing 3 changed files with 50 additions and 13 deletions
platform-operate-api/src/main/java/com/lyms/platform/operate/web/inteceptor/MybatisSqlInterceptor.java
View file @
069be1d
| ... | ... | @@ -71,7 +71,7 @@ |
| 71 | 71 | |
| 72 | 72 | System.out.println("delete or update sync sql = " + sql); |
| 73 | 73 | //发送要同步的sql |
| 74 | - SendMysqlSyncDatUtil.sendSql(sql, sqlId); | |
| 74 | + SendMysqlSyncDatUtil.addSqlToQueue(sql, sqlId); | |
| 75 | 75 | sql = null; |
| 76 | 76 | } |
| 77 | 77 | } |
| ... | ... | @@ -100,7 +100,7 @@ |
| 100 | 100 | sql = sql.substring(0,sql.lastIndexOf("(")+1)+list.get(0)+","+sql.substring(sql.lastIndexOf("(")+1,sql.length()); |
| 101 | 101 | System.out.println("add sync sql = "+sql); |
| 102 | 102 | //发送要同步的sql |
| 103 | - SendMysqlSyncDatUtil.sendSql(sql, sqlId); | |
| 103 | + SendMysqlSyncDatUtil.addSqlToQueue(sql, sqlId); | |
| 104 | 104 | sql = null; |
| 105 | 105 | } |
| 106 | 106 | } |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/request/SqlRequest.java
View file @
069be1d
platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/SendMysqlSyncDatUtil.java
View file @
069be1d
| 1 | 1 | package com.lyms.platform.operate.web.utils; |
| 2 | 2 | |
| 3 | +import com.lyms.platform.common.utils.ExceptionUtils; | |
| 3 | 4 | import com.lyms.platform.common.utils.JsonUtil; |
| 4 | 5 | import com.lyms.platform.common.utils.PropertiesUtils; |
| 5 | 6 | import com.lyms.platform.common.utils.StringUtils; |
| 6 | 7 | import com.lyms.platform.operate.web.request.SqlRequest; |
| 7 | 8 | import org.apache.commons.codec.binary.Base64; |
| 8 | 9 | import org.apache.commons.collections.CollectionUtils; |
| 10 | +import org.springframework.beans.factory.InitializingBean; | |
| 9 | 11 | |
| 10 | 12 | import java.util.HashSet; |
| 11 | 13 | import java.util.Set; |
| 14 | +import java.util.concurrent.BlockingQueue; | |
| 15 | +import java.util.concurrent.LinkedBlockingQueue; | |
| 16 | +import java.util.concurrent.TimeUnit; | |
| 12 | 17 | |
| 13 | 18 | /** |
| 14 | 19 | * Created by Administrator on 2017-04-25. |
| 15 | 20 | */ |
| 16 | -public class SendMysqlSyncDatUtil { | |
| 21 | +public class SendMysqlSyncDatUtil implements InitializingBean { | |
| 17 | 22 | |
| 23 | + private static BlockingQueue<SqlRequest> syncSqlQueue = new LinkedBlockingQueue(); | |
| 24 | + | |
| 18 | 25 | private static Set<String> urls = new HashSet<>(); |
| 19 | 26 | static |
| 20 | 27 | { |
| 21 | 28 | |
| 22 | 29 | |
| ... | ... | @@ -29,22 +36,44 @@ |
| 29 | 36 | } |
| 30 | 37 | } |
| 31 | 38 | |
| 32 | - public static void sendSql(String sql ,String sqlId) | |
| 39 | + public static void addSqlToQueue(String sql ,String sqlId) | |
| 33 | 40 | { |
| 34 | 41 | SqlRequest request = new SqlRequest(); |
| 35 | 42 | request.setSql(Base64.encodeBase64String(sql.getBytes())); |
| 36 | 43 | request.setSqlId(sqlId); |
| 37 | - String json = JsonUtil.obj2Str(request); | |
| 38 | - if (CollectionUtils.isNotEmpty(urls)) | |
| 39 | - { | |
| 40 | - for(String url : urls) | |
| 41 | - { | |
| 42 | - System.out.println("sync mysql url = " + url); | |
| 43 | - String result = HttpClientUtil.doPostSSL(url,json,"3d19960bf3e81e7d816c4f26051c49ba"); | |
| 44 | - System.out.println("sync mysql result = " + result); | |
| 44 | + syncSqlQueue.add(request); | |
| 45 | + } | |
| 46 | + | |
| 47 | + @Override | |
| 48 | + public void afterPropertiesSet() throws Exception { | |
| 49 | + System.out.println("init ........."); | |
| 50 | + new HandleThread().start(); | |
| 51 | + } | |
| 52 | + | |
| 53 | + | |
| 54 | + private class HandleThread extends Thread { | |
| 55 | + @Override | |
| 56 | + public void run() { | |
| 57 | + SqlRequest request = null; | |
| 58 | + while (true) { | |
| 59 | + try { | |
| 60 | + System.out.println("get resuest = " + request+" size = "+syncSqlQueue.size()); | |
| 61 | + request = syncSqlQueue.poll(1, TimeUnit.MINUTES); | |
| 62 | + String json = JsonUtil.obj2Str(request); | |
| 63 | + if (CollectionUtils.isNotEmpty(urls)) | |
| 64 | + { | |
| 65 | + for(String url : urls) | |
| 66 | + { | |
| 67 | + System.out.println("sync mysql url = " + url); | |
| 68 | + String result = HttpClientUtil.doPostSSL(url,json,"3d19960bf3e81e7d816c4f26051c49ba"); | |
| 69 | + System.out.println("sync mysql result = " + result); | |
| 70 | + } | |
| 71 | + } | |
| 72 | + } catch (Exception e) { | |
| 73 | + ExceptionUtils.catchException(e, "HandleThread thread error." + request); | |
| 74 | + } | |
| 45 | 75 | } |
| 46 | 76 | } |
| 47 | - | |
| 48 | 77 | } |
| 49 | 78 | } |