0 0

急求大婶指导redis Could not get a resource from the pool 异常10

功能:多台服务器同时访问redis中的队列
运行代码有时报错,有时正常,快要疯了,求大婶指导解决方案
报错信息:
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:22)
at com.bonc.text.service.crawler.RedisUtil.<init>(RedisUtil.java:24)
at com.bonc.text.service.crawler.DistributedCrawler.initCrawler(DistributedCrawler.java:53)
at com.bonc.text.service.crawler.DistributedCrawler.main(DistributedCrawler.java:80)
Caused by: java.util.NoSuchElementException: Could not create a validated object, cause: ValidateObject failed
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1233)
at redis.clients.util.Pool.getResource(Pool.java:20)
... 3 more


源代码:

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * 访问Redis工具类
 */
public class RedisUtil {
	
	public RedisUtil(){
		initialShardedPool();
		shardedJedis = shardedJedisPool.getResource();
	}
	// redis所在IP地址
	private static String IP;
	// 端口号
	private static int PORT_NUMBER;
	// 队列key值
	private static String KEY;
	//切片额客户端连接
	private static ShardedJedis shardedJedis;
	//切片连接池
    private static ShardedJedisPool shardedJedisPool;
    
	static{
		Properties prop = new Properties();
	    InputStream in;
		try {
			in = new BufferedInputStream(RedisUtil.class.getClass().getResourceAsStream("/craw.properties"));
			prop.load(in);
		    in.close();
		    
		    IP = prop.getProperty("IP");
		    PORT_NUMBER = Integer.parseInt(prop.getProperty("PORT_NUMBER"));
		    KEY = prop.getProperty("KEY");               
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
     * 初始化切片池 
     */ 
    private void initialShardedPool() {
        // 池基本配置 
        JedisPoolConfig config = new JedisPoolConfig();
        //控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
        //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
        config.setMaxActive(-1);
        //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
        config.setMaxIdle(50000);
        //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
        config.setMaxWait(10000 * 10000);
        //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
        config.setTestOnBorrow(true);
        // slave链接
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(1000000);
        shards.add(new JedisShardInfo(IP, PORT_NUMBER));
        // 构造池
        shardedJedisPool = new ShardedJedisPool(config, shards);
    }
	
    /**
     * 返还到连接池
     * 
     * @param pool 
     * @param redis
     */
    public static void returnResource() {
        if (shardedJedis != null) {
        	shardedJedisPool.returnResource(shardedJedis);
        }
    }
    
	/**
	 * 从redis 队列尾部取一条数据,并删除
	 */ 
	public String getUrlByRedis() {
        String str = shardedJedis.lpop(KEY);
        returnResource();
		return str;
	}
	
	/**
	 * 从redis 队列头部插入一条数据,首先判断是否已存在于队列中
	 * @param url (待插入的URL)
	 */ 
	public boolean insertUrlToRedis(String url){
		
        long num = shardedJedis.lpush(KEY, url);
        returnResource();
    	if(num > 1) {
    		return true;
    	}
		return false;
	}
	
	/**
	 * 判断某一URL是否存在于队列中
	 * @param url
	 */
	public boolean isExist(String url) {
    	List<String> list = shardedJedis.lrange(KEY, 0, shardedJedis.llen(KEY));
    	returnResource();
    	if(list.contains(url)) {
    		return true;
    	}
		return false;
	}
	
	/**
	 * 判断redis url队列中是否空
	 */ 
	public boolean isNull() {
    	long length = shardedJedis.llen(KEY);
    	returnResource();
    	if(length == 0) {
    		return true;
    	}
		return false;
	}
	
	public static void main(String[] args) {

		RedisUtil ru = new RedisUtil();
		System.out.println(ru.insertUrlToRedis("http://134"));
	}
}

2014年8月20日 20:23
目前还没有答案

相关推荐

Global site tag (gtag.js) - Google Analytics