I'm putting together a pretty simple application that needs to launch a
bunch of background threads to do some work, and the page waits for all of
them to complete, and then returns the aggregated results.

Now, that sounds pretty straightforward (e.g. sample scala code). Now, the
interesting part is that when the variable is accessed in the separate
thread, it is null.

I was able to work around it by assigning the value of sn to a local
variable, at which point it stopped being null when it's accessed in the
threads.

My theory goes that this has something to do w/ how Tapestry stores the
page instance variables (as map values in the thread), and when the
variable is accessed in the new threads, they have nothing to back them up.
My workaround seemed to work OK in my simple case, but what is the more
general solution (and yes, I know that the JavaEE spec says that you're not
supposed to start threads) where the current thread's instance data would
be available in the new threads ?



class ShowServer {
  @PageActivationContext @Property
  var sn:String = _

def setupRender():Unit = {
   val servDetFut  = for {
      jg <- future { mgr.jarGroupName(sn)}
      props = mgr.props(sn)
      pl <- future { mgr.players(sn) }
      worlds <- future { mgr.listWorlds(sn)}
      ops <- future { mgr.ops(sn) }
      wl <- future { mgr.whitelist(sn)}
      bl <- future { mgr.blacklist(sn) }
    } yield McServerDetails(
      server = mcServer,
      jarGroup = jg,
      worlds = worlds.toArray,
      players = pl.toArray,
      ops = ops.toArray,
      whiteList = wl.toArray,
      blackListed = bl,
      props = props
    )
    }

    serverDetails = Await.result(servDetFut, 60.seconds)
}

Cheers - Alex K

Reply via email to