Skip to content

Commit

Permalink
#393 新的Connection定义及Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin committed Sep 13, 2014
1 parent 85f705e commit a98f360
Show file tree
Hide file tree
Showing 19 changed files with 467 additions and 348 deletions.
4 changes: 4 additions & 0 deletions examples/showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<groupId>org.springside</groupId>
<artifactId>springside-metrics</artifactId>
</dependency>
<dependency>
<groupId>org.springside</groupId>
<artifactId>springside-redis</artifactId>
</dependency>
<!-- SPRINGSIDE end -->

<!-- SPRING basic begin -->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
package org.springside.examples.showcase.demos.redis;

import org.springside.modules.nosql.redis.JedisTemplate;
import org.springside.modules.nosql.redis.JedisUtils;
import org.springside.modules.nosql.redis.pool.JedisPool;
import org.springside.modules.nosql.redis.pool.JedisPoolBuilder;
import org.springside.modules.test.benchmark.BenchmarkTask;
import org.springside.modules.test.benchmark.ConcurrentBenchmark;

import redis.clients.jedis.JedisPool;

/**
* 测试Redis用于计数器时incr()方法的性能.
*
Expand Down Expand Up @@ -39,8 +38,8 @@ public RedisCounterBenchmark() {

@Override
protected void setUp() {
pool = JedisPoolFactory.createJedisPool(JedisUtils.DEFAULT_HOST, JedisUtils.DEFAULT_PORT,
JedisUtils.DEFAULT_TIMEOUT, threadCount);

pool = new JedisPoolBuilder().setDirectHostAndPort("localhost", "6379").setPoolSize(threadCount).buildPool();
jedisTemplate = new JedisTemplate(pool);

// 重置Counter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import org.springside.modules.mapper.JsonMapper;
import org.springside.modules.nosql.redis.JedisTemplate;
import org.springside.modules.nosql.redis.JedisTemplate.JedisActionNoResult;
import org.springside.modules.nosql.redis.JedisUtils;
import org.springside.modules.nosql.redis.pool.JedisPool;
import org.springside.modules.nosql.redis.pool.JedisPoolBuilder;
import org.springside.modules.test.benchmark.BenchmarkTask;
import org.springside.modules.test.benchmark.ConcurrentBenchmark;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
* 测试Redis用于Session管理的setEx()与get()方法性能, 使用JSON格式存储数据.
Expand Down Expand Up @@ -44,8 +44,7 @@ public RedisSessionBenchmark() {

@Override
protected void setUp() {
pool = JedisPoolFactory.createJedisPool(JedisUtils.DEFAULT_HOST, JedisUtils.DEFAULT_PORT,
JedisUtils.DEFAULT_TIMEOUT, threadCount);
pool = new JedisPoolBuilder().setDirectHostAndPort("localhost", "6379").setPoolSize(threadCount).buildPool();
jedisTemplate = new JedisTemplate(pool);

// 清空数据库
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
*******************************************************************************/
package org.springside.examples.showcase.demos.redis.elector;

import org.springside.examples.showcase.demos.redis.JedisPoolFactory;
import org.springside.modules.nosql.redis.JedisUtils;
import org.springside.modules.nosql.redis.elector.MasterElector;

import redis.clients.jedis.JedisPool;
import org.springside.modules.nosql.redis.pool.JedisPool;
import org.springside.modules.nosql.redis.pool.JedisPoolBuilder;

