1

I'm trying to init CDI-SE context inside my Quartz Application, so i have the following dependency (maven):

  <dependency>
                <groupId>org.jboss.weld.se</groupId>
                <artifactId>weld-se-core</artifactId>
                <version>2.3.4.Final</version>
            </dependency>

Inside my JobQuartz i have the method execute() with the following:

public void execute(JobExecutionContext context) throws JobExecutionException {
        Weld weld = new Weld();
        WeldContainer container = weld.initialize();
        service = container.instance().select(MyService.class).get();
        service.go();
        weld.shutdown();
    }

But i got the following error:

Caused by: java.lang.IllegalStateException: WELD-ENV-002009: Weld SE container cannot be initialized - no bean archives found

My project is a WAR, so i putted beans.xml file inside /src/main/webapp/META-INF/, see the content:

<?xml version="1.0"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.2" bean-discovery-mode="all">

</beans>

I copied the file to /src/main/resource/META-INF, but i got the same error.

Ronaldo Lanhellas
  • 2,975
  • 5
  • 46
  • 92
  • How do you deploy or "start" this WAR? Normally, you should be using Weld SE with JAR. WAR is usually server/servlet where you use standard Weld/weld-servlet (neither of which you manually start). – Siliarus Nov 21 '17 at 06:30
  • As a side note (doesn't solve your problem), `WeldContainer` implements `Instance` therefore you should be able to do `container.select(MyService.class)`. The `WeldContainer.instance()` method has been deprecated in newer versions. – Siliarus Nov 21 '17 at 06:34
  • @Siliarus, i deploy my war in JBoss but quartz i need some scopes that quartz dont have, so i used weld se – Ronaldo Lanhellas Nov 21 '17 at 10:17
  • beans.xml must be in `/src/main/webapp/WEB-INF/`, not `/src/main/webapp/META-INF/` – Rouliboy Nov 21 '17 at 11:07
  • I have beans.xml in this Path too, but i got same error – Ronaldo Lanhellas Nov 21 '17 at 11:16
  • If its JBoss EAP or WildFly you are using, then you are in EE and using Weld SE container won't work for you. The server has its own CDI implementation (another flavor of Weld in fact) which it will bootstrap on its own. All you need to do is have dependency on CDI and you should be able to use it (plus of course beans.xml depending in discovery mode you wish to have). – Siliarus Nov 21 '17 at 11:17
  • @Siliarus, I have a web application (war) as you can see, everything works normal with my CDI. But when i use Quartz i don't have some scopes that i need, like: RequestedScope. To solve it i'm trying to start Scope manually with Weld-SE, this is correct ? – Ronaldo Lanhellas Nov 21 '17 at 11:42
  • No, this isn't correct. What version of weld are you using? – John Ament Nov 21 '17 at 12:04
  • @RonaldoLanhellas no, that is incorrect. I'll try to formulate an aswer based on the details you gave me here. – Siliarus Nov 21 '17 at 12:55

2 Answers2

1

After some conversation in comments section I think I understand enough to answer you.

First of all, you should not start Weld SE container on your own as you then have two containers running side by side (which isn't intended/supported) - one SE and one "classic", handled by the container. Stick with the container-handled one, which is booted for you effortlessly.

Now, I see you are missing some scope activation means. If you are using some newer version of Weld, you can make use of an interceptor, which will activate RequestContext (I suppose that's the one you are after) before a method and tear it down afterwards. All you need for that is a dependency on Weld API (which is included in WFLY anyway) and then you simply annotate your method or class with that.

For the above you need Weld 2.4.x. Note that you can quite simply patch your WildFly. The patches are at the bottom of the Weld website and the how-to can be found here.

If you were to use Weld 3/CDI 2.0 then there is even a built-in bean (RequestContextController) which allows you to control this lifecycle.

Other option is then Deltaspike, as Johm Ament pointed out, but that requires you to bring in another dependency.

Siliarus
  • 6,393
  • 1
  • 14
  • 30
  • I think that i found another solution: Force my Quartz JOB to call my Service from a Rest Client, in this way i have a CDI Context. I tried and works fine. What do you think – Ronaldo Lanhellas Nov 21 '17 at 13:14
  • You haven't shown the actual code which doesn't meet your expectations so it's hard to judge. However what you describe sounds OK as in general it simply creates request hence triggering request scope context. That sounds valid. – Siliarus Nov 21 '17 at 13:49
  • Exactly, i just triggering a HTTP Request using JAX-RS. – Ronaldo Lanhellas Nov 21 '17 at 13:51
0

If what you're trying to do is start a request context, you have a couple of solutions.

  1. Use DeltaSpike's Quartz Integration for CDI - http://deltaspike.apache.org/documentation/scheduler.html

  2. Programmatically start and stop a context using BoundRequestContext (it's a CDI bean you can inject).

I would strongly recommend the DeltaSpike approach, since it takes care of all of the setup you would need for the job to automatically start the context.

John Ament
  • 11,595
  • 1
  • 36
  • 45