From aa99f88e0966d8f4c9b5b4c1a28545246ecba70b Mon Sep 17 00:00:00 2001 From: richie Date: Wed, 20 Mar 2019 09:01:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin.xml | 2 +- .../redis/core/RedisDatabaseConnection.java | 15 +++++++++- .../fr/plugin/db/redis/core/RedisPool.java | 8 +++--- .../redis/core/RedisScriptTableDataModel.java | 4 ++- .../db/redis/core/RedisTableDataModel.java | 28 +++++++------------ .../db/redis/ui/RedisConnectionPane.java | 12 ++++++-- .../plugin/db/redis/locale/redis.properties | 1 + .../db/redis/locale/redis_en_US.properties | 1 + .../db/redis/locale/redis_zh_CN.properties | 3 +- 9 files changed, 46 insertions(+), 28 deletions(-) diff --git a/plugin.xml b/plugin.xml index d9ba434..0aa97e9 100755 --- a/plugin.xml +++ b/plugin.xml @@ -3,7 +3,7 @@ com.fr.solution.plugin.db.redis.v10 yes - 5.1 + 5.2 10.0 2018-11-29 richie diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java b/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java index 6c931f7..4d86e06 100755 --- a/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java +++ b/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java @@ -20,11 +20,13 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { private static final int DEFAULT_REDIS_PORT = 6379; private static final int MAX_TOTAL = 10; + private static final int MAX_WAIT = -1; private Conf host = Holders.simple(StringUtils.EMPTY); private Conf port = Holders.simple(DEFAULT_REDIS_PORT); private Conf password = Holders.simple(StringUtils.EMPTY); private Conf maxTotal = Holders.simple(MAX_TOTAL); + private Conf maxWait = Holders.simple(MAX_WAIT); public RedisDatabaseConnection() { @@ -62,6 +64,14 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { this.maxTotal.set(maxTotal); } + public int getMaxWait() { + return maxWait.get(); + } + + public void setMaxWait(int maxWait) { + this.maxWait.set(maxWait); + } + @Override public void testConnection() throws Exception { Jedis client = createRedisClient(); @@ -77,7 +87,7 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { } public Jedis createRedisClient() { - return RedisPool.getPool().getResource(getHost(), getPort(), getPassword(), getMaxTotal()); + return RedisPool.getPool().getResource(getHost(), getPort(), getPassword(), getMaxTotal(), getMaxWait()); } @Override @@ -152,6 +162,7 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { setPassword(SecurityToolbox.encrypt(pwd)); } setMaxTotal(reader.getAttrAsInt("maxTotal", MAX_TOTAL)); + setMaxWait(reader.getAttrAsInt("maxWait", MAX_WAIT)); } } } @@ -166,6 +177,7 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { writer.attr("password", SecurityToolbox.decrypt(getPassword())); } writer.attr("maxTotal", getMaxTotal()); + writer.attr("maxWait", getMaxWait()); writer.end(); } @@ -176,6 +188,7 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { cloned.port = (Conf) port.clone(); cloned.password = (Conf) password.clone(); cloned.maxTotal = (Conf) maxTotal.clone(); + cloned.maxWait = (Conf) maxWait.clone(); return cloned; } } \ No newline at end of file diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java b/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java index a12daa9..0cdd3d0 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java +++ b/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java @@ -25,16 +25,16 @@ public class RedisPool { private Map jedisPoolMap = new ConcurrentHashMap(); - public Jedis getResource(String host, int port, String password, int maxTotal) { + public Jedis getResource(String host, int port, String password, int maxTotal, int maxWait) { JedisPool jedisPool = jedisPoolMap.get(host); if (jedisPool == null) { - jedisPool = createJedisPool(host, port, password, maxTotal); + jedisPool = createJedisPool(host, port, password, maxTotal, maxWait); jedisPoolMap.put(host, jedisPool); } return jedisPool.getResource(); } - private JedisPool createJedisPool(String host, int port, String password, int maxTotal) { + private JedisPool createJedisPool(String host, int port, String password, int maxTotal, int maxWait) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(maxTotal); //连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true @@ -51,7 +51,7 @@ public class RedisPool { config.setMaxIdle(10); //最大连接数,可配置 //获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1 - config.setMaxWaitMillis(-1); + config.setMaxWaitMillis(maxWait); //逐出连接的最小空闲时间 默认1800000毫秒(30分钟) config.setMinEvictableIdleTimeMillis(1800000); //最小空闲连接数, 默认0 diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java b/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java index f8f3c09..c673af4 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java +++ b/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java @@ -3,6 +3,7 @@ package com.fr.plugin.db.redis.core; import com.eclipsesource.v8.V8; import com.eclipsesource.v8.V8Array; import com.eclipsesource.v8.V8Object; +import com.fr.base.TableData; import com.fr.data.AbstractDataModel; import com.fr.plugin.db.redis.help.Console; import com.fr.plugin.db.redis.help.Files; @@ -61,7 +62,8 @@ public class RedisScriptTableDataModel extends AbstractDataModel { columnNames[i] = String.valueOf(v8Column.get(i)); } int returnRowCount = v8Table.length(); - for (int i = 0, realCount = Math.min(returnRowCount, this.rowCount); i < realCount; i++) { + int realCount = rowCount == TableData.RESULT_ALL ? returnRowCount : Math.min(returnRowCount, this.rowCount); + for (int i = 0; i < realCount; i++) { V8Array v8Row = v8Table.getArray(i); List row = new ArrayList(); for (int j = 0, col = v8Row.length(); j < col; j++) { diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java b/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java index 91e1acd..f5a5870 100755 --- a/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java +++ b/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java @@ -2,7 +2,6 @@ package com.fr.plugin.db.redis.core; import com.fr.base.Parameter; import com.fr.data.AbstractDataModel; -import com.fr.general.data.TableDataException; import com.fr.log.FineLoggerFactory; import com.fr.plugin.PluginLicense; import com.fr.plugin.PluginLicenseManager; @@ -14,12 +13,9 @@ import com.fr.third.redis.clients.jedis.Jedis; import java.util.List; public class RedisTableDataModel extends AbstractDataModel { - - private Jedis redisClient; private String[] columnNames; private List> data; - public RedisTableDataModel(Calculator calculator, Parameter[] ps, RedisDatabaseConnection mc, int dbIndex, String query, String script, int rowCount) { PluginLicense pluginLicense = PluginLicenseManager.getInstance().getPluginLicenseByID(RedisConstants.PLUGIN_ID); if (pluginLicense.isAvailable()) { @@ -33,11 +29,9 @@ public class RedisTableDataModel extends AbstractDataModel { if (StringUtils.isEmpty(query)) { return; } - if (redisClient == null) { - redisClient = mc.createRedisClient(); - redisClient.select(dbIndex); - FineLoggerFactory.getLogger().info("Connect to redis and select database:" + dbIndex); - } + Jedis redisClient = mc.createRedisClient(); + redisClient.select(dbIndex); + FineLoggerFactory.getLogger().info("Connect to redis and select database:" + dbIndex); try { long start = System.currentTimeMillis(); DataWrapper wrapper = VisitorFactory.getKeyValueResult(calculator, ps, redisClient, query, rowCount); @@ -45,33 +39,34 @@ public class RedisTableDataModel extends AbstractDataModel { wrapper.transform(script); data = wrapper.getData(); columnNames = wrapper.getColumnNames(); + redisClient.close(); } catch (Exception e) { throw new RuntimeException(e.getCause()); } } @Override - public int getColumnCount() throws TableDataException { + public int getColumnCount() { return columnNames.length; } @Override - public String getColumnName(int columnIndex) throws TableDataException { + public String getColumnName(int columnIndex) { return columnNames[columnIndex]; } @Override - public boolean hasRow(int rowIndex) throws TableDataException { + public boolean hasRow(int rowIndex) { return data != null && data.get(0).size() > rowIndex; } @Override - public int getRowCount() throws TableDataException { + public int getRowCount() { return data == null ? 0 : data.get(0).size(); } @Override - public Object getValueAt(int rowIndex, int columnIndex) throws TableDataException { + public Object getValueAt(int rowIndex, int columnIndex) { if (data != null && data.size() > columnIndex) { List columnData = data.get(columnIndex); if (columnData != null && columnData.size() > rowIndex) { @@ -83,9 +78,6 @@ public class RedisTableDataModel extends AbstractDataModel { @Override public void release() throws Exception { - if (redisClient != null) { - redisClient.quit(); - redisClient = null; - } + } } \ No newline at end of file diff --git a/src/main/java/com/fr/plugin/db/redis/ui/RedisConnectionPane.java b/src/main/java/com/fr/plugin/db/redis/ui/RedisConnectionPane.java index 7609f4b..2907f47 100755 --- a/src/main/java/com/fr/plugin/db/redis/ui/RedisConnectionPane.java +++ b/src/main/java/com/fr/plugin/db/redis/ui/RedisConnectionPane.java @@ -5,6 +5,7 @@ import com.fr.design.data.datapane.connect.DatabaseConnectionPane; import com.fr.design.gui.ibutton.UIPasswordField; import com.fr.design.gui.ilable.UILabel; import com.fr.design.gui.itextfield.UIIntNumberField; +import com.fr.design.gui.itextfield.UINumberField; import com.fr.design.gui.itextfield.UITextField; import com.fr.design.i18n.Toolkit; import com.fr.design.layout.FRGUIPaneFactory; @@ -23,6 +24,7 @@ public class RedisConnectionPane extends DatabaseConnectionPane