public class MasterElectorDemo {

public static void main(String[] args) throws Exception {

JedisPool pool = JedisPoolFactory.createJedisPool(JedisUtils.DEFAULT_HOST, JedisUtils.DEFAULT_PORT,
JedisUtils.DEFAULT_TIMEOUT, 1);
JedisPool pool = new JedisPoolBuilder().setDirectHostAndPort("localhost", "6379").setPoolSize(1).buildPool();
try {
MasterElector masterElector = new MasterElector(pool, 5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.springside.examples.showcase.demos.redis.JedisPoolFactory;
import org.springside.modules.nosql.redis.JedisUtils;
import org.springside.modules.nosql.redis.pool.JedisPoolBuilder;
import org.springside.modules.nosql.redis.scheduler.AdvancedJobConsumer;
import org.springside.modules.nosql.redis.scheduler.SimpleJobConsumer;
import org.springside.modules.test.benchmark.ConcurrentBenchmark;
Expand Down Expand Up @@ -41,8 +40,7 @@ public static void main(String[] args) throws Exception {
batchSize = Integer.parseInt(System.getProperty("batchsize",
String.valueOf(AdvancedJobConsumer.DEFAULT_BATCH_SIZE)));

pool = JedisPoolFactory.createJedisPool(JedisUtils.DEFAULT_HOST, JedisUtils.DEFAULT_PORT,
JedisUtils.DEFAULT_TIMEOUT, threadCount);
pool = new JedisPoolBuilder().setDirectHostAndPort("localhost", "6379").setPoolSize(threadCount).buildPool();

ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
for (int i = 0; i < threadCount; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.springside.examples.showcase.demos.redis.JedisPoolFactory;
import org.springside.modules.nosql.redis.JedisUtils;
import org.springside.modules.nosql.redis.pool.JedisPoolBuilder;
import org.springside.modules.nosql.redis.scheduler.AdvancedJobConsumer;
import org.springside.modules.nosql.redis.scheduler.SimpleJobConsumer;
import org.springside.modules.test.benchmark.ConcurrentBenchmark;
Expand All @@ -32,8 +31,7 @@ public static void main(String[] args) throws Exception {
threadCount = Integer.parseInt(System.getProperty(ConcurrentBenchmark.THREAD_COUNT_NAME,
String.valueOf(THREAD_COUNT)));

pool = JedisPoolFactory.createJedisPool(JedisUtils.DEFAULT_HOST, JedisUtils.DEFAULT_PORT,
JedisUtils.DEFAULT_TIMEOUT, threadCount);
pool = new JedisPoolBuilder().setDirectHostAndPort("localhost", "6379").setPoolSize(threadCount).buildPool();

ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
for (int i = 0; i < threadCount; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.springside.examples.showcase.demos.redis.JedisPoolFactory;
import org.springside.modules.nosql.redis.JedisUtils;
import org.springside.modules.nosql.redis.pool.JedisPool;
import org.springside.modules.nosql.redis.pool.JedisPoolBuilder;
import org.springside.modules.nosql.redis.scheduler.SimpleJobConsumer;
import org.springside.modules.test.benchmark.ConcurrentBenchmark;

import redis.clients.jedis.JedisPool;

import com.google.common.util.concurrent.RateLimiter;

/**
Expand Down Expand Up @@ -49,8 +47,7 @@ public static void main(String[] args) throws Exception {
threadCount = Integer.parseInt(System.getProperty(ConcurrentBenchmark.THREAD_COUNT_NAME,
String.valueOf(THREAD_COUNT)));

pool = JedisPoolFactory.createJedisPool(JedisUtils.DEFAULT_HOST, JedisUtils.DEFAULT_PORT,
JedisUtils.DEFAULT_TIMEOUT, threadCount);
pool = new JedisPoolBuilder().setDirectHostAndPort("localhost", "6379").setPoolSize(threadCount).buildPool();

ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
for (int i = 0; i < threadCount; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.springside.examples.showcase.demos.redis.JedisPoolFactory;
import org.springside.modules.nosql.redis.JedisUtils;
import org.springside.modules.nosql.redis.pool.JedisPool;
import org.springside.modules.nosql.redis.pool.JedisPoolBuilder;
import org.springside.modules.nosql.redis.scheduler.JobDispatcher;
import org.springside.modules.nosql.redis.scheduler.JobStatistics;

import redis.clients.jedis.JedisPool;

/**
* 运行JobDispatcher,每秒将Job从"job:ss:scheduled" sorted set 发布到"job:ss:ready" list.
* 如果有任务已被领取而长期没有被执行,会从"job:ss:locked" sorted set取回并重新发布到"job:ss:ready" list.
Expand All @@ -31,8 +29,7 @@ public class ReliableJobDispatcherDemo {

public static void main(String[] args) throws Exception {

JedisPool pool = JedisPoolFactory.createJedisPool(JedisUtils.DEFAULT_HOST, JedisUtils.DEFAULT_PORT,
JedisUtils.DEFAULT_TIMEOUT, 1);
JedisPool pool = new JedisPoolBuilder().setDirectHostAndPort("localhost", "6379").setPoolSize(1).buildPool();
try {
JobDispatcher dispatcher = new JobDispatcher("ss", pool);
dispatcher.setReliable(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.springside.examples.showcase.demos.redis.JedisPoolFactory;
import org.springside.modules.nosql.redis.JedisUtils;
import org.springside.modules.nosql.redis.pool.JedisPool;
import org.springside.modules.nosql.redis.pool.JedisPoolBuilder;
import org.springside.modules.nosql.redis.scheduler.JobDispatcher;
import org.springside.modules.nosql.redis.scheduler.JobStatistics;

import redis.clients.jedis.JedisPool;

/**
* 运行JobDispatcher,每秒将Job从"job:ss:scheduled" sorted set 发布到"job:ss:ready" list.
*
Expand All @@ -31,8 +29,7 @@ public class SimpleJobDispatcherDemo {

public static void main(String[] args) throws Exception {

JedisPool pool = JedisPoolFactory.createJedisPool(JedisUtils.DEFAULT_HOST, JedisUtils.DEFAULT_PORT,
JedisUtils.DEFAULT_TIMEOUT, 1);
JedisPool pool = new JedisPoolBuilder().setDirectHostAndPort("localhost", "6379").setPoolSize(1).buildPool();
try {
JobDispatcher dispatcher = new JobDispatcher("ss", pool);
JobStatistics statistics = new JobStatistics("ss", pool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.springside.examples.showcase.demos.redis.JedisPoolFactory;
import org.springside.examples.showcase.demos.redis.job.dispatcher.SimpleJobDispatcherDemo;
import org.springside.modules.nosql.redis.JedisUtils;
import org.springside.modules.nosql.redis.pool.JedisPool;
import org.springside.modules.nosql.redis.pool.JedisPoolBuilder;
import org.springside.modules.nosql.redis.scheduler.JobProducer;
import org.springside.modules.test.benchmark.BenchmarkTask;
import org.springside.modules.test.benchmark.ConcurrentBenchmark;

import redis.clients.jedis.JedisPool;

/**
* 运行JobProducer产生新的Job。
*
Expand Down Expand Up @@ -49,8 +47,7 @@ public JobProducerDemo() {

@Override
protected void setUp() {
pool = JedisPoolFactory.createJedisPool(JedisUtils.DEFAULT_HOST, JedisUtils.DEFAULT_PORT,
JedisUtils.DEFAULT_TIMEOUT, threadCount);
pool = new JedisPoolBuilder().setDirectHostAndPort("localhost", "6379").setPoolSize(threadCount).buildPool();
jobProducer = new JobProducer("ss", pool);
}

Expand Down
5 changes: 5 additions & 0 deletions modules/parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
<artifactId>springside-metrics</artifactId>
<version>${springside.version}</version>
</dependency>
<dependency>
<groupId>org.springside</groupId>
<artifactId>springside-redis</artifactId>
<version>${springside.version}</version>
</dependency>
<!-- SPRINGSIDE MODULES end -->

<!-- spring-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected void closeResource(Jedis jedis, boolean connectionBroken) {
}
} catch (Exception e) {
logger.error("Error happen when return jedis to pool, try to close it directly.", e);
JedisUtils.closeJedis(jedis);
JedisUtils.destroyJedis(jedis);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,28 @@
*******************************************************************************/
package org.springside.modules.nosql.redis;

import org.springside.modules.nosql.redis.JedisTemplate.JedisAction;
import org.springside.modules.nosql.redis.pool.JedisPool;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.exceptions.JedisException;

public class JedisUtils {
public static final String DEFAULT_HOST = "localhost";
public static final int DEFAULT_PORT = Protocol.DEFAULT_PORT;
public static final int DEFAULT_TIMEOUT = Protocol.DEFAULT_TIMEOUT;

private static final String OK_CODE = "OK";
private static final String OK_MULTI_CODE = "+OK";

/**
* 快速设置JedisPoolConfig, 不执行idle checking。
*/
public static JedisPoolConfig createPoolConfig(int maxIdle, int maxTotal) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMaxTotal(maxTotal);
poolConfig.setTimeBetweenEvictionRunsMillis(-1);
return poolConfig;
}

/**
* 快速设置JedisPoolConfig, 设置执行idle checking的间隔和可被清除的idle时间.
* 默认的checkingIntervalSecs是30秒,可被清除时间是60秒。
*/
public static JedisPoolConfig createPoolConfig(int maxIdle, int maxTotal, int checkingIntervalSecs,
int evictableIdleTimeSecs) {
JedisPoolConfig poolConfig = createPoolConfig(maxIdle, maxTotal);

poolConfig.setTimeBetweenEvictionRunsMillis(checkingIntervalSecs * 1000);
poolConfig.setMinEvictableIdleTimeMillis(evictableIdleTimeSecs * 1000);
return poolConfig;
}

/**
* 判断 是 OK 或 +OK.
* 判断 返回值是否ok.
*/
public static boolean isStatusOk(String status) {
return (status != null) && (OK_CODE.equals(status) || OK_MULTI_CODE.equals(status));
}

/**
* 退出然后关闭Jedis连接。如果Jedis为null则无动作。
* 在Pool以外强行销毁Jedis.
*/
public static void closeJedis(Jedis jedis) {
public static void destroyJedis(Jedis jedis) {
if ((jedis != null) && jedis.isConnected()) {
try {
try {
Expand All @@ -63,4 +38,22 @@ public static void closeJedis(Jedis jedis) {
}
}
}

/**
* Ping the jedis instance, return true is the result is PONG.
*/
public static boolean ping(JedisPool pool) {
JedisTemplate template = new JedisTemplate(pool);
try {
String result = template.execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.ping();
}
});
return (result != null) && result.equals("PONG");
} catch (JedisException e) {
return false;
}
}
}
Loading

0 comments on commit a98f360

Please sign in to comment.