ContextNotActiveException: RequestScope

2,144 views
Skip to first unread message

William Burke

unread,
Jul 26, 2019, 11:22:39 AM7/26/19
to Quarkus Development mailing list
I'm trying to do reflection on a bean annotated with @RequestScope and @ActivateRequestContext.  This works within a @QuarkusTest, but not in my program.  Ideas?

pseudo code:

Class myClass = ...;
Method methodOnMyClass = ...;
Object target = BeanContainer.instance(myClass);
method.invoke(target, params);

Caused by: javax.enterprise.context.ContextNotActiveException: interface javax.enterprise.context.RequestScoped
at io.quarkus.Function_ClientProxy.arc$delegate(Function_ClientProxy.zig:131)
at io.quarkus.Function_ClientProxy.run(Function_ClientProxy.zig:55)

--
Bill Burke
Red Hat

Martin Kouba

unread,
Jul 29, 2019, 1:33:28 AM7/29/19
to bbu...@redhat.com, Quarkus Development mailing list
Hey Bill,

I need more info, ideally a minimal reproducer. It could be that you're
calling a proxy from a different thread than the thread where the
context was activated.

Martin

Dne 26. 07. 19 v 17:22 William Burke napsal(a):
> --
> You received this message because you are subscribed to the Google
> Groups "Quarkus Development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to quarkus-dev...@googlegroups.com
> <mailto:quarkus-dev...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/quarkus-dev/CAL%3DE%3DjQMH164h8o%3DY5ANrCY8u2N0C65tD_4Jqm23nexXwDyuNQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/quarkus-dev/CAL%3DE%3DjQMH164h8o%3DY5ANrCY8u2N0C65tD_4Jqm23nexXwDyuNQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

--
Martin Kouba
Senior Software Engineer
Red Hat, Czech Republic

William Burke

unread,
Jul 29, 2019, 9:08:11 AM7/29/19
to Quarkus Development mailing list
I switched to @Dependent.  Sorry for ignorance.

Still, I don't understand  @ActivateRequestContext .  Should that let you NOT set a request context before invoking the method?  

Martin Kouba

unread,
Jul 29, 2019, 9:35:10 AM7/29/19
to bbu...@redhat.com, Quarkus Development mailing list, Matej Novotny
Dne 29. 07. 19 v 15:07 William Burke napsal(a):
> I switched to @Dependent.  Sorry for ignorance.
>
> Still, I don't understand @ActivateRequestContext .  Should that let you
> NOT set a request context before invoking the method?

Yes, @ActivateRequestContext is an interceptor binding for which an
container provides an interceptor that should activate the request
context before the method is invoked (unless the request is already active).

Hm, but now I think I finally understand the problem. You can't use the
binding on the bean class itself, e.g.:

@RequestScoped
class MyBean {

@ActivateRequestContext // --> failure, the proxy needs to get the
bean instance from the context first...
void ping() {
...
}

}

It's not very obvious but I think it's implied. Using the spec wording:
"every time a method of the bean is invoked upon a client proxy, the
client proxy must obtain a contextual instance of the bean..." and "The
container must obtain the active context object for the bean scope, then
obtain an instance of the bean by calling Context.get()". [1]

But there is no active context because interceptors were not applied
yet, at least in ArC. Actually, I'm not so sure about Weld but I assume
that it would be the same since it's also using subclasess for
interceptors. @manovotn am I right?

Martin

[1]
https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#client_proxy_invocation

>
> On Fri, Jul 26, 2019 at 11:22 AM William Burke <bbu...@redhat.com
> <mailto:bbu...@redhat.com>> wrote:
>
> I'm trying to do reflection on a bean annotated with @RequestScope
> and @ActivateRequestContext.  This works within a @QuarkusTest, but
> not in my program.  Ideas?
>
> pseudo code:
>
> Class myClass = ...;
> Method methodOnMyClass = ...;
> Object target = BeanContainer.instance(myClass);
> method.invoke(target, params);
>
> Caused by: javax.enterprise.context.ContextNotActiveException:
> interface javax.enterprise.context.RequestScoped
> at
> io.quarkus.Function_ClientProxy.arc$delegate(Function_ClientProxy.zig:131)
> at io.quarkus.Function_ClientProxy.run(Function_ClientProxy.zig:55)
>
> --
> Bill Burke
> Red Hat
>
>
>
> --
> Bill Burke
> Red Hat
>
> --
> You received this message because you are subscribed to the Google
> Groups "Quarkus Development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to quarkus-dev...@googlegroups.com
> <mailto:quarkus-dev...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/quarkus-dev/CAL%3DE%3DjSDEgqqSTyNo4SPg-8LK2Wkj_Oz4b9tPp3owCYeBfA2VA%40mail.gmail.com
> <https://groups.google.com/d/msgid/quarkus-dev/CAL%3DE%3DjSDEgqqSTyNo4SPg-8LK2Wkj_Oz4b9tPp3owCYeBfA2VA%40mail.gmail.com?utm_medium=email&utm_source=footer>.

William Burke

unread,
Jul 29, 2019, 9:48:04 AM7/29/19
to Martin Kouba, Quarkus Development mailing list, Matej Novotny
If that was true then you wouldn't be able to apply the annotation to the class as its @Target would only be set to method.

Martin Kouba

unread,
Jul 29, 2019, 9:56:16 AM7/29/19
to William Burke, Quarkus Development mailing list, Matej Novotny
Dne 29. 07. 19 v 15:47 William Burke napsal(a):
> If that was true then you wouldn't be able to apply the annotation to
> the class as its @Target would only be set to method.

I'm sorry but I don't understand. It's an interceptor binding annotation
and so it's ok to declare it on both the type and the method.

Anyway, the main use case for this annotation is to active the request
context for a method declared on @ApplicationScoped/@Dependent bean and
use an injected @RequestScoped bean (aka it's client proxy) inside the
> <mailto:quarkus-dev%2Bunsu...@googlegroups.com>
> > <mailto:quarkus-dev...@googlegroups.com
> <mailto:quarkus-dev%2Bunsu...@googlegroups.com>>.
Reply all
Reply to author
Forward
0 new messages