users@jersey.java.net

Re: [Jersey] Fwd: REST API: _at_Context raises java.lang.IllegalStateException exception

From: Viktor Klang <viktor.klang_at_gmail.com>
Date: Thu, 20 May 2010 09:56:16 +0200

On Thu, May 20, 2010 at 9:20 AM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:

>
> On May 20, 2010, at 7:28 AM, Pekka Mattila wrote:
>
> Thank you Viktor!
>
> Paul, I think Viktor handles this issue. Do you still want a simple Maven
> project or should we wait Viktor's work?
>
>
> Let's wait.
>
> Viktor, if you need any help/advice just shout!
>

Thanks Paul, I probably will :)


>
> Paul.
>
> Thank you for your help! :)
>
> Cheers,
> Pekka :)
>
> On Wed, May 19, 2010 at 8:25 PM, Viktor Klang <viktor.klang_at_gmail.com>wrote:
>
>> Hi guys,
>>
>> We're currently investigating another option in integrating with Jersey
>> and other web-frameworks in general so don't waste any time on this issue.
>>
>> Pekka: Hopefully we'll be able to offer you a solution for this, and it
>> looks right now like we'll separate the REST-bean from the Actor,
>> it's pretty much impossible to do anything else after the introduction of
>> ActorRef,
>>
>> I hope this will help,
>>
>>
>> On Wed, May 19, 2010 at 6:47 PM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
>>
>>> Hi Pekka,
>>>
>>> The URL got truncated can you resend.
>>>
>>> I dunno what constraints Akka imposes when resource class (something
>>> annotated with @Path) implements an Actor.
>>>
>>> The error indicates that the HttpServletRequest is being accessed out of
>>> the scope of a request. Looks like the case class Hello.toString occurs for
>>> logging and that calls the HttpServletRequest.toString on the
>>> HttpServletRequest field value of the case class.
>>>
>>> This can happen if thread boundaries are crossed. But i would have
>>> expected in this case that the HttpServletRequest instance passed to the
>>> hello function would not be a proxy.
>>>
>>> If you can package up a simple maven project i can investigate further.
>>>
>>> Paul.
>>>
>>> On May 17, 2010, at 12:34 PM, Pekka Mattila wrote:
>>>
>>> Hi,
>>>>
>>>> Here is a forwarded message about a problem of @Context and thread
>>>> local.
>>>>
>>>> Cheers,
>>>> Pekka :)
>>>>
>>>>
>>>>
>>>> ---------- Forwarded message ----------
>>>> From: Viktor Klang <viktor.kl..._at_gmail.com>
>>>> Date: May 17, 12:55 pm
>>>> Subject: REST API: @Context raises java.lang.IllegalStateException
>>>> exception
>>>> To: Akka User List
>>>>
>>>>
>>>> Hi Pekka!
>>>>
>>>> The relevant Jersey integration is provided by:
>>>>
>>>>
>>>> http://github.com/jboner/akka/blob/master/akka-http/src/main/scala/Ac...http://github.com/jboner/akka/blob/master/akka-http/src/main/scala/Ac...http://github.com/jboner/akka/blob/master/akka-http/src/main/scala/Ak.
>>>> ..
>>>>
>>>>
>>>> Perhaps you could crosspost this to the jersey user ML to see if
>>>> anyone
>>>> knows the root of this?
>>>>
>>>> Cheers,
>>>>
>>>> On Mon, May 17, 2010 at 11:48 AM, Pekka Mattila
>>>> <pe..._at_starduckstudios.com>wrote:
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> Hi all,
>>>>>
>>>>
>>>> I am trying to use @Context annotation with Akka REST, but it raises
>>>>> java.lang.IllegalStateException.
>>>>>
>>>>
>>>> Here is an example code:
>>>>>
>>>>
>>>> @Path("/hello")
>>>>> class TestService extends Actor {
>>>>>
>>>>
>>>> private case class Hello(request: HttpServletRequest)
>>>>>
>>>>
>>>> @GET
>>>>> @Produces(Array("text/plain"))
>>>>> def hello(@Context request: HttpServletRequest): Any = {
>>>>> (this !! Hello(request)).getOrElse("Couldn't say hello!")
>>>>> }
>>>>>
>>>>
>>>> def receive = {
>>>>> case Hello(request) => reply(helloImpl(request))
>>>>> }
>>>>>
>>>>
>>>> private def helloImpl(request: HttpServletRequest): Response = {
>>>>> try {
>>>>> println("request: "+request)
>>>>> } catch {
>>>>> case e: Throwable =>
>>>>> throw new RuntimeException(e)
>>>>> }
>>>>>
>>>>
>>>> return Response.status(Response.Status.OK).build
>>>>> }
>>>>>
>>>>
>>>> }
>>>>>
>>>>
>>>> I get the following stack trace:
>>>>>
>>>>
>>>> java.lang.IllegalStateException
>>>>> at
>>>>>
>>>>
>>>> com.sun.jersey.server.impl.container.servlet.ThreadLocalInvoker.invoke(Thre
>>>>> adLocalInvoker.java:
>>>>>
>>>>> 90)
>>>>> at $Proxy16.toString(Unknown Source)
>>>>> at java.lang.String.valueOf(String.java:2826)
>>>>> at scala.collection.Iterator$class.addString(Iterator.scala:1192)
>>>>> at scala.Product$$anon$1.addString(Product.scala:37)
>>>>> at scala.collection.Iterator$class.mkString(Iterator.scala:1159)
>>>>> at scala.Product$$anon$1.mkString(Product.scala:37)
>>>>> at scala.runtime.ScalaRunTime$._toString(ScalaRunTime.scala:146)
>>>>> at rest.TestService$Hello.toString(TestService.scala:16)
>>>>> at java.util.Formatter$FormatSpecifier.printString(Formatter.java:
>>>>> 2794)
>>>>> at java.util.Formatter$FormatSpecifier.print(Formatter.java:2677)
>>>>> at java.util.Formatter.format(Formatter.java:2433)
>>>>> at java.util.Formatter.format(Formatter.java:2367)
>>>>> at java.lang.String.format(String.java:2769)
>>>>> at net.lag.logging.Formatter.format(Formatter.scala:161)
>>>>> at net.lag.logging.FileHandler.liftedTree1$1(FileHandler.scala:
>>>>> 123)
>>>>> at net.lag.logging.FileHandler.publish(FileHandler.scala:119)
>>>>> at java.util.logging.Logger.log(Logger.java:458)
>>>>> at net.lag.logging.Logger.log(Logger.scala:108)
>>>>> at net.lag.logging.Logger.error(Logger.scala:118)
>>>>> at se.scalablesolutions.akka.actor.Actor
>>>>> $class.transactionalDispatch(Actor.scala:971)
>>>>> at se.scalablesolutions.akka.actor.Actor
>>>>> $class.liftedTree1$1(Actor.scala:901)
>>>>> at se.scalablesolutions.akka.actor.Actor$class.invoke(Actor.scala:
>>>>> 900)
>>>>> at rest.TestService.invoke(TestService.scala:14)
>>>>> at
>>>>>
>>>>> se.scalablesolutions.akka.dispatch.MessageInvocation.invoke(Reactor.scala:
>>>>> 23)
>>>>> at
>>>>> se.scalablesolutions.akka.dispatch.ExecutorBasedEventDrivenDispatcher$
>>>>> $anon$1.run(ExecutorBasedEventDrivenDispatcher.scala:75)
>>>>> at java.util.concurrent.ThreadPoolExecutor
>>>>> $Worker.runTask(ThreadPoolExecutor.java:886)
>>>>> at java.util.concurrent.ThreadPoolExecutor
>>>>> $Worker.run(ThreadPoolExecutor.java:908)
>>>>> at java.lang.Thread.run(Thread.java:637)
>>>>> at se.scalablesolutions.akka.dispatch.ThreadPoolBuilder
>>>>> $MonitorableThread.run(ThreadPoolBuilder.scala:244)
>>>>>
>>>>
>>>> Here is the Jersey code that throws the exception:
>>>>>
>>>>
>>>> public Object invoke(Object proxy, Method method, Object[] args)
>>>>> throws Throwable {
>>>>> if (threadLocalInstance.get() == null)
>>>>> throw new IllegalStateException(); <====== EXCEPTION
>>>>>
>>>>
>>>> try {
>>>>> return method.invoke(threadLocalInstance.get(), args);
>>>>> } catch (IllegalAccessException ex) {
>>>>> throw new IllegalStateException(ex);
>>>>> } catch (InvocationTargetException ex) {
>>>>> throw ex.getTargetException();
>>>>> }
>>>>> }
>>>>>
>>>>
>>>> Related Jersey code can be downloaded from:
>>>>>
>>>>
>>>> http://download.java.net/maven/2/com/sun/jersey/jersey-server/1.2/jer.
>>>>> ..
>>>>> .
>>>>>
>>>>
>>>> When TestService isn't Actor, everything works:
>>>>>
>>>>
>>>> @Path("/hello")
>>>>> class TestService {
>>>>>
>>>>
>>>> @GET
>>>>> @Produces(Array("text/plain"))
>>>>> def hello(@Context request: HttpServletRequest): Any = {
>>>>> helloImpl(request)
>>>>> }
>>>>>
>>>>
>>>> private def helloImpl(request: HttpServletRequest): Response = {
>>>>> try {
>>>>> println("request: "+request)
>>>>> } catch {
>>>>> case e: Throwable =>
>>>>> throw new RuntimeException(e)
>>>>> }
>>>>>
>>>>
>>>> return Response.status(Response.Status.OK).build
>>>>> }
>>>>>
>>>>
>>>> }
>>>>>
>>>>
>>>> So, any ideas how we can get this working with Akka?
>>>>>
>>>>
>>>> Cheers,
>>>>> Pekka
>>>>>
>>>>
>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups
>>>>> "Akka User List" group.
>>>>> To post to this group, send email to akka-user_at_googlegroups.com.
>>>>> To unsubscribe from this group, send email to
>>>>> akka-user+unsubscribe_at_googlegroups.com<akka-user%2Bunsubscribe_at_googlegroups.com>
>>>>> <akka-user%2Bunsubscribe_at_googlegroups .com>
>>>>>
>>>>> .
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/akka-user?hl=en.
>>>>>
>>>>
>>>> --
>>>> Viktor Klang
>>>> | "A complex system that works is invariably
>>>> | found to have evolved from a simple system
>>>> | that worked." - John Gall
>>>>
>>>> Akka - the Actor Kernel: Akkasource.org
>>>> Twttr: twitter.com/viktorklang
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Akka User List" group.
>>>> To post to this group, send email to akka-user_at_googlegroups.com.
>>>> To unsubscribe from this group, send email to akka-user
>>>> +unsubscribe_at_googlegroups.com.
>>>> For more options, visit this group athttp://
>>>> groups.google.com/group/akka-user?hl=en.
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>
>>>>
>>>
>>
>>
>> --
>> Viktor Klang
>> | "A complex system that works is invariably
>> | found to have evolved from a simple system
>> | that worked." - John Gall
>>
>> Akka - the Actor Kernel: Akkasource.org
>> Twttr: twitter.com/viktorklang
>>
>
>
>
> --
> Pekka Mattila
> Co-founder
> +358442818601
>
> Starduck Studios
> Sumeliuksenkatu 18 B, 1st. floor
> 33100 Tampere
> Finland
>
>
>


-- 
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang