5

My technical stack includes

To be able to use ManagedExecutorService and manage threads more wisely and in a safer manner, I would like to include this dependency

I tried to inject the bean in different ways and into differently-scoped beans (view-, session-, application-based).

  @Inject
  private javax.enterprise.concurrent.ManagedExecutorService service;

  @Resource
  private javax.enterprise.concurrent.ManagedExecutorService service;

  @Resource(lookup="java:comp/DefaultManagedExecutorService")
  private javax.enterprise.concurrent.ManagedExecutorService service;

Nothing seems to work and the exception goes

java.lang.RuntimeException:
    Error looking up java:comp/env/.../ManagedExecutorService in JNDI
javax.naming.NameNotFoundException:
    Name [.../ManagedExecutorService] is not bound in this Context.

It might be due to improper bean registration. Because I found none. I scanned the whole classpath and couldn't find anything that defines ManagedExecutorService or any of its implementations somewhere.

I tried to register it by myself in the context.xml to no avail. I used this TomEE tutorial (naively) assuming that configuration for my non-EE Tomcat would be the same.

<Resource name="ManagedExecutorService"
          type="javax.enterprise.concurrent.ManagedExecutorService" />

<Resource id="ManagedExecutorService"
          type="javax.enterprise.concurrent.ManagedExecutorService" />

Apparently, it's not.

My questions are

  1. Is it possible to set up Concurrency Utilities on a non-EE server, Tomcat 8.5 in particular?

  2. If so, what am I missing here?

I can't switch to any JavaEE application server.
I can't upgrade the listed dependencies.

Related:

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • So "@Inject" will definitely not work in Tomcat. Those is a JavaEE annotation, and Tomcat simply ignores it. Tomcat only implements the JSP and Servlet parts of JavaEE. You would need to use Apache TomEE which does work quite nicely :) – Jonathan S. Fisher Nov 15 '19 at 16:14
  • You would need to add the Glassfish implementation to the `tomcat/lib` directory, then in the `server.xml`, initialize an instance of it. Then, in a servlet, or filter, you can use the `@Resource` annotation to inject it (you shouldn't need to qualify the name if it's the only one) – Jonathan S. Fisher Nov 15 '19 at 16:17
  • 1
    If you're using Weld and Tomcat, you really ought to switch to TomEE. You're currently trying to hand-assemble a JavaEE server actually, and TomEE is a community that has done all the footwork for you. – Jonathan S. Fisher Nov 15 '19 at 16:18
  • @JonathanS.Fisher Thanks, I really appreciate you responded to my comments under your answer there. Actually, I made CDI work in plain Tomcat, and I successfully moved all my `@ManagedBean`s to CDI beans (e.g. `@Named` + `@SessionScoped`) – Andrew Tobilko Nov 15 '19 at 16:49
  • @JonathanS.Fisher I understand that I am basically assembling TomEE (a more lightweight and with fewer dependencies version). I don't need 80% of what TomEE is offering. And I suppose moving a big project from Tomcat to TomEE might be problematic. – Andrew Tobilko Nov 15 '19 at 17:02
  • @JonathanS.Fisher I am working on a small feature that requires spawning threads from CDI components. I could implement my own mechanism, but why should I if there is already a JSR and independent (I hope so - I started doubting this part) reference implementation for it? – Andrew Tobilko Nov 15 '19 at 17:04
  • Can I ask what the goal is? You say that you don't need 80% of what is in TomEE but it's unclear what you do need. If you're looking for a micro-service environment then something like [Quarkus](https://quarkus.io/) may be an option. Wildfly isn't bad but certainly wouldn't be used for something like an AWS Lambda. So, again, what problem are you trying to solve? – stdunbar Nov 15 '19 at 17:51
  • 1
    @stdunbar I want to make as little changes as possible (configuration-wise, and dependency-wise) and to have some EE features work in plain Tomcat (CDI [done] and Concurrency Management in CDI context [that's what the question is about]). It's not an architecture question. – Andrew Tobilko Nov 15 '19 at 17:57

0 Answers0