3

I know this is considered bad practice, and forbidden in some case (ejb).

So, there are three distinct questions:

  1. what is the state of the art, specifically with JSR 236 that should address the question. Is that already included in some application server ?

  2. The forbidden rule talks about a Java EE container. What about Tomcat ? That it is not a fully featured Java EE container.

  3. I already searched the web and found many resources talking about the matter, but they are mainly blogs and posts about experience, and all of them are quite old. Do you have some links, an official one from Sun/Oracle would be better, that explains in great detail what is going on with threads and Java EE. Or at least, if it is not forbidden by SO policy, a good book.

[UPDATE]

For 'own thread' I mean to use either the java concurrency or the classic runnable interface with start, wait etc.

Mike Braun
  • 3,729
  • 17
  • 15
Leonardo
  • 9,607
  • 17
  • 49
  • 89
  • Your question is too broad, because there are "three distinct questions". – Beryllium Sep 11 '13 at 08:58
  • Do you suggest a rephrasing or removing some points ? – Leonardo Sep 11 '13 at 09:10
  • Check if Glassfish supports JSR 236. Try it, and post your problems. The 2nd question is only applicable to TomEE (Tomcat is no Java EE container), but this question is not bound to a specific one. As for the 3rd one, if the blogs are "quite" old", I would rather look at `@Asynchronous` and/or MDBs. – Beryllium Sep 11 '13 at 09:22

2 Answers2

2

what is the state of the art, specifically with JSR 236 that should address the question. Is that already included in some application server?

JSR 236 (Concurrency utils for Java EE) has eventually became part of Java EE 7. Java EE 7 is currently implemented by GlassFish 4, and it's expected that Wildfly 8 (formerly JBoss AS/EAP) will implement it early next year.

A current disadvantage of JSR 236 is that you cannot define your own thread pool; you have to do with the one provided by the application server which (IMHO) seriously affects the usability of the specification. You may be able to work around this limitation using proprietary methods, e.g. perhaps by interacting with a graphical UI or by modifying some file in the installation directory of your application (which you are typically not allowed to do in bigger companies).

See this for some additional resources about JSR 236.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • @Leonardo forbidden is a big word. We've always used our executor services in full application servers like JBoss and GlassFish. Just remember that those threads are unmanaged, and if you need an EJB you need to bootstrap it from the `global:` JNDI namespace. – Arjan Tijms Sep 18 '13 at 12:06
  • @Leonardo How about using TomEE instead of Tomcat. – Chris Ritchie Jan 27 '14 at 19:24
0

I can't think of too many occasions when this is the right thing to do.

One of the biggest problems when creating your own threads is that, depending on how you create them, they don't have access to all the features you're using a container for in the first place.

I recommend you try to use some sort of listener and then call an @Asynchronous method on your favorite bean. Just remember that in order for @Asynchronous to work, it has to be called through a proxy otherwise the container doesn't know anything about the call and it ends up being a normal method call in the same thread.

class MyListener {
    private MyBean proxy;

    MyListener(MyBean proxy) {
        this.proxy = proxy;
    }

    void handler(MyEvent event) {
        proxy.handler(event);
    }
}

@ApplicationScoped
public class MyBean {

    private MyBean proxy;

    @PostConstruct
    private void init() {
        // Use JNDI or BeanManager to get a proxy to this bean
        proxy = ??
    }

    @Asynchronous
    void handler(MyEvent event) {
    }
}

Now, even if the listener you are using doesn't abide by the container thread rules, you can still use the container features in your handler. I use this to take events off of Maps, Queues and Topics in Hazelcast and it works great.

If you are still bent on doing threads, here is an article that explains a pretty clean way of doing it (http://www.adam-bien.com/roller/abien/entry/conveniently_transactionally_and_legally_starting).

Baldy
  • 2,002
  • 14
  • 14
  • >`I can't think of too many occasions when this is the right thing to do` - this is maybe not the best place to discuss this in length, but in ~10 years of Java EE development I encountered many situations where this is needed. You are right that `@Asynchronous` is a very viable alternative, but it runs via just 1 thread-pool. If you execute an other `@Asynchronous` from the first one, and wait for the results you can deadlock the pool. A producer/consumer pipeline can thus not be implemented safely with it. JSR 236 recognized this need, but IMHO didn't fully deliver yet. – Arjan Tijms Sep 18 '13 at 12:10