Scala Redis驱动程序 - usng blpop命令

时间:2013-12-07 22:44:15

标签: scala redis

我正在尝试使用blpop Redis命令,该命令会在10秒后拉出一些Long值:

 val r = new RedisClient("10.0.0.15", 6379)

    implicit val parseV =  new com.redis.serialization.Parse[Long](f=>{ByteBuffer.wrap(f).getLong})
    val rs =  r.blpop[String,Long](10,"queue")
    rs match{
        case Some(s)=>println(s._2)
        case None => println("timeOut");
    }

然而,在超时之后,我将获得以下例外,

有趣的是,如果返回的Object长度很大,如果byte []应该是8个字节(Java的长度是64位),但是如果异常则返回的byte []有5个字节的长度

它是什么? 我该如何照顾或防止这种例外? 这是StackTrace:

Exception in thread "main" java.nio.BufferUnderflowException
    at java.nio.Buffer.nextGetIndex(Buffer.java:498)
    at java.nio.HeapByteBuffer.getLong(HeapByteBuffer.java:406)
    at com.vanilla.scala.redis.ScalaRedis$$anonfun$1.apply(ScalaRedis.scala:14)
    at com.vanilla.scala.redis.ScalaRedis$$anonfun$1.apply(ScalaRedis.scala:13)
    at com.redis.serialization.Parse.apply(Serialization.scala:54)
    at com.redis.serialization.Parse.apply(Serialization.scala:53)
    at scala.Option.map(Option.scala:145)
    at com.redis.R$class.asBulk(RedisProtocol.scala:121)
    at com.redis.RedisClient.asBulk(RedisClient.scala:60)
    at com.redis.StringOperations$$anonfun$get$1.apply(StringOperations.scala:26)
    at com.redis.StringOperations$$anonfun$get$1.apply(StringOperations.scala:26)
    at com.redis.Redis$class.send(RedisClient.scala:21)
    at com.redis.RedisClient.send(RedisClient.scala:60)
    at com.redis.StringOperations$class.get(StringOperations.scala:26)
    at com.redis.RedisClient.get(RedisClient.scala:60)
    at com.vanilla.scala.redis.ScalaRedis$delayedInit$body.apply(ScalaRedis.scala:23)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:71)
    at scala.App$$anonfun$main$1.apply(App.scala:71)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
    at scala.App$class.main(App.scala:71)
    at com.vanilla.scala.redis.ScalaRedis$.main(ScalaRedis.scala:7)
    at com.vanilla.scala.redis.ScalaRedis.main(ScalaRedis.scala)

1 个答案:

答案 0 :(得分:1)

我认为你得到了这个异常,因为redis返回的值实际上是Long值的String表示。

看看RedisClient提供的Implicit Parser,你会看到它首先构造一个String(来自UTF-8编码的字节),然后在其上调用toLong()。

这也意味着您可以使用此Parser而不是定义自己的:

val r = new RedisClient("localhost", 6379)

implicit val parseV = com.redis.serialization.Parse.Implicits.parseLong
val rs =  r.blpop[String,Long](10,"queue")
rs match{
    case Some(s)=>println(s._2)
    case None => println("timeOut");
}