Concurrency utilities for Java EE 7

93
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 1 Yoshio Terada Java Evangelist http://yoshio3.com , Twitter : @yoshioterada エエエエエエエエエエエエエエエエエエエエエエエ エエエエ C-4

Transcript of Concurrency utilities for Java EE 7

  • 1.Copyright 2013, Oracle and/or its affiliates. All rights reserved.1Yoshio TeradaJava Evangelisthttp://yoshio3.com,Twitter : @yoshioteradaC-4

2. Copyright 2013, Oracle and/or its affiliates. All rights reserved.2Oracle 3. Copyright 2013, Oracle and/or its affiliates. All rights reserved.3 4. Copyright 2013, Oracle and/or its affiliates. All rights reserved.4Java EE 7 JSR Connector1.6JPA 2.1 JTA 1.2 JMS 2.0Managed Bean 1.0 EJB 3.2CommonAnnotations 1.1Interceptors 1.1 CDI 1.1PortableExtensionsServlet 3.1JSP 2.2 JSF 2.2JAX-RS2.0EL 3.0BeanValidation1.1ConcurrencyUtilities for EEBatchApplication(JSR-352)Java API forJSON(JSR-353)Java API forWebSocket(JSR-356) 5. Copyright 2013, Oracle and/or its affiliates. All rights reserved.5JSR-236 :Concurrency Utilities forJava EE 6. Copyright 2013, Oracle and/or its affiliates. All rights reserved.6 7. Copyright 2013, Oracle and/or its affiliates. All rights reserved.7Java Java SE 8. Copyright 2013, Oracle and/or its affiliates. All rights reserved.819961997JDK1.0ThreadRunnable20042006JDK1.1J2SE1.2J2SE 1.3J2SE 1.4Java SE 5JSR-166Concurrency UtilitiesJava SE 6JSR-166xJava Thread 199820002002 9. Copyright 2013, Oracle and/or its affiliates. All rights reserved.92011Java SE 7JSR-166yFork/JoinJava SE 8JSR-166eConcurrency Utilities2013Java SE Thread 10. Copyright 2013, Oracle and/or its affiliates. All rights reserved.10 Copyright 2013, Oracle and/or its affiliates. All rights reserved.10class MyWebServer{public static void main(String argv[]){ServerSocket socket = new ServerSocket(80);while(true){final Socket conn = socket.accept();Runnable r = new Runnable(){publiv void run(){addConnQueue(conn);//}};new Thread(r).start();}} } Web SE EE 11. Copyright 2013, Oracle and/or its affiliates. All rights reserved.11new Thread(r).start();Thread-1Thread-2Thread-3Thread-n 12. Copyright 2013, Oracle and/or its affiliates. All rights reserved.12Thread-1Thread-2Thread-3Thread-nThread-1Thread-2Thread-3Thread-n -Xss 13. Copyright 2013, Oracle and/or its affiliates. All rights reserved.13Java VM OS 14. Copyright 2013, Oracle and/or its affiliates. All rights reserved.14 15. Copyright 2013, Oracle and/or its affiliates. All rights reserved.15Java Concurrency Utilities !! API 16. Copyright 2013, Oracle and/or its affiliates. All rights reserved.16JSR-166 17. Copyright 2013, Oracle and/or its affiliates. All rights reserved.17 Executors, Thread Pool, Futures : Queues, Blocking Queues, Concurrent HashMap : Semaphores, Barriers, Atomic 18. Copyright 2013, Oracle and/or its affiliates. All rights reserved.18 Copyright 2013, Oracle and/or its affiliates. All rights reserved.18public interface Executor{void execute(Runnable command);}Executor 19. Copyright 2013, Oracle and/or its affiliates. All rights reserved.19 Copyright 2013, Oracle and/or its affiliates. All rights reserved.19public interface ExecutorService extends Executor{void shutdown();List shutdownNow();boolean isShutdown();boolean isTerminated();boolean awaitTermination(long timeout,TimeUnit unit); Future submit(Callable task)}ExecutorService 20. Copyright 2013, Oracle and/or its affiliates. All rights reserved.20 Copyright 2013, Oracle and/or its affiliates. All rights reserved.20class MyWebServer{static int CPU_NUM =Runtime.getRuntime().availableProcessors();ExecutorService pool =Executors.newFixedThreadPool(CPU_NUM);public static void main(String argv[]){ServerSocket socket = new ServerSocket(80);while(true){final Socket conn = socket.accept();Runnable r = new Runnable(){public void run(){;//} };pool.execute(r);}}} Executor Web 21. Copyright 2013, Oracle and/or its affiliates. All rights reserved.21newFixedThreadPoolExecutorService LinkedBlockingQueue (FIFO)ThreadFactoryT1 T2 T3 T4 TnExecutorService pool =Executors.newFixedThreadPool(CPU_NUM);pool.execute(r); 22. Copyright 2013, Oracle and/or its affiliates. All rights reserved.22Concurrency Utilities 23. Copyright 2013, Oracle and/or its affiliates. All rights reserved.23Java EE JMS(MDB)Async ServletAsync EJB 24. Copyright 2013, Oracle and/or its affiliates. All rights reserved.2419982009JPEJ2EE 1.4Java EE 6 Servlet EJBJava EE 199920012003Java EE 52006J2EE 1.2JMSJ2EE 1.3MDB 25. Copyright 2013, Oracle and/or its affiliates. All rights reserved.25JMS & MDBJava EE 7 26. Copyright 2013, Oracle and/or its affiliates. All rights reserved.26JNDI jms/MyConFactory jms/MyQueue 27. Copyright 2013, Oracle and/or its affiliates. All rights reserved.27JNDI JMS jms/MyFactoryjms/MyQueueJNDI JNDI 28. Copyright 2013, Oracle and/or its affiliates. All rights reserved.28 Copyright 2013, Oracle and/or its affiliates. All rights reserved.28@Statelesspublic class MailAddressRegisterEJB {@Resource(mappedName = "java:comp/JMSConFact")ConnectionFactory conn;@Resource(mappedName = "jms/mailRegistQueue")Queue queue;public void registEmailAddress(String address){try(JMSContext context = conn.createContext()){context.createProducer().send(queue,address);}}}JMS 2.0 (Java EE 7) 29. Copyright 2013, Oracle and/or its affiliates. All rights reserved.29 Copyright 2013, Oracle and/or its affiliates. All rights reserved.29@MessageDriven(mappedName = "jms/mailRegistQueue")public class SendMessageMDB implements MessageListener{ public SendMessageMDB(){}@Inject MailSender mailSender;@Overridepublic void onMessage(Message message) {try {TextMessage msg = (TextMessage) message;mailSender.sendMessage(msg.getText());} catch (JMSException jmse) {jmse.printStackTrace();}}} MDB (Java EE 7) 30. Copyright 2013, Oracle and/or its affiliates. All rights reserved.30Servlet 3.0 : Java EE 6 (asyncSupported = true) 31. Copyright 2013, Oracle and/or its affiliates. All rights reserved.31 Copyright 2013, Oracle and/or its affiliates. All rights reserved.31@WebServlet(name = "MailSenderServlet", urlPatterns ={"/MailSenderServlet"}, asyncSupported = true)public class MailSenderServlet extends HttpServlet {protected void processRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {AsyncContext ac = request.startAsync();ac.start(new MailSenderRunnable(ac));Servlet 3.0 : 32. Copyright 2013, Oracle and/or its affiliates. All rights reserved.32EJB 3.1 : Java EE 6 @Asynchronous 33. Copyright 2013, Oracle and/or its affiliates. All rights reserved.33 Copyright 2013, Oracle and/or its affiliates. All rights reserved.33@Statelesspublic class SyncEmailSenderEJB {@InjectMailSender mailsend;public void syncSendMessage(String email){mailsend.sendMessage(email);}@Asynchronouspublic void asyncSendMessage(String email){mailsend.sendMessage(email);}} EJB 3.1 (Java EE 6) 34. Copyright 2013, Oracle and/or its affiliates. All rights reserved.34 Copyright 2013, Oracle and/or its affiliates. All rights reserved.34 35. Copyright 2013, Oracle and/or its affiliates. All rights reserved.35 Copyright 2013, Oracle and/or its affiliates. All rights reserved.35 36. Copyright 2013, Oracle and/or its affiliates. All rights reserved.36Concurrency Utilities forEE 37. Copyright 2013, Oracle and/or its affiliates. All rights reserved.37Application ServersJava SEWeb/EJB EJB JSPServletRunnableCallableJava EE (JAX-RS,JavaMail, CDI )Java EE Thread 38. Copyright 2013, Oracle and/or its affiliates. All rights reserved.38Application ServersJava SEWeb/EJB EJB JSPServletRunnableCallableJava EE ConcurrencyJava EE (JAX-RS,JavaMail, CDI )ManagedExecutor ServiceManagedScheduledExecutorServiceContextServiceManagedThreadFactoryConcurrency Utilities for EE 39. Copyright 2013, Oracle and/or its affiliates. All rights reserved.39 Copyright 2013, Oracle and/or its affiliates. All rights reserved.39 40. Copyright 2013, Oracle and/or its affiliates. All rights reserved.40JSR-236 2003 (BEA, IBM : commonj )J2EE 1.4, Java EE5 JSR-236 : Timer for Application Servers 41. Copyright 2013, Oracle and/or its affiliates. All rights reserved.41JSR-236 2008 6 2006 4 : Version 0.1: Early Draft PreviewJSR-236 : Concurrency Utilities for Java EE2012 Q2 : Java EE 7 !! 42. Copyright 2013, Oracle and/or its affiliates. All rights reserved.42 :https://cu-javaee.java.net/ 43. Copyright 2013, Oracle and/or its affiliates. All rights reserved.43 Copyright 2013, Oracle and/or its affiliates. All rights reserved.43 44. Copyright 2013, Oracle and/or its affiliates. All rights reserved.44 Copyright 2013, Oracle and/or its affiliates. All rights reserved.44 45. Copyright 2013, Oracle and/or its affiliates. All rights reserved.45 Copyright 2013, Oracle and/or its affiliates. All rights reserved.45 46. Copyright 2013, Oracle and/or its affiliates. All rights reserved.46 Copyright 2013, Oracle and/or its affiliates. All rights reserved.46 47. Copyright 2013, Oracle and/or its affiliates. All rights reserved.47 Copyright 2013, Oracle and/or its affiliates. All rights reserved.47 48. Copyright 2013, Oracle and/or its affiliates. All rights reserved.48EE (ManagedExecutorService ) 49. Copyright 2013, Oracle and/or its affiliates. All rights reserved.49 Copyright 2013, Oracle and/or its affiliates. All rights reserved.49 50. Copyright 2013, Oracle and/or its affiliates. All rights reserved.50A implements RunnableB implements Callable EE 51. Copyright 2013, Oracle and/or its affiliates. All rights reserved.51 Copyright 2013, Oracle and/or its affiliates. All rights reserved.51public class MyRunnableTask implements Runnable {@Overridepublic void run() {try {Thread.sleep(10000); //} catch (InterruptedException ex) {logger.log(Level.SEVERE, null, ex);}}}Runnable 52. Copyright 2013, Oracle and/or its affiliates. All rights reserved.52 Copyright 2013, Oracle and/or its affiliates. All rights reserved.52Callable call() public class MyCallableTask implementsCallable {@Overridepublic String call() throws Exception {return Hello World;}} 53. Copyright 2013, Oracle and/or its affiliates. All rights reserved.53 54. Copyright 2013, Oracle and/or its affiliates. All rights reserved.54 Copyright 2013, Oracle and/or its affiliates. All rights reserved.54@Statelesspublic class MyManagedExecutorService {@Resource(name ="concurrent/DefaultManagedExecutorService")ManagedExecutorService managedExecsvc;public void execExecutorService() {MyRunnableTask task = new MyRunnableTask();managedExecsvc.submit(task);MyCallableTask singleTask =new MyCallableTask("Foo Bar");Future singleFuture =managedExecsvc.submit(singleTask);} EJB 55. Copyright 2013, Oracle and/or its affiliates. All rights reserved.55(ManagedScheduledExecutorService) 56. Copyright 2013, Oracle and/or its affiliates. All rights reserved.56 Copyright 2013, Oracle and/or its affiliates. All rights reserved.56 57. Copyright 2013, Oracle and/or its affiliates. All rights reserved.57A implements RunnableB implements Callable EE 58. Copyright 2013, Oracle and/or its affiliates. All rights reserved.58 59. Copyright 2013, Oracle and/or its affiliates. All rights reserved.59 Copyright 2013, Oracle and/or its affiliates. All rights reserved.59@Statelesspublic class MyManagedScheduledExecutorService{@Resource(name = "concurrent/DefaultManagedScheduledExecutorService")ManagedScheduledExecutorService managedScheduledExecsvc;public void execScheduledExecutorService() {MyRunnableTask task = new MyRunnableTask();managedScheduledExecsvc.schedule(task, 60L, TimeUnit.SECONDS);} EJB 60. Copyright 2013, Oracle and/or its affiliates. All rights reserved.60 Copyright 2013, Oracle and/or its affiliates. All rights reserved.60@Statelesspublic class MyManagedScheduledExecutorService{@Resource(name = "concurrent/DefaultManagedScheduledExecutorService")ManagedScheduledExecutorService managedScheduledExecsvc;public void execScheduledExecutorService() {MyRunnableTask task = new MyRunnableTask();managedScheduledExecsvc.schedule(task, new MyTrigger(new Date(), 10, 1000) } EJB 61. Copyright 2013, Oracle and/or its affiliates. All rights reserved.61 Copyright 2013, Oracle and/or its affiliates. All rights reserved.61import javax.enterprise.concurrent.Trigger;public class MyTrigger implements Trigger {@Overridepublic Date getNextRunTime(LastExecution le,Date date){}@Overridepublic boolean skipRun(LastExecution le,Date date) {}} 62. Copyright 2013, Oracle and/or its affiliates. All rights reserved.62 63. Copyright 2013, Oracle and/or its affiliates. All rights reserved.63taskSubmitted Submitted taskStartingtaskAborted StartedtaskDoneDonesubmit()submit 64. Copyright 2013, Oracle and/or its affiliates. All rights reserved.64 Copyright 2013, Oracle and/or its affiliates. All rights reserved.64public class MyManagedTaskListener implementsManagedTaskListener {public void taskSubmitted(Future future,ManagedExecutorService mes, Object o) {}public void taskStarting(Future future,ManagedExecutorService mes, Object o) {}public void taskAborted(Future future,ManagedExecutorService mes, Object o, Throwable thrwbl){}public void taskDone(Future future,ManagedExecutorService mes, Object o, Throwable thrwbl){}} 65. Copyright 2013, Oracle and/or its affiliates. All rights reserved.65 Copyright 2013, Oracle and/or its affiliates. All rights reserved.65@Resource(name = "concurrent/MyManagedExecutorService")ManagedExecutorService manageExecsvc;public void invokeMyTaskListener() {MyRunnableTask task = new MyRunnableTask();MyManagedTaskListener listener =new MyManagedTaskListener();Runnable taskWithListener =ManagedExecutors.managedTask(task, listener);manageExecsvc.execute(taskWithListener);} 66. Copyright 2013, Oracle and/or its affiliates. All rights reserved.66ContextSerivce 67. Copyright 2013, Oracle and/or its affiliates. All rights reserved.67 Copyright 2013, Oracle and/or its affiliates. All rights reserved.67 68. Copyright 2013, Oracle and/or its affiliates. All rights reserved.68 Copyright 2013, Oracle and/or its affiliates. All rights reserved.68 69. Copyright 2013, Oracle and/or its affiliates. All rights reserved.69 Copyright 2013, Oracle and/or its affiliates. All rights reserved.69 70. Copyright 2013, Oracle and/or its affiliates. All rights reserved.70 Copyright 2013, Oracle and/or its affiliates. All rights reserved.70 71. Copyright 2013, Oracle and/or its affiliates. All rights reserved.71Dynamic Proxy public class MyRunnable implements Runnable{@Overridepublic void run() {System.out.println( ");}} 72. Copyright 2013, Oracle and/or its affiliates. All rights reserved.72 InvocationHandler public class MyInvocationHandler implements InvocationHandler {private Object underlying;public MyInvocationHandler(Object underlying) {this.underlying = underlying;}@Overridepublic Object invoke(Object proxy, Method method,Object[] args) throwsThrowable {System.out.println(" ");Object ret = method.invoke(underlying, args);System.out.println(" ");return ret;}} 73. Copyright 2013, Oracle and/or its affiliates. All rights reserved.73Dynamic Proxy public class MyDynamicProxy {public static void main(String argv[]){MyRunnable task = new MyRunnable();InvocationHandler handler = newMyInvocationHandler(task);Runnable proxy = (Runnable)Proxy.newProxyInstance(MyRunnable.class.getClassLoader(),newClass[]{Runnable.class}, handler);ExecutorService exec = Executors.newSingleThreadExecutor();exec.submit(proxy);}} 74. Copyright 2013, Oracle and/or its affiliates. All rights reserved.74Dynamic Proxy ExecutorService exec =Executors.newSingleThreadExecutor();exec.submit(proxy); 75. Copyright 2013, Oracle and/or its affiliates. All rights reserved.75 Copyright 2013, Oracle and/or its affiliates. All rights reserved.75 76. Copyright 2013, Oracle and/or its affiliates. All rights reserved.761 77. Copyright 2013, Oracle and/or its affiliates. All rights reserved.77 Copyright 2013, Oracle and/or its affiliates. All rights reserved.77@Statelesspublic class ContextServiceManager {@Resource(name = "concurrent/DefaultContextService")ContextService ctxSvc;public void execSimpleContextService() {ExecutorService singleThreadExecutor =Executors.newSingleThreadExecutor(threadFactory);MyRunnableTask task = new MyRunnableTask();Runnable proxiedTask =ctxSvc.createContextualProxy(task,Runnable.class);singleThreadExecutor.submit(proxiedTask);}}2 78. Copyright 2013, Oracle and/or its affiliates. All rights reserved.78 Copyright 2013, Oracle and/or its affiliates. All rights reserved.78Dynamic Proxy 79. Copyright 2013, Oracle and/or its affiliates. All rights reserved.79ThreadFactory 80. Copyright 2013, Oracle and/or its affiliates. All rights reserved.80 Copyright 2013, Oracle and/or its affiliates. All rights reserved.80 81. Copyright 2013, Oracle and/or its affiliates. All rights reserved.81 Copyright 2013, Oracle and/or its affiliates. All rights reserved.81 82. Copyright 2013, Oracle and/or its affiliates. All rights reserved.821 83. Copyright 2013, Oracle and/or its affiliates. All rights reserved.83 Copyright 2013, Oracle and/or its affiliates. All rights reserved.83@Resource(name = "concurrent/DefaultManagedThreadFactory")ManagedThreadFactory threadFactory;public void execThreadFactory() {MyRunnableTask task = new MyRunnableTask();Thread taskThread =threadFactory.newThread(task);taskThread.start();}ThreadFactory 2 84. Copyright 2013, Oracle and/or its affiliates. All rights reserved.84 Copyright 2013, Oracle and/or its affiliates. All rights reserved.84ExecutorService threadPoolExecutor =Executors.newFixedThreadPool(4,threadFac);threadPoolExecutor = new ThreadPoolExecutor(4, 4,0L, TimeUnit.MILLISECONDS,newLinkedBlockingQueue(),threadFac);ThreadPoolExecutor 85. Copyright 2013, Oracle and/or its affiliates. All rights reserved.85 Copyright 2013, Oracle and/or its affiliates. All rights reserved.85@Resource(name = "concurrent/DefaultManagedThreadFactory")ManagedThreadFactory threadFactory;public void execThreadFactory() {MyRunnableTask task = new MyRunnableTask();ExecutorService exec =new ThreadPoolExecutor(4, 4,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue(),threadFactory);exec.submit(task);} 86. Copyright 2013, Oracle and/or its affiliates. All rights reserved.86 87. Copyright 2013, Oracle and/or its affiliates. All rights reserved.87 Copyright 2013, Oracle and/or its affiliates. All rights reserved.87 88. Copyright 2013, Oracle and/or its affiliates. All rights reserved.88 Copyright 2013, Oracle and/or its affiliates. All rights reserved.88 89. Copyright 2013, Oracle and/or its affiliates. All rights reserved.89 Copyright 2013, Oracle and/or its affiliates. All rights reserved.89 90. Copyright 2013, Oracle and/or its affiliates. All rights reserved.90 Copyright 2013, Oracle and/or its affiliates. All rights reserved.90 91. Copyright 2013, Oracle and/or its affiliates. All rights reserved.91 Copyright 2013, Oracle and/or its affiliates. All rights reserved.91 92. Copyright 2013, Oracle and/or its affiliates. All rights reserved.92 93. Copyright 2013, Oracle and/or its affiliates. All rights reserved.93