18
DevLOVE: モモモモモモモモモモ モモモモモモモモ OSGi 2010/8/23 モモ モモ (@nobusue) ※ モモモモモ 2010/8/23 モモモモモモモモモモモモモモモモモモモモモモモ モモモモモモモモモモモモモモモモモモモモモモモモ 、、 モモモモモモ モモモモモモモモモモモモモモモモモモモモ ・。

DevLOVE 20100823 EnterpriseOSGi

Embed Size (px)

DESCRIPTION

2010/8/23 DevLOVE モジュール指向勉強会の資料です

Citation preview

Page 1: DevLOVE 20100823 EnterpriseOSGi

DevLOVE: モジュール指向勉強会エンタープライズ OSGi

2010/8/23須江 信洋 (@nobusue)

※ この資料は 2010/8/23 時点での公開情報に基づいて記載されています。また、資料の内容は個人としての見解を述べたものであり、所属する企業・組織が内容を保証するものではありません。

Page 2: DevLOVE 20100823 EnterpriseOSGi

自己紹介氏名 : 須江 信洋 ( すえ のぶひろ )所属 : 日本 IBM ソフトウェア事業仕事 : JavaEE 関連のプリセールスお世話になっているコミュニティ

JJUG, JGGUG, JSUGその他

http://twitter.com/nobusueGroovy と猫が好きです

Page 3: DevLOVE 20100823 EnterpriseOSGi

menuなぜ今 OSGi か?OSGi 概要エンタープライズ OSGi

Page 4: DevLOVE 20100823 EnterpriseOSGi

なぜ今 OSGi ?

Answer:Java はモジュール指向プラットフォーム

じゃないから

Page 5: DevLOVE 20100823 EnterpriseOSGi

モジュール指向を実現するには モジュール境界が定義できること

外部に公開するインターフェース モジュールに閉じたリソース 物理的なパッケージングの仕様

モジュール間の依存性管理が可能であること 依存先モジュールの明示 モジュールのバージョン管理 依存関係の解決

モジュールのライフサイクル管理が可能であること インストール / アンインストール 開始 / 停止 ライフサイクルイベントへの対応

Page 6: DevLOVE 20100823 EnterpriseOSGi

Java の現状 モジュール境界が定義できること

クラスやメソッドの可視性だけでは制御が難しい public ではクラスローダー全体に公開されてしまう そもそも、 Java のクラスはモジュールとしては細かすぎ

JAR はモジュールとしては不十分 単に複数のクラスやリソースをまとめる機能のみ ロードされた後はクラスと同じ扱い

モジュール間の依存性管理が可能であること 静的な依存関係はコンパイルしてみないと分からない 実行時の依存関係は実行してみないと分からない バージョン管理機能はない

モジュールのライフサイクル管理が可能であること ロードしたクラスはアンロードできない ライフサイクルという概念はない ( ロードされたら直ちに有効と

なる )

Page 7: DevLOVE 20100823 EnterpriseOSGi

7

OSGi の歴史 1999 年:「 Open Service Gateway Initiative 」が設立

当初は家庭や小規模オフィス向けのゲートウェイ装置で動作するサービスプログラムの実行基盤

2003 年:名称を「 OSGi Alliance 」に変更 対象を車載機器やモバイル端末、エンタープライズシステム

に拡大 2003 年: Eclipse 3.0 が

プラグイン管理システムとして OSGi 仕様を採用 Java の世界での知名度が一気に向上

2010 年: OSGi R4.2 の一部として Enterprise Specification公開 Java EE 環境で OSGi を活用するための拡張仕様 Spring Framework 由来の DI コンテナ機能も標準化

Page 8: DevLOVE 20100823 EnterpriseOSGi

8

OSGi の提供する機能 Moduleレイヤー

依存関係の解決複数バージョンの管理

Life Cycleレイヤーモジュールの動的ロード

Serviceレイヤー Securityレイヤー

実行環境が依存関係を管理

同一モジュールの異なるバージョンが存在しても OK

JVM を起動したままモジュールの入れ替えが可能

Page 9: DevLOVE 20100823 EnterpriseOSGi

9

OSGi におけるモジュール「 Bundle 」

Manifest-Version: 1.0Bundle-SymbolicName: com.ibm.jp.myappBundle-Name: My Application BundleBundle-Vendor: IBM JapanBundle-Version: 2.0.0Import-Package: com.ibm.jp.mylib1.ui;version="[1.2.0,1.3.0)", com.ibm.jp.mylib1.util;version="[1.2.0,1.3.0)"Export-Package: com.ibm.jp.myapp;version="2.0.0"Bundle-RequiredExecutionEnvironment: J2SE-1.5Bundle-Activator: com.ibm.jp.myapp.MyAppActivator

