Adam Bien's Weblog

Wednesday Feb 12, 2014

JavaEE 7 - Injecting A ManagedExecutorService

Legacy resources can be easily integrated with JavaEE 7 in a robust way using the JSR 236: Concurrency Utilities for Java EE 7

Thanks for watching!

See also other screencasts at: http://tv.adam-bien.com or subscribe to http://www.youtube.com/user/bienadam.

See you at Java EE 7 Workshops at MUC Airport or on demand and in a location very near you: airhacks.io!


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Comments:

What was that ? How can I unseen my 3 minutes ?

Posted by com.ibm.websphere.threadmonitor.dump.java on February 12, 2014 at 12:05 PM CET #

Hi com.ibm.websphere.threadmonitor.dump.java,

this was just an answer to multiple Java EE / concurrency questions I got. Instead of writing multiple emails, I just recorded a screencast.

a follow-up is coming,

thanks for watching! Btw. You not only spent 3 minutes watching this, but also about 1 min commenting. So in total you spent about 4 mins...

cheers,

adam

Posted by Adam Bien on February 12, 2014 at 02:13 PM CET #

Hi,

In jee 6 I used: "ExecutorService executor = Executors.newFixedThreadPool(THREADS);". Please explain me what differs ManagedExecutorService from "old standard" ExecutorService, and why should I use @Resource injection over static factory method ?

Posted by Marek on February 14, 2014 at 11:26 AM CET #

@Marek,

a ManagedExecutorService provides you with the following additional features:

1.Monitoring: an application server will be able to report back the current amount of threads etc.
2. Management / Configuration: with MES it is trivial to change the thread pool configuration any settings without recompiling the application.
3. Context: MES provides tasks with additional context like e.g. security.
4. Robustness: starting application managed threads can quickly overload the application server. In the past I got some contracts to solve such problems :-)

enjoy Java!,

--adam

Posted by Adam Bien on February 14, 2014 at 02:07 PM CET #

Hi Adam,

Sorry this may be a little off topic.

My question is:
I have several EJB timers which perform some background tasks (like a robot/agent). Where should I put these classes under the ECB (Entity-Control-Boundary) scope?

Thanks!

-david

Posted by David on February 16, 2014 at 08:53 AM CET #

Hi Adam!

Could you please elaborate on the purpose of this video? You say that it's the only legal way to run threads in Java EE 6, so I have 2 questions:

1) Why are you running this on Java EE 7? As far as I understand the preferred way to run multithreaded apps in Java EE 7 is with ManagedExecutorService. Or am I wrong?

2) Why do we need Runnable at all in this example? The EJB doesn't start a new Thread, it just executes a run() method. So, instead of passing Runnable, we could as well pass some Command object.

Posted by Anton on February 16, 2014 at 08:29 PM CET #

@Anton,

I think you are referring to another video ("EJBs as Executors": http://www.youtube.com/watch?v=3jGhdBU2MoA).

1.) I ran this video on JavaEE 7, because all my projects are JavaEE 7 projects and I was too lazy to downgrade the server to JavaEE 6

2.) You don't need Runnable at all. However: with the Runnable-trick you can pass any legacy runnable-implementation and it is getting to be executed in a managed way. It is actually a Command pattern.

cheers && thanks for your comment,

adam

Posted by Adam Bien on February 17, 2014 at 11:36 AM CET #

@David,

I usually put timers to the control package. However: if the timers coordinate multiple controls and act more like a facade, then I would put them to the boundary package.

cheers && thanks for asking about ECB,

-adam

Posted by Adam Bien on February 17, 2014 at 11:37 AM CET #

Hi Adam,

are there some restriction in submitting Runnable to a ManagedExecutorService in one @PostConstruct method ?

See following Singleton SB:

@Singleton
@Startup
public class SimpleStartupMes {
@Resource
ManagedExecutorService mes;

@PostConstruct
void startup() {
System.out.println("Starting UP...");
mes.submit(new Runnable() {
@Override
public void run() {
System.out.println("HELLO !");
}
});
System.out.println("Started !");
}
}

When I deploy the EAR to Glassfish-4.0 the anonymous Runnable will not be started,
the String "HELLO !" does not appear.

This is the output:

INFO: Starting UP...
INFO: Started !
INFO: ExecutorServiceTest-ear-1.0.0-SNAPSHOT was successfully deployed in 1.509 milliseconds.

But, when I restart the container then it works.

This is the output after Glassfish restart:

INFO: Starting UP...
INFO: Started !
INFO: HELLO !

What is wrong here ?

Thanks !

Posted by Marco on May 14, 2014 at 02:34 PM CEST #

@marco there is an issue related to your comment that you might be interested in following or voting for in the Glassfish JIRA: https://java.net/jira/browse/GLASSFISH-21087

Posted by Noah White on July 15, 2014 at 05:29 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
...the last 150 posts
...the last 10 comments
Links
License