Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE specification and multi threading

I am writing a Java EE application using Struts and Spring. In one of the operations there is heavy database processing, and hence performance issues. What I want to know is can I use multithreading here? I think the Java EE specification does not allow custom threads to be created apart from those created by Server (I use Weblogic). Please guide me through this.

like image 737
Sid Avatar asked Jul 09 '10 11:07

Sid


People also ask

What is Java EE specification?

Java Platform Enterprise Edition (Java EE) Java EE specification also features some specifications unique to enterprise computing. These include Enterprise JavaBeans (EJB), servlets, portlets, Java Server Pages (JSP), Java Server Faces (JSF) and several Web service technologies.

Which elements is part of the Java EE specification?

The Java EE specification defines the following Java EE components: Application clients and applets are components that run on the client. Java Servlet, JavaServer Faces, and JavaServer PagesTM (JSPTM) technology components are web components that run on the server.

What is Java Multi threading?

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process.

Does Java support multi threading?

Java supports multithreading at the language (syntactic) level and via support from its run-time system and thread objects. At the language level, methods within a class that are declared synchronized do not run concurrently.


2 Answers

The recommended way to create threads in a Java EE environment, is with the Concurrency Utils API, which is part of the EE7 specification.

By using this API your new thread will be created, and managed by the container, guaranteeing that all EE services are available to your thread (eg security, transactions).

The examples below are taken from my own site here and here

Using a ManagedExecutorService

To create a new thread using a ManagedExecutorService, first create a task object that implements Callable. Within the call() method we will define the work that we want carried out in a separate thread.

public class ReportTask implements Callable<Report> {      Logger logger = Logger.getLogger(getClass().getSimpleName());      public Report call() {         try {             Thread.sleep(3000);         catch (InterruptedException e) {             logger.log(Level.SEVERE, "Thread interrupted", e);         }         return new Report();     } } 

Then we need to invoke the task by passing it though to the submit() method of the ManagedExecutorService.

@Stateless public class ReportBean {      @Resource     private ManagedExecutorService executorService;      public void runReports() {         ReportTask reportTask = new ReportTask();         Future<Report> future = executorService.submit(reportTask);     } } 

Using a ManagedThreadFactory

First create a Runnable task which will define what work is to be done in the background.

public class ReportTask implements Runnable {      Logger logger = Logger.getLogger(getClass().getSimpleName());      public void run() {         try {             //do your background task             Thread.sleep(10000);         } catch (InterruptedException e) {             logger.log(Level.SEVERE, "Thread interrupted", e);         }     } } 

To get a container managed thread, we simply ask the ManagedThreadFactory for a new thread, and pass it our Runnable instance. To start the thread we call start().

@Stateless public class ReportBean {      @Resource     private ManagedThreadFactory threadFactory;      public void runReports() {         ReportTask reportTask = new ReportTask();         Thread thread = threadFactory.newThread(reportTask);         thread.start();     } } 
like image 77
Chris Ritchie Avatar answered Sep 20 '22 03:09

Chris Ritchie


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.

like image 39
ewernli Avatar answered Sep 21 '22 03:09

ewernli