GAE JCache NumberFormatException,我需要编写 Java 来避免吗?

发布于 2024-08-29 19:21:23 字数 1279 浏览 2 评论 0原文

下面的代码在这一行中产生 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2024-09-05 19:21:23

我怀疑异常确实发生在对 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.

拔了角的鹿 2024-09-05 19:21:23

我现在添加了一个小的 Java 类用于放置/更新。这不是最优雅的解决方案,但它确实有效。 Scala 解决方案仍然会受到赞赏。

Java:

import javax.cache.Cache;

public class CacheHelper {
    public static final void update(Cache cache,String key,Object value) {
        cache.put(key,value);
    }
}

Scala:

import java.util.Collections
import javax.cache.CacheManager
import somewhere.CacheHelper

object QueryGenerator extends ServerResource {
  private val log = Logger.getLogger(classOf[QueryGenerator].getName)
}

class QueryGenerator extends ServerResource {

  @Get("html")
  def getHtml(): Representation = {
    val cf = CacheManager.getInstance().getCacheFactory()
    val cache = cf.createCache(Collections.emptyMap())

    val counter = if (cache.containsKey("counter")) {
      cache.get("counter").asInstanceOf[Int]
    } else {
      0
    }

    CacheHelper.update(cache,"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)
  }
}

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:

import javax.cache.Cache;

public class CacheHelper {
    public static final void update(Cache cache,String key,Object value) {
        cache.put(key,value);
    }
}

Scala:

import java.util.Collections
import javax.cache.CacheManager
import somewhere.CacheHelper

object QueryGenerator extends ServerResource {
  private val log = Logger.getLogger(classOf[QueryGenerator].getName)
}

class QueryGenerator extends ServerResource {

  @Get("html")
  def getHtml(): Representation = {
    val cf = CacheManager.getInstance().getCacheFactory()
    val cache = cf.createCache(Collections.emptyMap())

    val counter = if (cache.containsKey("counter")) {
      cache.get("counter").asInstanceOf[Int]
    } else {
      0
    }

    CacheHelper.update(cache,"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)
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文