Java EE 7 Detail Okinawa

51
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 1 Yoshio Terada Java Evangelist http://yoshio3.com , Twitter : @yoshioterada Java EE 7 リリリ

description

 

Transcript of Java EE 7 Detail Okinawa

Page 1: Java EE 7 Detail Okinawa

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

Yoshio TeradaJava Evangelisthttp://yoshio3.com, Twitter : @yoshioterada

Java EE 7 リリース

Page 2: Java EE 7 Detail Okinawa

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

以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはできません。以下の事項は、マテリアルやコード、機能を提供することをコミットメント(確約)するものではないため、購買決定を行う際の判断材料になさらないで下さい。オラクル製品に関して記載されている機能の開発、リリースおよび時期については、弊社の裁量により決定されます。

Oracle は、米国オラクルコーポレーション及びその子会社、関連会社の米国及びその他の国における登録商標です。文中の社名、商品名等は各社の商標または登録商標である場合があります。

Page 3: Java EE 7 Detail Okinawa

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

Make the Future Java(Java の未来を創造 )

Page 4: Java EE 7 Detail Okinawa

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

Java EE 7– HTML 5 とシンプル化にフォーカス

Java EE 8– クラウド対応とモジュール化対応への

継続的な投資

Java EE の今後Java EE

Hasan Rizvi

Page 5: Java EE 7 Detail Okinawa

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

Java EE 7 へ含まれる予定の JSR 一覧

Connector1.6

JPA 2.1 JTA 1.2 JMS 2.0

Managed Bean 1.0 EJB 3.2

Common Annotations

1.1

Interceptors 1.1

CDI 1.1

PortableExtensio

ns Servlet 3.1

JSP 2.2JSF 2.2

JAX-RS 2.0

EL 3.0

Bean

Valid

atio

n1

.1

Concurrency Utilities for

EE(JSR-236)

Batch Application(JSR-352)

Java API forJSON

(JSR-353)

Java API forWebSocket(JSR-356)

新規追加 大幅な更新 通常の更新

Page 6: Java EE 7 Detail Okinawa

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

Top Ten Features in Java EE 7

1. WebSocket client/server endpoints2. Batch Applications3. JSON Processing4. Concurrency Utilities5. Simplified JMS API6. @Transactional and @TransactionScoped7. JAX-RS Client API8. Default Resources9. More annotated POJOs10. Faces Flow

Page 7: Java EE 7 Detail Okinawa

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

JSR 356: JavaTM API for WebSocket

Page 8: Java EE 7 Detail Okinawa

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

WebSocket の背景 – Server Push

Polling

Long Polling

Comet/Ajax

複雑・非効率・無駄

Page 9: Java EE 7 Detail Okinawa

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

WebSocket とは

TCP ベース

双方向・全二重通信

RFC 6455JavaScript API by W3C

Page 10: Java EE 7 Detail Okinawa

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

ブラウザの対応状況http://caniuse.com/websockets

現バージョンの対応状況

Page 11: Java EE 7 Detail Okinawa

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

WebSocket の基本概念

コネクションの確立

双方向のメッセージ送信

コネクションの切断

Page 12: Java EE 7 Detail Okinawa

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

コネクションの確立

HandShake リクエスト

HandShake レスポンス

クライアント サーバ

Page 13: Java EE 7 Detail Okinawa

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

HandShake リクエスト (RFC 6455 で定義 )

サーバクライアント

GET /mychat HTTP/1.1Host: server.example.comUpgrade: websocketConnection: UpgradeSec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==Sec-WebSocket-Protocol: megachat, chatSec-WebSocket-Extensions : compress, muxSec-WebSocket-Version: 13Origin: http://example.com

Page 14: Java EE 7 Detail Okinawa

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

HandShake レスポンス (RFC 6455 で定義 )

サーバクライアント

HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=Sec-WebSocket-Protocol: chatSec-WebSocket-Extensions: compress, mux

Page 15: Java EE 7 Detail Okinawa

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

コネクションの確立

クライアント サーバ

Page 16: Java EE 7 Detail Okinawa

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

WebSocket のライフサイクル

クライアント サーバ

Open Open

Close

MessageMessageMessageMessageMessageMessage

Error

接続確立

接続切断

Page 17: Java EE 7 Detail Okinawa

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

WebSocket APIwww.w3.org/TR/websockets/

Page 18: Java EE 7 Detail Okinawa

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

Java API for WebSocket (JSR-356)

http://jcp.org/en/jsr/detail?id=356http://java.net/projects/websocket-spec

