<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    gembin

    OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

    HBase, Hadoop, ZooKeeper, Cassandra

    Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

    There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

    About Me

     

    A Simple OSGi Service

    from http://java.dzone.com/articles/simple-osgi-service

    Lately, I’ve been experimenting more with OSGi, and I want to share some of the examples I’ve put together. The examples involve Felix, Spring Dynamic Modules, and Jetty, though could easily be used with Equinox. Once I’m finished with these exercises, I’m hoping to compare and contrast the different approaches I’ve taken, as well as comparing embedded Jetty with the Equinox Servlet Bridge. I’m a believer that OSGi is a disruptive technology that stands to transform Java development as we know it today.

    There are numerous OSGi tutorials, blogs, and samples posted around the web. My OSGi resources page lists some of these, and if you know of other good resources not listed, please let me know. When I experiment with a new technology, I like to focus exclusively on the technology itself, without code generators, wizards, or other utilities that do half the work for me. I feel it maximizes the experience. To that end, all of my exercises avoid using tools like Maven or Eclipse. I find Maven confusing and using Eclipse for some of these smaller exercises is overkill. Save for a few necessary tools like Ant and the Felix framework itself, I’ll roll everything by hand. Hopefully, this helps clarify the core essence of OSGi.

    The first sample is the canonical HelloWorld example where I create two simple bundles - a client.jar and service.jar. Again, in OSGi parlance, a bundle is a .jar file. You can find the working code in My Google Code repository (the HelloWorld project), complete with the Ant build script, the bundle cache, and a .bat or .sh script to start Felix depending on your OS. The only two prerequisites for these samples is Felix (I used version 1.0.0) and Ant, which you’ll have to download separately. Let’s walk through the creation of these two simple bundles.

    First, let’s look at HelloConsumer in client.jar. HelloConsumer uses the HelloService to get the hello and goodbye messages when the client.jar bundle is started and stopped, respectively. HelloConsumer implements OSGi’s BundleActivator, meaning Felix will invoke the start method when the bundle is started, and the stop method when the bundle is stopped. As seen on line 26, HelloConsumer obtains a reference to the HelloService using the OSGi ServiceTracker class.

    1. package com.extensiblejava.hello.client;  
    2.   
    3. import com.extensiblejava.hello.service.HelloService;  
    4.   
    5. import org.osgi.framework.BundleActivator;  
    6. import org.osgi.framework.BundleContext;  
    7. import org.osgi.framework.ServiceReference;  
    8. import org.osgi.util.tracker.ServiceTracker;  
    9.   
    10. public class HelloConsumer implements BundleActivator {  
    11.   
    12.     private ServiceTracker helloWorldTracker;  
    13.     private HelloService helloService;  
    14.   
    15.     public void setService(HelloService helloService) {  
    16.         this.helloService = helloService;  
    17.     }  
    18.   
    19.     public void removeService() {  
    20.         this.helloService = null;  
    21.     }  
    22.   
    23.     public void start(BundleContext context) throws Exception {  
    24.         helloWorldTracker = new ServiceTracker(context, HelloService.class.getName(), null);  
    25.         helloWorldTracker.open();  
    26.         HelloService hello = (HelloService) helloWorldTracker.getService();  
    27.   
    28.         if (hello == null) {  
    29.             System.out.println("Hello service unavailable on HelloConsumer start");  
    30.         } else {  
    31.             System.out.println(hello.sayHello());  
    32.         }  
    33.     }  
    34.   
    35.     public void stop(BundleContext context) {  
    36.         HelloService hello = (HelloService) helloWorldTracker.getService();  
    37.         if (hello == null) {  
    38.             System.out.println("Hello service unavailable on HelloConsumer stop");  
    39.         } else {  
    40.             System.out.println(hello.sayGoodbye());  
    41.         }  
    42.   
    43.         helloWorldTracker.close();  
    44.     }  
    45.   
    46. }  

     

    The HelloService and HelloServiceImpl form the OSGi service. HelloService is a simple Java interface that defines the API, while HelloServiceImpl implements the BundleActivator and is the class invoked when the server bundle is started and stopped. As seen on line 18, HelloServiceImpl registers itself as an OSGi service. Without registration, HelloConsumer would not be able to use HelloService as an OSGi service.

    1. package com.extensiblejava.hello.service.impl;  
    2.   
    3. import java.util.Properties;  
    4. import com.extensiblejava.hello.service.HelloService;  
    5. import org.osgi.framework.BundleActivator;  
    6. import org.osgi.framework.BundleContext;  
    7. import org.osgi.framework.ServiceListener;  
    8. import org.osgi.framework.ServiceEvent;  
    9. import org.osgi.framework.ServiceRegistration;  
    10.   
    11. public class HelloServiceImpl implements HelloService, BundleActivator {  
    12.   
    13.     private ServiceRegistration registration;  
    14.   
    15.     public void start(BundleContext context) {  
    16.         Properties props = new Properties();  
    17.         props.put("Language""English");  
    18.         registration = context.registerService(HelloService.class.getName(), this, props);  
    19.     }  
    20.   
    21.     public void stop(BundleContext context) {  
    22.   
    23.     }  
    24.   
    25.     public String sayHello() {  
    26.         return "Hello World!! ";  
    27.     }  
    28.   
    29.     public String sayGoodbye() {  
    30.         return "Goodbye World!!";  
    31.     }  
    32. }  

    There’s a bit more magic here that makes this all work. A wonderful aspect of OSGi is that dependencies must be managed explicitly, with each bundle’s manifest declaring the packages the bundle imports and exports. Bundle manifests specify other key pieces of information, too. Notably, it tells the OSGi environment of the BundleActivator to invoke on start and stop. If a bundle imports a package, then another bundle within that OSGi run-time must export that same package. The client bundle manifest imports the package containing HelloService while the service bundle manifest exports that same package. Were these imports and exports not explicitly declared, we’d receive OSGi run-time errors. In our example above, HelloServiceImpl registers itself as an OSGi service allowing clients to obtain a reference to HelloService using the OSGi ServiceTracker.

    The client and service OSGi bundles can be deployed to any OSGi run-time. I’ve used Felix for this example, but could have used Equinox just as easily. To run these examples, you can checkout the code from the Google Code repository. Since I include the bundle cache in the repository, you should be able to start felix and experiment with OSGi. You’ll likely have to modify the start script (OS-based) to ensure Felix is in your classpath. Upon starting Felix, you’ll be asked to enter a profile name. To use the bundle cache included with the example, simply use HelloWorld as your profile.

    If you’re feeling a bit more adventurous, you can make some changes to the HelloServiceImpl message and rebuild the project by invoking the Ant build script. Once compiled, move back into the Felix shell and update the service bundle using the Felix update command. For a listing of all Felix commands, simply type ‘help’.

    If you want to deploy the bundles to your own Felix cache, restart Felix and enter a different profile name. Any name will do, and Felix will create a new bundle cache for you. Here are some more suggested steps to build, install, and continue experimenting with the bundles and OSGi:

    • Build service by executing ant within service directory
    • Build client by executing ant within client directory
    • Run startfelix.bat or startfelix.sh depending on your OS
    • install the bundles from the felix shell
      • install file:service/bin/service.jar
      • install file:client/bin/client.jar
      • ps
      • start {service-bundle-id}
      • start {client-bundle-id}
    • Experiment by starting and stopping client and service to get a feel for OSGi.
    • Now change the service message printed in HelloServiceImpl.java and compile. Then do the following while client is running.
      • stop {service-bundle-id}
      • update {service-bundle-id}
      • start {service-bundle-id}
      • stop {client-bundle-id}

    When stopping the client bundle, you should see a different goodbye message than what you saw in step 4.

    OSGi offers a very dynamic and adaptable run-time environment. Even when not using OSGi services, reuse across bundles can occur so long as the package dependencies are explicitly managed in the bundle manifests. The benefit with OSGi services is that the OSGi run-time manages the service lifecycle, however. Many of the presentations I give on architecture & design talk about the importance of managing .jar relationships, and I’ve long felt that the .jar file is a great candidate as a first class component on the Java platform. In the next post, I’ll explore the simple benefits of an incredibly important heuristic that states we should “separate interface from implementation.” You can get a head start by taking a look at the HelloWorldSpec project on My Google Code repository.

    http://techdistrict.kirkk.com/

    Article Type: 
    Opinion/Editorial

    posted on 2008-10-29 14:23 gembin 閱讀(767) 評論(0)  編輯  收藏 所屬分類: OSGi

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(6)

    隨筆分類(440)

    隨筆檔案(378)

    文章檔案(6)

    新聞檔案(1)

    相冊

    收藏夾(9)

    Adobe

    Android

    AS3

    Blog-Links

    Build

    Design Pattern

    Eclipse

    Favorite Links

    Flickr

    Game Dev

    HBase

    Identity Management

    IT resources

    JEE

    Language

    OpenID

    OSGi

    SOA

    Version Control

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    free counters
    主站蜘蛛池模板: 一级毛片免费毛片一级毛片免费| 国产精品手机在线亚洲| 日本亚洲高清乱码中文在线观看| 精品亚洲永久免费精品| 又黄又爽的视频免费看| 亚洲视频在线观看网站| 最近更新免费中文字幕大全| 国产卡一卡二卡三免费入口| 亚洲AV无码精品色午夜果冻不卡| 美女裸免费观看网站| 亚洲成人免费电影| 午夜亚洲国产理论秋霞| 人与动性xxxxx免费| 国产午夜免费福利红片| 亚洲人成小说网站色| 久久国产免费福利永久| 亚洲AV无码码潮喷在线观看 | 亚洲AV中文无码乱人伦| 亚洲中文字幕一区精品自拍| 57pao一国产成永久免费| 亚洲欧洲一区二区| **aaaaa毛片免费同男同女| 亚洲国产日韩一区高清在线| 朝桐光亚洲专区在线中文字幕| 四虎1515hh永久久免费| 亚洲色av性色在线观无码| 日本免费一区二区久久人人澡 | 亚洲一区二区三区电影| CAOPORN国产精品免费视频| 亚洲一区二区三区自拍公司| xvideos永久免费入口| 亚洲成av人片不卡无码久久| 永久免费观看黄网站| 亚洲av午夜精品一区二区三区| 美女视频黄a视频全免费网站一区| 国产婷婷高清在线观看免费| 亚洲国产精品免费在线观看| 黄页网站在线看免费| 亚洲卡一卡2卡三卡4麻豆| AV大片在线无码永久免费| 亚洲男人天堂2022|