Java EE specification and multi threading
This question pops up once in a while.
As per the spec it's not authorized. The best page to look at is this one: Q/A: J2EE Restrictions
That said, there are ways to spawn threads, especiall in Weblogic with the WorkManager
.
See these questions:
- How can an EJB parallelize a long, CPU intensive process?
- Why spawning threads in J2EE container is discouraged?
- J2EE programmers do not write to files
The fact that the first one targets EJB shouldn't matter that much, and the last one about access to file system is about general restrictions.
Hope it helps.
How to create threads in Java EE environment?
You have some options:
- Asynchronous beans. These are vendor-specific, as you mention.
- commonj is just barely not vendor-specific. As far as I know, it was only implemented by IBM WebSphere Application Server and BEA WebLogic. The API was effectively superseded by Concurrency Utilities for Java EE, which is really the best choice.
- EJB
@Asynchronous
methods. Requires using EJBs (unwanted complexity for some). - EJB timers. Requires using EJBs, requires serializable data.
- JMS. Probably requires using MDBs to receive the message, requires serializable data.
- Actually create threads. The EE specs do not recommend this, but as long as you don't attempt to use EE constructs (
lookup("java:...")
, JPA, UserTransaction, etc.), then you should be fine.
Why is spawning threads in Java EE container discouraged?
It is discouraged because all resources within the environment are meant to be managed, and potentially monitored, by the server. Also, much of the context in which a thread is being used is typically attached to the thread of execution itself. If you simply start your own thread (which I believe some servers will not even allow), it cannot access other resources. What this means, is that you cannot get an InitialContext and do JNDI lookups to access other system resources such as JMS Connection Factories and Datasources.
There are ways to do this "correctly", but it is dependent on the platform being used.
The commonj WorkManager is common for WebSphere and WebLogic as well as others
More info here
And here
Also somewhat duplicates this one from this morning
UPDATE: Please note that this question and answer relate to the state of Java EE in 2009, things have improved since then!
Concurrent processing in JAVA EE
Whatever solution you go with, you will eventually need to cope with a burst of traffic. The JMS/MDB the burst is controlled by the queue effectively. Also a point to consider is that the queue can be made persistence, so it will survive a server restart. Also a queue can be distributed across many servers, giving you horizontal scalability.
The thread approach is of course quicker to develop, test and deploy. However, I would consider using a BlockingQueue so that your threads do not run amock.
Is it a good way to use Timer in Java EE
Finally, I have got the answer and I am happy with JEE7 Multithreading tools:
@Stateless
public class ReportBean {
@Resource
private ManagedThreadFactory threadFactory;
public void runReports() {
Thread thread = threadFactory.newThread(new Runnable());
thread.start();
}
I've used this bean in my work...
The Java EE7 offer a new way to make the using of JAVA SE Managed by the Web Container and not by the JVM.
Related Topics
How to Get All Table Names from a Database
How Is Length Implemented in Java Arrays
Should Getters and Setters Be Synchronized
Getting Database Connection in Pure JPA Setup
Converting Decimal to Binary Java
Parallel Streams, Collectors and Thread Safety
Using Javamail to Connect to Gmail Smtp Server Ignores Specified Port and Tries to Use 25
Must Spring Component Classes Be Thread-Safe
Is There a Newline Constant Defined in Java Like Environment.Newline in C#
JPA Query Selecting Only Specific Columns Without Using Criteria Query
Can't Cast to to Unspecific Nested Type with Generics
How to Set Up Java Logging Using a Properties File? (Java.Util.Logging)
How to Automate Drag & Drop Functionality Using Selenium Webdriver Java