Pergunta

I need a custom threda on my weblogic server; I cannot use TimerEJB or Delayed MDB since I have to use a 3d library.

I know that custom threads on application server are discouraged; here a related post (4 years old): Why is spawning threads in Java EE container discouraged?

Is it still discouraged? Can I use ExecutorService or Quartz? Or I have to consider only commonj and worker manager?

Foi útil?

Solução

On Weblogic 11g (EJB3.0) is dicouraged to create "own thread executor" (see also Java EE specification and multi threading) you should use:

  • TimerService

example:

MyEjb {
   @Stateless
   public class TimerBean implements TimerRemote {

    @Resource
    TimerService service;

    @Override
    public void startTimer() {
        Timer timer = service.createTimer(1000,  1000, null);
        System.out.println("Timers set");
    }

    @Timeout
    public void handleTimeout(Timer timer) {
        System.out.println("Handle timeout event here...");
    }
}

**If you cannot use Deleyed MDB or TimerService you need to work with Work Manager **.

Weblogic and Websphere are CommonJ (JSR 237 Timer & WorkManager) compliant; in other words they use a common interface to work with work manager; in weblogic, work managers regulate the life cycle of threads (Servlet, EJB, MDB,..., custom thread).

IMHO do not use ExecutorService since is not under the control of weblogic: if you stop the application (not the server) the threads under weblogic control will stop, the other threads should not terminate; I experienced this problem.

Work Manager

The WorkManager feature in WebLogic Server is dynamic in nature, based on the number of incoming requests the thread pool size automatically re-sizes to maximize the throughput. To work with WorkManager you can access through commonj interface or MBean.

example:

InitialContext ic = new InitialContext();
commonj.work.WorkManager wm = (WorkManager);
ic.lookup(‘java:comp/env/wm/default’); // default work manager

Spring

For instance spring has an easy way to attach its own TaskExecutor to comonj worker manager:

<bean id="workManager" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor">
        <property name="workManagerName" value="java:comp/env/wm/default" />
    </bean>

Quartz

Quartz works on application servers and with spring they can use work manager

JAVA EE 7

JEE7 introduces Batch (JSR 352). The Batch Applications for the Java Platform API (Batch) provides a programming model for batch applications and a runtime for scheduling and executing jobs.

Executor Service

From java specifications:

Also new in the Java EE 7 platform is the Batch API, which provides a programming model for batch applications and a runtime for scheduling and executing jobs, and the Concurrency Utilities API, which provides asynchronous capabilities by means of managed executor service, managed scheduled executor.

In JEE7 Executor Service has been extended with ManagedExecutorService and ManagedThreadFactory.

example:

   javax.naming.InitialContext ctx = new InitialContext();
   ManagedExecutorService mes = (ManagedExecutorService)
       ctx.lookup("java:comp/env/concurrent/ThreadPool");

   // Create a set of tasks to perform the account retrieval.
   ArrayList<Callable<MyObject>> retrieverTasks = new ArrayList<Callable<MyObject>>();
   retrieverTasks.add(new MyCallable());

Concluding: Weblogic

See http://www.oracle.com/technetwork/java/restrictions-142267.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top