From 2bb6f847428e85226c7e0875ceff7602b5c43198 Mon Sep 17 00:00:00 2001 From: "yihua.huang" Date: Fri, 26 Jul 2013 19:12:55 +0800 Subject: [PATCH] fix redis null pointer exception --- .../webmagic/scheduler/RedisScheduler.java | 36 ++++--------------- 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/webmagic-plugin/webmagic-misc/src/main/java/us/codecraft/webmagic/scheduler/RedisScheduler.java b/webmagic-plugin/webmagic-misc/src/main/java/us/codecraft/webmagic/scheduler/RedisScheduler.java index 575beef..382642b 100644 --- a/webmagic-plugin/webmagic-misc/src/main/java/us/codecraft/webmagic/scheduler/RedisScheduler.java +++ b/webmagic-plugin/webmagic-misc/src/main/java/us/codecraft/webmagic/scheduler/RedisScheduler.java @@ -7,9 +7,6 @@ import us.codecraft.webmagic.Request; import us.codecraft.webmagic.Task; import us.codecraft.webmagic.schedular.Scheduler; -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.ReentrantLock; - /** * 使用redis管理url,构建一个分布式的爬虫。
* @@ -25,10 +22,6 @@ public class RedisScheduler implements Scheduler { private static final String SET_PREFIX = "set_"; - private ReentrantLock lock = new ReentrantLock(); - - private Condition condition = lock.newCondition(); - public RedisScheduler(String host) { pool = new JedisPool(new JedisPoolConfig(), host); } @@ -38,15 +31,9 @@ public class RedisScheduler implements Scheduler { Jedis jedis = pool.getResource(); //使用SortedSet进行url去重 if (jedis.zrank(SET_PREFIX + task.getUUID(), request.getUrl()) == null) { - try { - lock.lock(); - //使用List保存队列 - jedis.rpush(QUEUE_PREFIX + task.getUUID(), request.getUrl()); - jedis.zadd(SET_PREFIX + task.getUUID(), System.currentTimeMillis(), request.getUrl()); - condition.signal(); - } finally { - lock.unlock(); - } + //使用List保存队列 + jedis.rpush(QUEUE_PREFIX + task.getUUID(), request.getUrl()); + jedis.zadd(SET_PREFIX + task.getUUID(), System.currentTimeMillis(), request.getUrl()); } pool.returnResource(jedis); } @@ -55,21 +42,10 @@ public class RedisScheduler implements Scheduler { public synchronized Request poll(Task task) { Jedis jedis = pool.getResource(); String url = jedis.lpop(QUEUE_PREFIX + task.getUUID()); - if (url == null) { - try { - lock.lock(); - while (url == null) { - try { - condition.await(); - url = jedis.lpop(QUEUE_PREFIX + task.getUUID()); - } catch (InterruptedException e) { - } - } - } finally { - lock.unlock(); - } - } pool.returnResource(jedis); + if (url==null){ + return null; + } return new Request(url); } }