仕様

ステータス: Early Draft

Page 19: Java EE 7 Detail Okinawa

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

参照実装http://java.net/projects/tyrus

最新の GlassFish v4 開発ビルドにバンドル済

Page 20: Java EE 7 Detail Okinawa

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

アノテーション レベル 内容

@ServerEndpoint クラス WebSocket のエンドポイントを示す

@OnOpen メソッド 接続確立時に呼び出すメソッドを指定

@OnClose メソッド 接続切断時に呼び出すメソッドを指定

@OnMessage メソッド メッセージを受信した時に呼び出すメソッドを指定

@PathParam メソッドパラメータ

エンドポイントの URI 引数に指定されたパラメータを取得する際に仕様

@OnError メソッド エラー発生時に呼び出すメソッドを指定

WebSocket アノテーション

Page 21: Java EE 7 Detail Okinawa

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

WebSocket アプリケーション・ライフサイクル

@ServerEndpoint("/hello")var wsUri = "ws://HOST/foo/hello";var websocket = new WebSocket(wsUri);

@OnOpenwebsocket.onopen = function(evt) { onOpen(evt) };

websocket.onmessage = function(evt) { onMessage(evt) };

@OnMessage

websocket.onerror = function(evt) { onError(evt) };

@OnError

websocket.onclose = function(evt) { onClose (evt) };

@OnClose

JavaScript WebSocket API

Page 22: Java EE 7 Detail Okinawa

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

import javax.net.websocket.annotations.*;

@ServerEndpoint("/hello")public class HelloBean {

@OnMessage public void sayHello(Session session, String msg) { session.getRemote().sendString(“Hello” + msg); }}

HelloWorld アプリケーション (1 対1 )

Page 23: Java EE 7 Detail Okinawa

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

Twitter

Singleton

サンプル・アプリケーションの構成 ( 多対1 )

Remote Endpoint

RE

RE

RE

Session

Session

Session

WebSocketEndpoint

Twitter4J

Page 24: Java EE 7 Detail Okinawa

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

@ServerEndpoint(value= "/twitter")public class TwitterWebSocket { @OnOpen public void initOpen(Session session) { TwitterClientSingleton.peers.add(session); } @OnClose public void closeWebSocket(Session session){ TwitterClientSingleton.peers.remove(session); }}

Twitter Streaming アプリ

Page 25: Java EE 7 Detail Okinawa

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

