Execution framework java util concurrent A task that

  • Slides: 19
Download presentation

Execution framework (java. util. concurrent) A task that returns a result and may throw

Execution framework (java. util. concurrent) A task that returns a result and may throw an exception public interface Callable<V> { V call() throws Exception; } public interface Runnable { Future represents the result of an asynchronous computation public interface Future<V> { boolean cancel(boolean may. Interrupt. If. Running); boolean is. Cancelled(); boolean is. Done(); V get() throws Interrupted. Exception, Execution. Exception; V get(long timeout, Time. Unit unit) throws Interrupted. Exception, Execution. Exception } public interface Scheduled. Future<V> extends Delayed, Future<V> { }

Execution framework (java. util. concurrent) Scheduled. Executor. Service : Executors. new. Scheduled. Thread. Pool(int

Execution framework (java. util. concurrent) Scheduled. Executor. Service : Executors. new. Scheduled. Thread. Pool(int core. Pool. Size) new. Single. Thread. Scheduled. Executor() Executor. Service: Executors. new. Cached. Thread. Pool() Executors. new. Single. Thread. Executors. new. Fixed. Thread. Pool(int n. Threads) Blocking. Queue Implementation: Linked. Blocking. Queue public interface Completion. Service<V> { Future<V> submit(Callable<V> task); Future<V> take() throws Interrupted. Exception; Future<V> poll(); … } Implementation: Executor. Completion. Service(Executor executor)

Glassfish асинхронный клиент веб-сервиса wsimport -b async. xml <bindings node="wsdl: definitions"> <enable. Async. Mapping>true</enable.

Glassfish асинхронный клиент веб-сервиса wsimport -b async. xml <bindings node="wsdl: definitions"> <enable. Async. Mapping>true</enable. Async. Mapping> </bindings> @Web. Method @Request. Wrapper(local. Name=. . @Response. Wrapper(local. Name =“Result”, . . public Response<Result> execute(); public Future<? > execute( @Web. Param(name = "async. Handler", target. Namespace = "") Async. Handler<Result> async. Handler);

Glassfish асинхронный клиент веб-сервиса Пример public interface Response<T> extends Future<T> { Map<String, Object> get.

Glassfish асинхронный клиент веб-сервиса Пример public interface Response<T> extends Future<T> { Map<String, Object> get. Context(); } public interface Async. Handler<T> { void handle. Response(Response<T> res); } final Async. Handler<Result> handler = new Async. Handler<Result>() { @Override public void handle. Response(Response<Result> res) { res. get() …. }

Glassfish асинхронный клиент веб-сервиса @Oneway Пример <operation name= « execute"> <input message="tns: Result"/> </operation>

Glassfish асинхронный клиент веб-сервиса @Oneway Пример <operation name= « execute"> <input message="tns: Result"/> </operation> @Oneway @Web. Method public void execute() no exceptions

Glassfish v 3 асинхронный клиент EJB @Remote public interface IAsync. EJB { @Asynchronous public

Glassfish v 3 асинхронный клиент EJB @Remote public interface IAsync. EJB { @Asynchronous public Future<Result> execute() throws Exception; } public class Async. EJB implements IAsync. EJB { @Asynchronous public Future<Result> execute() throws Exception{. . . return new Async. Result<Result>(res); } }

Glassfish низкоуровневая реализация веб-сервиса @Web. Service public class Async. Web. Service implements Async. Provider<Source>

Glassfish низкоуровневая реализация веб-сервиса @Web. Service public class Async. Web. Service implements Async. Provider<Source> { public void invoke(final Source request, final Async. Provider. Callback<Source> callback, final Web. Service. Context ctx) { Executor. submit(new Callable<Void>() { @Override public Void call() throws Exception { try{ …. // parse request, form result callback. send(result); return null; } catch (Exception e){ callback. send. Error(e);

Glassfish v 3 асинхронные сервлеты @Web. Servlet(url. Patterns = {"/async"}, async. Supported = true)

Glassfish v 3 асинхронные сервлеты @Web. Servlet(url. Patterns = {"/async"}, async. Supported = true) public class Async. Servlet extends Http. Servlet { private static final Scheduled. Executor. Service executor = Executors. new. Scheduled. Thread. Pool(1); @Override protected void do. Get(Http. Servlet. Request req, Http. Servlet. Response res) { final Async. Context ac = req. start. Async(); Executor. submit(new Callable<Void>() { @Override public Void call() throws Exception { Http. Servlet. Response res = (Http. Servlet. Response) ac. get. Response(); …. ac. complete(); return null; }