本文整理匯總了Java中redis.clients.jedis.JedisPool類的典型用法代碼示例。如果您正苦於以下問題:Java JedisPool類的具體用法?Java JedisPool怎麽用?Java JedisPool使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JedisPool類屬於redis.clients.jedis包,在下文中一共展示了JedisPool類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: isAvailable
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
public boolean isAvailable() {
for (JedisPool jedisPool : jedisPools.values()) {
Jedis jedis = jedisPool.getResource();
boolean isBroken = false;
try {
if (jedis.isConnected()) {
return true; // 至少需單台機器可用
}
} catch (JedisConnectionException e) {
isBroken = true;
} finally {
if (isBroken) {
jedisPool.returnBrokenResource(jedis);
} else {
jedisPool.returnResource(jedis);
}
}
}
return false;
}
示例2: checkJedisIsReusedWhenReturned
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
@Test
public void checkJedisIsReusedWhenReturned() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "0");
pool.returnResource(jedis);
jedis = pool.getResource();
jedis.auth("foobared");
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
assertTrue(pool.isClosed());
}
示例3: toJedisPool0
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
private JedisPool toJedisPool0() {
return new JedisPool(new GenericObjectPoolConfig(), this.getHost(), this.getPort(), this.getTimeout(), this.getPassword(), this.getDatabase()) {
@Override
public Jedis getResource() {
try {
return super.getResource();
} catch (JedisConnectionException var2) {
RedisConfig.LOGGER.error(RedisConfig.this.toString(), var2);
throw new JedisConnectionException(RedisConfig.this.toString(), var2);
}
}
@Override
public void close() {
}
};
}
示例4: Client
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
/**
* Create a new client to a RediSearch index
* @param indexName the name of the index we are connecting to or creating
* @param host the redis host
* @param port the redis pot
*/
public Client(String indexName, String host, int port, int timeout, int poolSize) {
JedisPoolConfig conf = new JedisPoolConfig();
conf.setMaxTotal(poolSize);
conf.setTestOnBorrow(false);
conf.setTestOnReturn(false);
conf.setTestOnCreate(false);
conf.setTestWhileIdle(false);
conf.setMinEvictableIdleTimeMillis(60000);
conf.setTimeBetweenEvictionRunsMillis(30000);
conf.setNumTestsPerEvictionRun(-1);
conf.setFairness(true);
pool = new JedisPool(conf, host, port, timeout);
this.indexName = indexName;
this.commands = new Commands.SingleNodeCommands();
}
示例5: available
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
@Override
public boolean available() {
for (JedisPool jedisPool : jedisPools.values()) {
Jedis jedis = jedisPool.getResource();
try {
if (jedis.isConnected()) {
return true; // 至少需單台機器可用
}
} catch (JedisConnectionException e) {
logger.error("Jedis Connection Exception", e);
} finally {
if(jedis != null){
jedis.close();
}
}
}
return false;
}
示例6: searchForUsersByExpression
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
@Override
public Optional<List<String>> searchForUsersByExpression(String expression) {
System.out.println("expression: " + expression);
JedisPool jedisPool = JedisFactory.getPool();
try (Jedis jedis = jedisPool.getResource()) {
if (jedis.exists("0")) {
jedis.del("0");
}
long lenght = jedis.zcard("all_users");
Set<String> sresult = jedis.zrange("all_users", 0, lenght);
for (Iterator<String> iterator = sresult.iterator(); iterator.hasNext();) {
String iter = iterator.next();
System.out.println("Iterator: " + iter);
if (iter.length() >= expression.length() && iter.substring(0, expression.length()).equals(expression)) {
jedis.lpush("0", iter);
System.out.println("Suche hinzugefuegt: " + iter);
}
}
return Optional.of(jedis.lrange("0", 0, 1));
} catch (Exception e) {
System.out.println("Mock SearchUserByExpression: " + expression);
e.printStackTrace();
return Optional.of(new ArrayList<String>());
}
// return Optional.ofNullable("searchForUsersByExpression:
// Redis-Answer-Expression");
}
示例7: publish
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
public void publish(byte[] channel, byte[] message) {
Collection<JedisPool> poolCollection = jedisCluster.getClusterNodes().values();
Iterator<JedisPool> iterator = poolCollection.iterator();
if (iterator.hasNext()) {
JedisPool jedisPool = (JedisPool)iterator.next();
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.publish(channel, message);
} catch (Exception var11) {
throw new RuntimeException(var11);
} finally {
jedisPool.returnResourceObject(jedis);
}
}
}
示例8: connect
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
public void connect()
{
this.instance.log(Level.INFO, "Connecting to database...");
JedisPoolConfig jedisConfiguration = new JedisPoolConfig();
jedisConfiguration.setMaxTotal(-1);
jedisConfiguration.setJmxEnabled(false);
Logger logger = Logger.getLogger(JedisPool.class.getName());
logger.setLevel(Level.OFF);
this.jedisPool = new JedisPool(jedisConfiguration, this.instance.getConfiguration().redisIp, this.instance.getConfiguration().redisPort, 0, this.instance.getConfiguration().redisPassword);
try
{
this.jedisPool.getResource().close();
} catch (Exception e)
{
this.instance.log(Level.SEVERE, "Can't connect to the database!");
System.exit(8);
}
this.instance.log(Level.INFO, "Connected to database.");
}
示例9: call
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
@SuppressWarnings("deprecation")
@Override
public <T> T call(JedisPool jedisPool) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return exec(jedis);
} catch (Exception e) {
logger.error("jedisPool getResouce or exec has exception");
throw e;
} finally {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
示例10: main
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
public static void main(String[] args) {
//定義redis的連接池
JedisPool jedisPool = new JedisPool(new GenericObjectPoolConfig(),
"127.0.0.1", 6379, 30000);
// JedisPool jedisPool = new JedisPool(new GenericObjectPoolConfig(),
// "127.0.0.1", 6379, 30000, "123456"); //有密碼
//create, config and start
Spider spider = Spider.create() //創建爬蟲實例
.setThreadCount(10) //設置任務線程池數量
.addStartRequests("http://blog.csdn.net/") //添加起始url
.setPageProcessor(new HelloWorldPageProcessor()) //設置頁麵解析器
.setScheduler(new RedisScheduler(jedisPool)) //設置請求任務調度器
.setPipeline(new HelloWorldPipeline()); //結果集處理器
//監控
SpiderMonitor.register(spider);
//啟動
spider.start();
}
示例11: setUp
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
@Before
public void setUp() throws IOException {
initMocks(this);
final Random random = new SecureRandom();
redisServer = new RedisServer();
redisServer.start();
pool = new JedisPool();
repository = new RedisKeyRepository(pool);
manager = new RedisKeyManager(random, pool, repository);
manager.setMaxActiveKeys(3);
clearData();
manager.initialiseNewRepository();
resource = new ProtectedResource(repository, random);
}
示例12: transformResponseToUserList
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
private List<User> transformResponseToUserList(List<String> userList) {
JedisPool jedisPool = JedisFactory.getPool();
try (Jedis jedis = jedisPool.getResource()) {
System.out.println("Die Uebergebene userList" + userList.toString());
List<User> users = new ArrayList<>();
for (int i = 0; i < userList.size(); i++) {
String keyUser = userList.get(i);
System.out.println("keyUser: " + keyUser);
// String pw = jedis.hget(keyUser, "pw");
User user = new User(jedis.hget(keyUser, "name"), null);
users.add(user);
}
System.out.println("users:" + users);
return users;
} catch (Exception e) {
System.out.println("transformResponseToUserList");
e.printStackTrace();
return null;
}
}
示例13: intercept
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
/**
* 攔截所有調用,選擇正確的實例執行命令
* @param o 調用實例
* @param method 調用方法
* @param args 方法參數
* @param methodProxy 方法代理
* @return 命令返回值
* @throws Throwable 方法執行異常或連接異常
*/
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
GedisInstanceType type;
if (READ_METHOD_LIST.contains(method.getName())) {
type = GedisInstanceType.READ;
} else {
type = GedisInstanceType.WRITE;
}
JedisPool pool = getJedisPoolByType(type);
Jedis jedis = pool.getResource();
try {
return method.invoke(jedis, args);
} catch (Exception e) {
jedis.close();
throw e;
} finally {
jedis.close();
}
}
示例14: selectDatabaseOnActivation
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
@Test
public void selectDatabaseOnActivation() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000,
"foobared");
Jedis jedis0 = pool.getResource();
assertEquals(0L, jedis0.getDB().longValue());
jedis0.select(1);
assertEquals(1L, jedis0.getDB().longValue());
pool.returnResource(jedis0);
Jedis jedis1 = pool.getResource();
assertTrue("Jedis instance was not reused", jedis1 == jedis0);
assertEquals(0L, jedis1.getDB().longValue());
pool.returnResource(jedis1);
pool.destroy();
assertTrue(pool.isClosed());
}
示例15: connect
import redis.clients.jedis.JedisPool; //導入依賴的package包/類
@Override
public void connect(NURL nurl) {
super.connect(nurl);
this.retryPeriod = nurl.getParameter("retryPeriod", retryPeriod);
JedisPoolConfig config = new JedisPoolConfig();
Map<String, String> parameters = nurl.getParameters();
if (parameters != null) {
if (!parameters.isEmpty()) {
try {
Beans.copyProperties(config, nurl.getParameters());
} catch (Exception e) {
logger.error("The copy properties exception.", e);
}
}
}
jedisPool = new JedisPool(config, nurl.getHost(), nurl.getPort());
}