Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
arden committed Aug 25, 2010
1 parent 355dd86 commit 369a963
Show file tree
Hide file tree
Showing 9 changed files with 1,005 additions and 901 deletions.
Binary file added lib/commons-pool-1.5.4.jar
Binary file not shown.
1,774 changes: 891 additions & 883 deletions nbproject/build-impl.xml

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions nbproject/genfiles.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
build.xml.data.CRC32=074da0ae
build.xml.script.CRC32=b102c1a2
build.xml.stylesheet.CRC32=28e38971@1.38.1.45
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=074da0ae
nbproject/build-impl.xml.script.CRC32=93195a2d
nbproject/build-impl.xml.stylesheet.CRC32=78c6a6ee@1.38.1.45
build.xml.data.CRC32=074da0ae
build.xml.script.CRC32=b102c1a2
build.xml.stylesheet.CRC32=28e38971@1.38.1.45
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=074da0ae
nbproject/build-impl.xml.script.CRC32=7397afa4
nbproject/build-impl.xml.stylesheet.CRC32=f33e10ff@1.38.2.45
Empty file.
4 changes: 4 additions & 0 deletions nbproject/private/private.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
compile.on.save=true
do.depend=false
do.jar=true
javac.debug=true
javadoc.preview=true
user.properties.file=C:\\Users\\arden\\.netbeans\\6.9\\build.properties
4 changes: 4 additions & 0 deletions nbproject/private/private.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
</project-private>
9 changes: 6 additions & 3 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=jredis-auto-connect
application.vendor=arden
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
Expand All @@ -24,14 +24,17 @@ debug.test.classpath=\
dist.dir=dist
dist.jar=${dist.dir}/jredis-auto-connect.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
file.reference.commons-pool-1.5.4.jar=lib/commons-pool-1.5.4.jar
file.reference.jrails-1.0.jar=lib\\jrails-1.0.jar
file.reference.jredis-core-all-1.0-rc1-jar-with-dependencies.jar=lib\\jredis-core-all-1.0-rc1-jar-with-dependencies.jar
includes=**
jar.compress=false
javac.classpath=\
${file.reference.jrails-1.0.jar}:\
${file.reference.jredis-core-all-1.0-rc1-jar-with-dependencies.jar}
${file.reference.jredis-core-all-1.0-rc1-jar-with-dependencies.jar}:\
${file.reference.commons-pool-1.5.4.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
Expand Down
30 changes: 23 additions & 7 deletions src/com/tujiao/jredis/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,46 @@
*/
package com.tujiao.jredis;

import com.jrails.commons.utils.PropertiesUtils;
import com.jrails.modules.spring.ServiceLocator;
import com.tujiao.jredis.pool.RedisPoolableObjectFactory;
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec;
import org.jredis.connector.ConnectionSpec;
import org.jredis.JRedis;
import org.jredis.ri.alphazero.JRedisClient;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.SoftReferenceObjectPool;

/**
*
* @author arden
*/
public class RedisClient implements Runnable {
private static String host = "121.11.69.53";
private static String host = "localhost";
private static int port = 6379;
private static JRedis jredis = null;
private static final int retryConnectTimes = 3;
private static PoolableObjectFactory factory = new RedisPoolableObjectFactory();
public static SoftReferenceObjectPool pool = new SoftReferenceObjectPool(factory);

static {
// try {
// // 启动自动重连功能
// RedisReconnect.start();
// } catch (Exception e) {
// e.printStackTrace();
// }
}

public static JRedis getRedis() {
Object o;
try {
// 启动自动重连功能
RedisReconnect.start();
} catch (Exception e) {
e.printStackTrace();
o = pool.borrowObject();
if (o instanceof JRedis) {
return (JRedis)o;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}

public static JRedis getRedisClient() {
Expand Down
69 changes: 69 additions & 0 deletions src/com/tujiao/jredis/pool/RedisPoolableObjectFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.tujiao.jredis.pool;

import com.paojiao.redis.RedisClient;
import org.apache.commons.pool.PoolableObjectFactory;
import org.jredis.JRedis;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

/**
*
* @author arden
*/
public class RedisPoolableObjectFactory implements PoolableObjectFactory {
private static Logger logger = LoggerFactory.getLogger(RedisPoolableObjectFactory.class);

@Override
public void activateObject(Object o) throws Exception {
logger.debug("Activating Object " + o);
}

@Override
public void destroyObject(Object o) throws Exception {
logger.debug("DestroyObject Object " + o);
}

@Override
public Object makeObject() throws Exception {
JRedis jredis = RedisClient.connect();
logger.debug("正在创建JRedis对象:" + jredis);
return jredis;
}

@Override
public void passivateObject(Object o) throws Exception {
logger.debug("Passivating Object " + o);
}

@Override
public boolean validateObject(Object o) {
JRedis jredis = null;
boolean result = true;
try {
if (o instanceof JRedis) {
jredis = (JRedis)o;
jredis.ping();
logger.debug("----:jredis:" + jredis);
//RedisClient.pool.returnObject(jredis);
}
} catch (Exception e) {
result = false;
try {
if (jredis != null) {
jredis.quit();
}
} catch (Exception ex) {
//ex.printStackTrace();
}
//e.printStackTrace();
}
logger.debug("Validating Object " + o + " : " + result);
return result;
}

}

0 comments on commit 369a963

Please sign in to comment.