question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support @RequestScoped beans in command line mode

See original GitHub issue

Description Having

@RequestScoped
public class MyJPAService {
}

And the main class for a command line application:

@QuarkusMain
public class MainApplication implements QuarkusApplication {
    @Inject
    MyJPAService service;

    public static void main(String[] args) {
        Quarkus.run(MainApplication.class, args);
    }
    @Override
    public int run(String... args) throws Exception {
        System.out.println("Instance:  " + service);
        return 0;
    }
}

When I run mvn quarkus:dev, I get the following exception:

2020-04-22 13:29:50,255 ERROR [io.qua.run.Application] (Quarkus Main Thread) Error running Quarkus application: javax.enterprise.context.ContextNotActiveException: interface javax.enterprise.context.RequestScoped

A workaround is to call Arc.container().requestContext().activate(); in the MainApplication#run method (and deactivate when it’s done).

Implementation ideas Change the core implementation that calls my Main method to initialize the Arc container if a @RequestScoped class is used.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
mkoubacommented, Apr 24, 2020

@mkouba you mean users should be required to put Arc.container().requestContext().activate(); in every cli or that you think the context should just always be activated ?

Nope. We should either activate it automatically or encourage users to use @ActivateRequestContext.

1reaction
mkoubacommented, Apr 23, 2020

If you activate request context for the whole main method, it means it will be active from app start to its shutdown. That kind of defeats the purpose of the scope…

I believe that the intention is to support @RequestScoped beans during “quarkus command execution”. And the request context is not necessarlly bound to an HTTP request. There are few exceptions in the spec and we have few more in Quarkus. In this case, the command execution is a “request”.

Yes, @ActivateRequestContext should work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing Command Mode - Quarkus
In Quarkus 1.4 command mode lets you write apps that run without an ... If you would like to access request scoped beans...
Read more >
Spring - Request scoped bean from object pool - Stack Overflow
Implement a HandlerInterceptor and inject it with a request scoped bean. When preHandle is called, setup the bean with the correct value.
Read more >
9 Using Contexts and Dependency Injection for the Java EE ...
This example defines the scope of the Accountant bean class to be @RequestScoped . The Accountant class in this example is qualified by...
Read more >
A Command Line Application with Quarkus - Adam Bien
Command mode already ships with quarkus runtime, there are no additional ... Therefore scopes like e.g. @RequestScoped are not available.
Read more >
Context and Dependency Injection beans :: Open Liberty Docs
Contexts, which help you bind the lifecycle and interactions of stateful ... beans.xml file or a beans.xml file with the bean-discovery-mode="all" attribute ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found