java ee concurrency utilities

18
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 1

Upload: reza-rahman

Post on 15-Jan-2015

10.287 views

Category:

Technology


2 download

DESCRIPTION

This fast-faced, code-centric lightning talk covers the new Java EE Concurrency (aka Concurrency Utilities for Java EE) API. Java EE Concurrency is a long-awaited standard for enabling Java SE util.concurrent style low-level concurrency capabilities in a safe, secure, scalable and reliable way in managed Java EE environments. The API provides managed versions of executor services, scheduled executors, thread factories, tasks and much, much more that you can use in a portable fashion across any container without risking container integrity.

TRANSCRIPT

Page 1: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.1

Page 2: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.2

Concurrency Utilities for the Java EE PlatformReza RahmanGlassFish/Java EE [email protected]

Page 3: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.3

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 4: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.4

Concurrency Utilities for the Java EE Platform

Provides low-level asynchronous processing capabilities to Java EE application components in a safe, reliable, consistent manner

Extension of Java SE Concurrency Utilities APIs

Page 5: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.5

Primary Interfaces

Extends existing Java SE concurrency utilities– ManagedExecutorService

– ManagedScheduledExecutorService

– ManagedThreadFactory

New interface– ContextService

Lookup via JNDI or @Resource Default objects

– java:comp/DefaultManagedExecutorService etc...

Page 6: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.6

ManagedExecutorService

For running tasks asynchronously on threads provided by Java EE container

Container context captured from submitting thread to be applied on execution thread

Not transactional – tasks should use their own transactions

Overview

Page 7: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.7

ManagedExecutorService

Extends from java.util.concurrent.ExecutorService No additions Lifecycle APIs disabled – throws IllegalStateException

– awaitTermination, isTerminated, isShutdown, shutdown, shutdownNow

API

Page 8: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.8

ManagedExecutorServiceAPI

void execute(Runnable command);

<T> Future<T> submit(Callable<T> task);

Future<?> submit(Runnable task);

<T> Future<T> submit(Runnable task, T result);

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks);

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit);

<T> T invokeAny(Collection<? extends Callable<T>> tasks)

<T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit);

Page 9: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.9

ManagedExecutorServiceExample@Resource(name=“concurrent/myExecutor”)ManagedExecutorService mes;

void someMethod() { Callable<Integer> c = new Callable<>() { Integer call() { // Interact with a database...return answer. }

// Submit the task and do something else. Future result = mes.submit(c); ... // Get the result when ready... int theValue = result.get(); ...

Page 10: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.10

ManagedScheduledExecutorService

For scheduling tasks to run after a given delay, periodically, or at some custom schedule

Extends from ManagedExecutorService and java.util.concurrent.ScheduledExecutorService

Overview

Page 11: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.11

ManagedScheduledExecutorServiceAPI

<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit);

ScheduledFuture<V> schedule(Runnable command, long delay,

TimeUnit unit);

ScheduledFuture<?> scheduleAtFixedFate(Runnable command,

long initialDelay, long period, TimeUnit unit);

ScheduledFuture<?> scheduledWithFixedDelay(Runnable command,

long initialDelay, long delay, TimeUnit unit);

ScheduledFuture<?> schedule(Runnable command, Trigger trigger);

<V> ScheduledFuture<V> schedule(Callable<V> callable, Trigger trigger);

Page 12: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.12

ManagedScheduledExecutorServiceTrigger

interface Trigger {

// Return true if you want to skip the

// currently-scheduled execution.

boolean skipRun(LastExecution lastExecutionInfo,

Date scheduledRunTime);

// Retrieves the time in which to run the task

// next. Invoked during submit time and after

// each task has completed.

Date getNextRunTime(LastExecution lastExecutionInfo,

Date taskScheduledTime);

}

Page 13: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.13

Managed[Scheduled]ExecutorServiceManagedTaskListener

Listeners can be registered with the task when submitted to Managed[Scheduled]ExecutorService

API– taskSubmitted – The task was submitted to the executor

– taskAborted – The task was unable to start or was cancelled

– taskStarting – The task is about to start

– taskDone – The task has completed (successfully, exception, cancelled, aborted, or rejected)

The listener method runs in unspecified context, but can be configured to run in the same container context as the task

Page 14: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.14

Managed[Scheduled]ExecutorServiceManagedTask

Any task submitted to an ManagedExecutorService or ManagedScheduledExecutorService can optionally implement ManagedTask

API– Map<String, String> getExecutionProperties()

– ManagedTaskListener getManagedTaskListener()

Execution properties– CONTEXTUAL_CALLBACK_HINT

– IDENTITY_NAME

– LONGRUNNING_HINT

Page 15: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.15

ManagedTaskRegistering ManagedTaskListener

// Runnable implements ManagedTaskpublic class TaskWithListener implements Runnable, ManagedTask { ... public ManagedTaskListener getManagedTaskListener() { return aManagedTaskListener; }}// Or use ManagedExecutors utility method to associate// a ManagedTaskListener to a taskRunnable aTask;ManagedTaskListener myTaskListner;Runnable taskWithListener = ManagedExecutors.managedTask(aTask, myTaskListener);// Submit taskWithListener to a ManagedExecutorService

Page 16: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.16

Where to Find Out More

Read the draft spec and API docs at http://concurrency-ee-spec.java.net

GlassFish 4.0 – http://glassfish.java.net/

– http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/

Reference Implementation– http://java.net/projects/cu-javaee

Slide Deck– http://www.slideshare.net/reza_rahman

Page 17: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.17

Graphic Section Divider

Page 18: Java EE Concurrency Utilities

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.18