Manifest-Version: 1.0Bundle-SymbolicName: com.ibm.jp.myappBundle-Name: My Application BundleBundle-Vendor: IBM JapanBundle-Version: 2.0.0Import-Package: com.ibm.jp.mylib1.ui;version="[1.2.0,1.3.0)", com.ibm.jp.mylib1.util;version="[1.2.0,1.3.0)"Export-Package: com.ibm.jp.myapp;version="2.0.0"Bundle-RequiredExecutionEnvironment: J2SE-1.5Bundle-Activator: com.ibm.jp.myapp.MyAppActivator

Jar のマニフェストファイルにMetadata を記述

バンドルは OSGi Framework 実装が管理する「 Repository 」に登録されその管理下でロード・実行される

Page 10: DevLOVE 20100823 EnterpriseOSGi

10

バージョン管理ImportExport

com.foo.bar

1.4.5

com.foo.bar

1.3.12

com.foo.bar

[1.2.0, 1.4.0)

com.foo.bar

[1.4.0, 1.5.0)

Page 11: DevLOVE 20100823 EnterpriseOSGi

11

ライフサイクル管理

package com.ibm.jp.myapp;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class MyAppActivator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("Bundle is starting."); } public void stop(BundleContext context) throws Exception { System.out.println("Bundle is stopping."); }}

package com.ibm.jp.myapp;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class MyAppActivator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("Bundle is starting."); } public void stop(BundleContext context) throws Exception { System.out.println("Bundle is stopping."); }}

Page 12: DevLOVE 20100823 EnterpriseOSGi

OSGi R4.2 Enterprise SpecOSGi Alliance Enterprise Expert

Group(EEG) によって定められたEnterprise 向けの仕様http://www.infoq.com/news/2010/03/osgi-

enterprise-42-released以下が規定されている

アプリケーションのアセンブリ・フォーマットの拡張

JavaEE コンテナ・サービスとの統合宣言的な DI 仕様 (Blueprint)

Page 13: DevLOVE 20100823 EnterpriseOSGi

デプロイメント・モデル

OSGi Bundle(.jar)

OSGi Bundle(.jar)

OSGi Bundle(.jar)

Web App Bundle(.war)

EnterpriseBundle App

(.eba)

EnterpriseBundle App(.eba)

EnterpriseBundle App(.eba)

BusinessLevel

Application

VirtualHost

Context

Path

OSGimetadat

a

Page 14: DevLOVE 20100823 EnterpriseOSGi

Bundle の入れ替え : WAS の例

14

バージョンを指定して保存⇒アプリケーション再起動で反映

Page 15: DevLOVE 20100823 EnterpriseOSGi

Blueprint と宣言的サービス あるBundleが提供するサービスを、 Client(他のBundle)から

動的に参照 JNDIサービスにより、OSGiのサービスレジストリに JNDI経由でアク

セス可能

OSGi Bundle(.jar)

<blueprint xmlns:tx="http://www.ibm.com/appserver/schemas/8.0/blueprint/transactions" xmlns:jpa="http://www.ibm.com/xmlns/ibm-blueprint-jpa/v1.0.0"><bean id="blabberImpl" class="com.ibm.ws.eba.example.blabber.persistence.BlabberImpl"> <jpa:context property="entityManager" unitname="blabber" /> <tx:transaction method="*" value="Required"/></bean><service id="blabberService" ref="blabberImpl" interface="com.ibm.ws.eba.example.blabber.persistence.spi.BlabberUserInterface" /></blueprint>

[OSGI-INF/blueprint/xxx.xml]

InitialContext ic = new InitialContext(); return (BlabberUserInterface) ic.lookup("osgi:service/" + BlabberUserInterface.class.getName());

Client

Page 16: DevLOVE 20100823 EnterpriseOSGi

OSGi の利点 (1)設計モデルと実行モデルの一致

設計モデル

実行モデル (Java)

A.jar B.jar

C.jar

A.jar B.jar

C.jar

flat classloader

実行モデル (OSGi)

A.jar B.jar

C.jar

classloader by bundle

Page 17: DevLOVE 20100823 EnterpriseOSGi

OSGi の利点 (2)依存関係のトレーサビリティ

A.jar B.jar

C.jar D.jar

E.jarF.jar G.jar

モジュールを入れ替える際に影響のあるモジュールが機械的に特定できる

Page 18: DevLOVE 20100823 EnterpriseOSGi

18

参考資料 OSGi R4.2 Specs

http://www.osgi.org/Download/Release4V42 Introduction to OSGi by Neil Bartlett

http://www.slideshare.net/njbartlett/introduction-to-osgi-tokyo-jug

"Enterprise OSGi in Action"(Manning) http://www.manning.com/alves/ PDF(MEAP版 ) が購入可能

エンタープライズ OSGi入門 : 第 1回 OSGi概要と実行環境の導入 http://

www.ibm.com/developerworks/jp/websphere/library/was/was7_fep_osgi/1.html