views:

96

answers:

2

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!

+2  A: 

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.

Stephen C
A: 

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)
  }
}
Jasper
Your two pieces of code aren't equivalent--one uses `Long`, the other `Int`, and you changed your imports. Also, you rewrote in Java a line of code that seems unrelated to the crash. Are you positive that if you write CacheHelper as an object in Scala that does exactly the same thing that it crashes? (I.e. `object CacheHelper { def update ... }`.) It would be very weird if it did.
Rex Kerr
I did not write an object, but had the method in the QueryGenerator Class. It generated some error already [mentioned here][k].Sorry about the import and type inconsistencies. I just played around a lot in the meantime.[k]: http://stackoverflow.com/questions/1657494/how-to-use-jcache-in-scala-i-get-compiler-type-error-found-string-required-k
Jasper