GAE JCache NumberFormatException,我需要编写 Java 来避免吗?
下面的代码在这一行中产生 NumberFormatException:
val cache = cf.createCache(Collections.emptyMap())
您看到任何错误吗?
我需要 写一个Java版本来避免这种情况,或者有Scala的方法吗?
...
import java.util.Collections
import net.sf.jsr107cache._
object QueryGenerator extends ServerResource {
private val log = Logger.getLogger(classOf[QueryGenerator].getName)
}
class QueryGenerator extends ServerResource {
def getCounter(cache:Cache):long = {
if (cache.containsKey("counter")) {
cache.get("counter").asInstanceOf[long]
} else {
0l
}
}
@Get("html")
def getHtml(): Representation = {
val cf = CacheManager.getInstance().getCacheFactory()
val cache = cf.createCache(Collections.emptyMap())
val counter = getCounter(cache)
cache.put("counter", counter + 1)
val q = QueueFactory.getQueue("query-generator")
q.add(TaskOptions.Builder.url("/tasks/query-generator").method(Method.GET).countdownMillis(1000L))
QueryGenerator.log.warning(counter.toString)
new StringRepresentation("QueryGenerator started!", MediaType.TEXT_HTML)
}
}
谢谢!
The code below produces a NumberFormatException in this line:
val cache = cf.createCache(Collections.emptyMap())
Do you see any errors?
Will I need to write a Java version to avoid this, or is there a Scala way?
...
import java.util.Collections
import net.sf.jsr107cache._
object QueryGenerator extends ServerResource {
private val log = Logger.getLogger(classOf[QueryGenerator].getName)
}
class QueryGenerator extends ServerResource {
def getCounter(cache:Cache):long = {
if (cache.containsKey("counter")) {
cache.get("counter").asInstanceOf[long]
} else {
0l
}
}
@Get("html")
def getHtml(): Representation = {
val cf = CacheManager.getInstance().getCacheFactory()
val cache = cf.createCache(Collections.emptyMap())
val counter = getCounter(cache)
cache.put("counter", counter + 1)
val q = QueueFactory.getQueue("query-generator")
q.add(TaskOptions.Builder.url("/tasks/query-generator").method(Method.GET).countdownMillis(1000L))
QueryGenerator.log.warning(counter.toString)
new StringRepresentation("QueryGenerator started!", MediaType.TEXT_HTML)
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑异常确实发生在对 getCounter 的调用中。当您尝试将字符串转换为数字并且该字符串不包含可识别的数字时,会引发
NumberFormatException
。I suspect that the exception is really happening in the call to
getCounter
.NumberFormatException
is thrown when you try to convert a String to a number, and that String does not contain a recognizable number.我现在添加了一个小的 Java 类用于放置/更新。这不是最优雅的解决方案,但它确实有效。 Scala 解决方案仍然会受到赞赏。
Java:
Scala:
I have added a small Java Class for the putting/updating now. Not the most elegant solution, but it works. A Scala solution would still be appreciated.
Java:
Scala: