您可以使用 OSGi 應用程式作為「服務元件架構 (SCA)」元件。
開始之前
識別要作為 SCA 元件使用的 OSGi 應用程式。 OSGi 應用程式是使用 Blueprint 元件模型來公開或耗用服務的 OSGi 軟體組集合。 OSGi 應用程式包含應用程式資訊清單,可宣告下列服務:
- 應用程式所提供可從應用程式外部存取的服務
- 應用程式想要從應用程式外部耗用的服務
您可以使用 SCA 來提供這些服務的服務連結。
關於這項作業
如果要使用 SCA 來提供服務連結,請執行下列動作:
- 修改 OSGi 應用程式以提供或使用遠端服務。
- 撰寫 SCA 組合定義,以使用 OSGi 應用程式作為元件實作,並提供其遠端服務的連結。
OSGi 應用程式在其應用程式資訊清單 (在企業軟體組保存檔 (EBA) 的 META-INF/APPLICATION.MF 檔中提供) 的應用程式-ImportService 和應用程式-ExportService 陳述式中宣告外部服務。 Application-ExportService 陳述式宣告 OSGi 應用程式所提供的遠端服務。 Application-ImportService 陳述式宣告 OSGi 應用程式所相依的服務。 應用程式資訊清單中指定的所有服務都是可移除的。
程序
- 在 SCA 組合定義中建立
implementation.osgiapp 元件。指定 implementation.osgiapp 元件類型,並將 applicationSymbolicName 和 applicationVersion 設為符合應用程式資訊清單中 Application-SymbolicName 和 Application-Version 屬性的值。
例如,假設 OSGi 應用程式資訊清單 META-INF/APPLICATION.MF 檔包含下列標頭:
Application-SymbolicName: helloworldApp
Application-Version: 1.0.0
在 SCA 組合定義中配置參照這些標頭的元件; 例如:
<?xml version="1.0" encoding="UTF-8"?>
<composite
xmlns="https://www.osoa.org/xmlns/sca/1.0"
xmlns:scafp="https://www.ibm.com/xmlns/prod/websphere/sca/1.0/2007/06"
targetNamespace="https://www.example.com"
name="HelloWorldComposite">
<component name="HelloWorldComponent">
<scafp:implementation.osgiapp
applicationSymbolicName="helloworldApp"
applicationVersion="1.0.0"/>
</component>
</composite>
implementation.osgiapp 元素需要使用與 "https://www.ibm.com/xmlns/prod/websphere/sca/1.0/2007/06" 名稱空間相關聯的 XML 名稱空間字首。
applicationVersion 屬性是選用的。 您可以新增它,以確保使用特定版本的 OSGi 應用程式。
- 識別要從遠端提供的藍圖服務。
- 編輯 OSGi 應用程式資訊清單中的應用程式-ExportService 標頭,以便識別一個以上要匯出的服務介面。
下列範例標頭指定要在應用程式外部提供實作及匯出 example.HelloWorld 介面的藍圖服務。
Application-ExportService: example.HelloWorld
具有此類服務的 Blueprint 元件範例如下:
<blueprint xmlns="https://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="helloWorldComponent" class="example.HelloWorldImpl"
<service id="helloWorld" ref="helloWorldComponent"
interface="example.HelloWorld">
<service-properties>
<entry key="service.exported.interfaces" value="*"/>
</service-properties>
</service>
</blueprint>
Blueprint 服務必須指定 service.exported.interfaces 內容,以識別要從遠端公開其哪些介面。 該值可以是星號 (*) ,以指出其所有介面都可在遠端使用,或可以是特定介面名稱。
- 配置對應於元件中每一個可遠端藍圖服務的 SCA 服務。
將藍圖服務 id 值用於 SCA 服務名稱。 如果藍圖服務沒有 id 值,請改用 Bean id 值。 如果為藍圖服務定義多個介面,則對於第一個服務介面 SCA 服務名稱,請使用藍圖 id 值。 對於第二個以及更新版本的服務,請依 blueprint.xml 檔案中定義的介面順序使用 SCA 服務名稱的 Blueprint
id_ fully qualified interface name 值。
例如,將步驟 2a 中「藍圖」元件所顯示的 helloWorld 服務新增至步驟 1 中的 SCA 組合定義:
<?xml version="1.0" encoding="UTF-8"?>
<composite
xmlns="https://www.osoa.org/xmlns/sca/1.0"
xmlns:scafp="https://www.ibm.com/xmlns/prod/websphere/sca/1.0/2007/06"
targetNamespace="https://www.example.com"
name="HelloWorldComposite">
<component name="HelloWorldComponent">
<scafp:implementation.osgiapp
applicationSymbolicName="helloworldApp"
applicationVersion="1.0.0"/>
<service name="helloWorld">
<binding.sca>
</service>
</component>
</composite>
此範例使用 binding.sca 作為服務連結。 服務可以透過 SCA 支援的一或多個其他連結提供,但 EJB 2.x 服務連結除外。
不需要 SCA 服務元素。 如果未指定,依預設會透過 binding.sca 提供服務。
- 識別要從 OSGi 應用程式外部提供的服務。
- 編輯 OSGi 應用程式資訊清單中的應用程式-ImportService 標頭,以便識別一個以上要匯入的服務介面。
下列範例標頭指定匯入 example.Greeting 服務介面:
Application-ImportService: example.Greeting
藍圖參照透過過濾 service.imported 內容來明確要求匯入的服務。 對於應用程式內的本端服務,遠端服務會使用依值傳遞語意,而不是依參照傳遞語意。 下列範例顯示具有此類參照的 Blueprint 元件:
<blueprint xmlns="https://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="helloWorldComponent" class="example.HelloWorldImpl"
<property name="greetingList" ref="greetingRef"/>
</bean>
<service id="helloWorld" ref="helloWorldComponent"
interface="example.HelloWorld">
<service-properties>
<entry key="service.exported.interfaces" value="*"/>
</service-properties>
</service>
<reference-list id="greetingRef" interface="example.Greeting"
filter="(&(service.imported=*))"/>
</blueprint>
- 配置 SCA 參照,以對應於元件中每一個匯入的服務。
例如,將步驟 3 的藍圖元件中顯示的 example.Greeting 參照新增至步驟 2 的 SCA 組合定義:
<?xml version="1.0" encoding="UTF-8"?>
<composite
xmlns="https://www.osoa.org/xmlns/sca/1.0"
xmlns:scafp="https://www.ibm.com/xmlns/prod/websphere/sca/1.0/2007/06"
targetNamespace="https://www.example.com"
name="HelloWorldComposite">
<component name="HelloWorldComponent">
<scafp:implementation.osgiapp
applicationSymbolicName="helloworldApp"
applicationVersion="1.0.0"/>
<service name="helloWorld">
<binding.sca>
</service>
<reference name="example.Greeting">
<binding.sca uri=”MyGreetingComponent”>
</reference>
</component>
</composite>
此範例使用 binding.sca 作為參照連結。 參照可以使用 SCA 支援的一或多個其他連結。
implementation.osgiapp 元件的 SCA 參照隱含地具有對應關係屬性 (multiplicity='0..n') 的 0 對多。 這表示您可以根據應用程式的需求,將參照佈線至 0、1 或多個服務。 對於 multiplicity 屬性:
- 藍圖
reference-list 元素會選取多個服務。 藍圖實作無法對參照清單中的服務順序與複合定義中的連結順序進行任何假設。
- 藍圖
reference 元素會選取單一服務。 如果 SCA 參照提供多個連結,則未指定使用連結的選項。 如果 SCA 參照未提供任何連結,則無法滿足相依關係。 如果參照是必要的, Bean 可能不會啟動。
您可以置換 SCA 參照上的對應關係屬性,設定 1 對多 ('1..n') 或 1-to-1 ('1..1') ,以確保指定特定數目的連結。
下一步
部署 OSGi 應用程式和 SCA 組合,以使用應用程式作為相同商業層次應用程式的組合單元。 您可以使用管理主控台或 wsadmin 指令來建立商業層次應用程式,匯入 EBA 檔和 SCA 組合作為資產,然後將 EBA 和 SCA 資產作為組合單元新增至商業層次應用程式。