设为首页 收藏本站
查看: 794|回复: 0

[经验分享] Build and deploy OSGi as Spring bundles using Felix(From IBM developerworks)

[复制链接]

尚未签到

发表于 2017-5-27 09:38:33 | 显示全部楼层 |阅读模式
  source: http://www.ibm.com/developerworks/opensource/library/ws-osgi-spring2/index.html?ca=dgr-jw22&S_TACT=105AGX59&S_CMP=grsitejw22
  



DSC0000.gif
developerWorks>SOA and Web services|Open source>


OSGi and Spring: Part 2: Build and deploy OSGi as Spring bundles using Felix

  A step-by-step guide for the developers to build java components and package it as OSGi based Spring bundles using Apache Felix, an open source OSGi container


DSC0001.gif



Document options


<noscript></noscript>
<!--PAPERSIZE NOT BLANK-->
<noscript></noscript>
DSC0002.gif   Print this page

DSC0003.gif   PDF - Fits A4 and Letter
25KB (9 pages)

DSC0004.gif   Get Adobe® Reader®

DSC0005.gif   E-mail this page

DSC0006.gif   Discuss

  Sample code


<!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!--Updated 12/04/08 by gem-->
Hey there! developerWorks is using Twitter


  Follow us


<!--END RESERVED FOR FUTURE USE INCLUDE FILES-->
Rate this page


  Help us improve this content





  Level: Intermediate
  Naveen Balani (naveenbalani@rediffmail.com), Enterprise Architect, IBM
Rajeev Hathi (rajeevhathi@gmail.com), Technical Architect, Consultant
  30 Mar 2009

Build and package java classes as OSGi bundles using the Spring DM framework in a Felix container. This article, Part 2 of this series, shows you how to create bundles using the Spring framework and then deploy them in a Felix runtime environment. You will see how the core OSGi framework dependency is removed through a simple Spring-based configuration.

<!--START RESERVED FOR FUTURE USE INCLUDE FILES-->
<!--  START : HTML FOR SEARCH REFERRER -->
<!--  START : HTML FOR ARTICLE SEARCH --><!--  END : HTML FOR ARTICLE SEARCH --><!--  START : HTML FOR CODE SEARCH --><!--  END : HTML FOR CODE SEARCH -->




<!--  END : HTML FOR SEARCH REFERRER --><!--END RESERVED FOR FUTURE USE INCLUDE FILES-->  Introduction
  In this article, you will revisit the order application developed in part 1 of the series. The application will now use Spring DM to build and package the bundles. The application client will invoke the service component to process the order and the server component will print the order ID. The article will help you understand the concept of Spring DM and its use with the Felix-based OSGi container.
  System requirements
  To run the examples in this article, make sure the following software is installed and set up on your machine:



  • java 5 or higher
  • Ant build tool
  • Felix binary distribution 1.0.4
  • Spring DM bundles
  After the above distribution is installed, set up the following environment variables: (by way of example set ANT_HOME=C:\apache-ant-1.7.0).


  • JAVA_HOME (for java)
  • ANT_HOME (for Ant)
  Next add the following to the PATH environment variable:


  • JAVA_HOME\bin
  • ANT_HOME\bin
  Spring DM
  Spring DM includes JARs or bundles that help deploy Spring applications in an OSGi environment. Spring DM-based applications can make use of services offered by the OSGi environment. These types of applications provide ease and convenience in the development of OSGi-based applications. Spring DM offers the following benefits in the OSGi environment:


  • Modularizes the application into dynamic bundles and provide runtime services to these bundles.
  • Provides versioning capability to the modules.
  • Modules are bundled as services that can be dynamically discovered and consumed.
  • Modules can be installed, updated and uninstalled dynamically
  • Takes advantage of the Spring framework in configuring the application as OSGi modules
  • Provides seperation of business logic and configuration thereby making development easy and convenient
  Order Application Revisited
  We will revisit the order application developed in part 1 of this series. As you can see, the classes, currently, are strongly coupled with the OSGi framework. Now you will remove this strong coupling and make the classes as simple POJOs with the use of Spring DM. Let's look at the revised OrderClient.java, below.


Listing 1. The client component OrderClient

package order.client;
import order.OrderService;
public class OrderClient {
private OrderService orderService;
public void setOrderService(OrderService orderService) {
this.orderService = orderService;
}
public void removeService() {
this.orderService = null;
}
public void start() {
orderService.processOrder();
}
public void stop() {
System.out.println("Bundle stopped");
}
}



  The OSGi framework dependency is completely removed. The class is a plain POJO with the start() method simply processing the order. There is no use of ServiceTracker class.


Listing 2. OrderService implementation