@Startup   @Singletonpublic class TwitterClientSingleton extends StatusAdapter { public static Set<Session> peers = null;  static { peers = Collections.synchronizedSet(new HashSet()); } ...続く

Twitter Streaming アプリ

Session Session Session Session

クライアントの参照情報を保持

RE RE RE RE・・・・・・

Page 26: Java EE 7 Detail Okinawa

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

public void onStatus(Status status) { User user = status.getUser(); String resStr = "@" +user.getScreenName() + " : " + status.getText() ; try { for (Session peer : peers) { peer.getBasicRemote().sendText(resStr); } } catch (IOException ioe) { ioe.printStackTrace(); } }

データ送信コード ( ブロッキング )シーケンシャルに配信を実施 ( 大量のデータで待ち )

全ピア (RE) に対して配信

Session Session Session Session

RE RE RE RE

Page 27: Java EE 7 Detail Okinawa

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

データ送信コード ( ノン・ブロッキング

for (Session peer : peers) { Future<Void> futureSend = peer.getAsyncRemote().sendText (resStr); while (!futureSend.isDone()) { // wait here ? } SendResult status = futureSendResult.get(); if (status.isOK()) { // ok } else { // whoops, send failed. }}

Page 28: Java EE 7 Detail Okinawa

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

Java EE 7 Themes

Batch Concurrency Simplified JMS

More annotated POJOs Less boilerplate code Cohesive integrated

platform

DEVELOPERPRODUCTIVITY

WebSockets JSON Servlet 3.1 NIO REST

MEETING ENTERPRISE DEMANDS

Java EE 7

Page 29: Java EE 7 Detail Okinawa

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

Java EE 7 : 2013 年 春頃提供開始予定

テーマ: HTML5 と開発生産性の向上

Java EE 7 SDK の提供開始

Page 30: Java EE 7 Detail Okinawa

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

Java EE 7 よりWeb Profile に追加

• JAX-RS• WebSocket• JSON-P

Page 31: Java EE 7 Detail Okinawa

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

Concurrency Utilities for EE

Page 32: Java EE 7 Detail Okinawa

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

Application ServersApplication Servers

Java SE

Web/EJB コンテナ

EJB JSP

Servlet

Runnable

CallableJava EE 関連機能 (JAX-RS,JavaMail, CDI など )

Java EE 環境で禁止されていた Thread 生成

コンテナ外部で動作

必要な情報の伝搬が

不可能

Page 33: Java EE 7 Detail Okinawa

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

Application ServersApplication Servers

Java SE

Web/EJB コンテナ

EJB JSP

Servlet

Runnable

Callable

Java EE と Concurrency のアーキテクチャ

Java EE 関連機能(JAX-RS,JavaMail, CDI など )

ManagedExecutor ServiceManagedScheduledExecutorServiceContextServiceManagedThreadFactory

Concurrency Utilities for EE

Page 34: Java EE 7 Detail Okinawa

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

タスクを実装

A implements RunnableB implements Callable

非同期タスクを実装

リソース・インジェクションでサーバ管理スレッドを利用

サーバ側で設定を実施

デフォルト設定も利用可

1 2

EE 環境の非同期タスクの実装手順

Page 35: Java EE 7 Detail Okinawa

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

public class MyRunnableTask implements Runnable { @Override public void run() { try { Thread.sleep(10000); //何らかの処理 } catch (InterruptedException ex) { logger.log(Level.SEVERE, null, ex); } }}

Runnable のタスクを作成1

Page 36: Java EE 7 Detail Okinawa

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

Callable のタスクを作成    ● call() は返り値を持つ事が可能    ● チェック例外のスローが可能

public class MyCallableTask implements Callable<String> { @Override public String call() throws Exception { return “Hello World”; }}

Page 37: Java EE 7 Detail Okinawa

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

サーバ側の設定例2

Page 38: Java EE 7 Detail Okinawa

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

@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<String> singleFuture =    managedExecsvc.submit(singleTask);}

非同期タスクを実行する EJB3

サーバ設定のリソースをインジェクト

Page 39: Java EE 7 Detail Okinawa

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

非同期タスクのスケジューリング(ManagedScheduledExecutorService の利用例 )

Page 40: Java EE 7 Detail Okinawa

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

Memo

実行命令から指定した時間経過後

タスクを実行 !!

トリガーも指定可能

Page 41: Java EE 7 Detail Okinawa

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

タスクを実装

A implements RunnableB implements Callable

非同期タスクを実装

リソース・インジェクションでサーバ管理スレッドを利用

サーバ側で設定を実施

デフォルト設定も利用可

1 2

EE 環境の非同期タスクの実装手順

Page 42: Java EE 7 Detail Okinawa

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

サーバ側の設定例2

Page 43: Java EE 7 Detail Okinawa

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

@Statelesspublic class MyManagedScheduledExecutorService{ @Resource(name = "concurrent/ DefaultManagedScheduledExecutorService") ManagedScheduledExecutorService managedScheduledExecsvc;

public void execScheduledExecutorService() { MyRunnableTask task = new MyRunnableTask(); managedScheduledExecsvc.schedule( task, 60L, TimeUnit.SECONDS); }

非同期タスクを実行する EJB3

1分後にタスクを実行

Page 44: Java EE 7 Detail Okinawa

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

@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) }

非同期タスクを実行する EJB3

独自トリガーも

指定可能

Page 45: Java EE 7 Detail Okinawa

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

import javax.enterprise.concurrent.Trigger;public class MyTrigger implements Trigger {

@Override public Date getNextRunTime(LastExecution le, Date date){ }

@Override public boolean skipRun(LastExecution le, Date date) {

}} いつ、どのような時にタスクを実行するか

スキップするかプログラミング上から制御

Page 46: Java EE 7 Detail Okinawa

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

タスクのライフサイクル管理・監視

Page 47: Java EE 7 Detail Okinawa

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

タスクのライフサイクル

taskSubmitted Submitted taskStarting

taskAborted Started

taskDone

Done

submit()の実行

submit成功

キャンセル・中止

タスクの実行準備

タスク実行

キャンセル・中止タスク実行完了

タスク状態の監視

が可能

  部分を実装

Page 48: Java EE 7 Detail Okinawa

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

public class MyManagedTaskListener implements ManagedTaskListener { 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){ }}

ManagedTaskListener インタフェース

を実装した独自のタスク・リスナーを作成

Page 49: Java EE 7 Detail Okinawa

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

@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);}

Page 50: Java EE 7 Detail Okinawa

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

Page 51: Java EE 7 Detail Okinawa

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