package order.impl;
import order.OrderService;
public class OrderServiceImpl implements OrderService {
public void start() {
System.out.println("Order Service registered");
}
public void stop() {
System.out.println("Order Service stopped");
}
public void processOrder() {
System.out.println("Order id: ORD123") ;
}
}



  Similarly, the above OrderServiceImpl too a simple POJO with the processOrder() method. There is no association with the OSGi core components. Also neither client nor service classes implement the BundleActivator. The bundle lifecycle here is managed by Spring DM.
  So, how do these simple POJOs work as OSGi components? The magic lies in the Spring configuration files. It is here where you define the OSGi part.
  But before you define XML files, there are some changes to be made to the manifest files. The manifest will include import packages that will contain Spring DM files. This will ensure that Spring will now take over the management and lifecycle of OSGi bundles. The code snippets below illustrates the service and client manifest files.


Listing 3. Service Manifest

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Order Service
Bundle-SymbolicName: orderservice
Bundle-Version: 1.0.0
Import-Package: org.springframework.beans.factory.xml,
org.springframework.aop, org.springframework.aop.framework,
org.aopalliance.aop, org.xml.sax, org.osgi.framework,
org.springframework.osgi.service.importer.support,
org.springframework.beans.propertyeditors,
org.springframework.osgi.service.exporter.support,
org.springframework.osgi.service.exporter
Export-Package: order




Listing 4. Client Manifest file

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Order Service Client
Bundle-SymbolicName: orderclient
Bundle-Version: 1.0.0
Import-Package: org.springframework.beans.factory.xml,
org.springframework.aop, org.springframework.aop.framework,
org.aopalliance.aop, org.xml.sax, org.osgi.framework,
org.springframework.osgi.service.importer.support,
org.springframework.beans.propertyeditors,
org.springframework.osgi.service.importer,
org.springframework.osgi.service.exporter.support,
order



  You will create two XML files each for service and client components. For the service, the orderservice.xml XML file will define the order service implementation bean and the orderservice-osgi.xml XML file will define the order service interface and refer to its implementation. Similarly for the client, XML files will define the order client bean and refer to order service components. The service and client XML files reside under their respective META-INF/spring folder. The below code shows the service XML file


Listing 5. Service XML file orderservice.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="orderService" class="order.impl.OrderServiceImpl"/>
</beans>




Listing 6. Service OSGi XML file orderservice.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="orderService" class="order.impl.OrderServiceImpl"/>
</beans>



  You could actually provide both the bean and OSGi definition in one XML file. It is not necessary to create two sperate files. Here we are making two sperate files so that we distinguish between the interface definition and configuration. Managing and maintaining these files becomes easier. The beauty of a Spring-based configuration is that it will allow you to test your order service bean outside the OSGi container.
  To be able to actually do this, you will have to install the relevant Spring DM jars in Felix.


Listing 7. Portion of Felix configuration

felix.auto.start.1= \
file:/felix-1.0.4/bundle/org.apache.felix.shell-1.0.1.jar \
file:/felix-1.0.4/bundle/org.apache.felix.shell.tui-1.0.1.jar \
file:/felix-1.0.4/bundle/org.apache.felix.bundlerepository-1.0.3.jar \
file:/osgi_spring/order/springlib/aopalliance.osgi-1.0-SNAPSHOT.jar \
file:/osgi_spring/order/springlib/jcl104-over-slf4j-1.4.3.jar \
file:/osgi_spring/order/springlib/log4j.osgi-1.2.15-SNAPSHOT.jar \
file:/osgi_spring/order/springlib/org.apache.felix.main-1.0.1.jar \
file:/osgi_spring/order/springlib/slf4j-api-1.4.3.jar \
file:/osgi_spring/order/springlib/slf4j-log4j12-1.4.3.jar \
file:/osgi_spring/order/springlib/spring-aop-2.5.1.jar \
file:/osgi_spring/order/springlib/spring-beans-2.5.1.jar \
file:/osgi_spring/order/springlib/spring-context-2.5.1.jar \
file:/osgi_spring/order/springlib/spring-core-2.5.1.jar \
file:/osgi_spring/order/springlib/spring-osgi-core-1.0.2.jar \
file:/osgi_spring/order/springlib/spring-osgi-extender-1.0.2.jar \
file:/osgi_spring/order/springlib/spring-osgi-io-1.0.2.jar



  As you can see from what we've demonstrated above, the Felix congifuration file will have the Spring DM bundles defined. You can download the Spring DM bundle and put in the folder of your choice. Once the jars are put in the appropriate folder, make the relevant entries in the Felix config file specifying these jars to be installed at the Felix startup.
  You can then proceed with the steps of installing the client and service bundles in the Felix runtime environment. Once installed, you should be able to start and stop the bundles and see the same results. You will notice that unlike a Felix-based OSGi, this time it is Spring DM that is managing the bundle lifecycle.
  Conclusion
  The power of the Spring framework has made development of OSGi applications simpler and more effective. OSGi itself also has revolutionized the way java applications are bundled. OSGi, in association with Spring, has provided a solid foundation to the development of enterprise-wide applications.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-381597-1-1.html 上篇帖子: WebSphere V7.0 IBM HTTP Server 静默安装(无人值守命令安装)及概要管理命令 下篇帖子: Rational 7系列[四]——IBM Rational ClearCase Version 7.0.0新特性 续
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表