Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > Develop OSGi applications > Develop and deploying an OSGi application


Create an OSGi application

For the simple hello-world OSGi application, the bundle that defines the service, and the bundle that uses the service, are packaged together in an OSGi application.


Before you begin

This topic assumes that we have already completed the following tasks:

We can create the application as described in this topic, in which case the application manifest file is created for you by the tooling, or you can create the application using Apache Ant.


About this task

OSGi bundles are packaged as Java archive (.jar) files. A single OSGi application is packaged in an enterprise bundle archive (.eba) file, just as an enterprise application is packaged in an enterprise archive (.ear) file. In this example application, the bundles are packaged directly in the .eba file. However the .eba file does not have to contain the bundles; they can be pulled in at run time. For an OSGi application deployed to WAS, a bundle can be located in the .eba file, or in the WAS internal bundle repository, or in an external bundle repository. Every .eba file contains an application manifest (APPLICATION.MF) file, which contains the metadata that defines the application. It lists the bundles that the application uses, and says where each bundle is located.
IBM Rational Application Developer v8 provides graphical support for creating and packaging bundles. The sample procedure below uses this tool. We can also use other tools, and the steps are adaptable to other tools.

This sample procedure creates a simple hello-world OSGi application called com.ibm.ws.eba.helloWorld, in which the two bundles com.ibm.ws.eba.helloWorldService and com.ibm.ws.eba.helloWorldClient are packaged together in an .eba file.

When you deploy and start this application, it prints the greeting message "OSGi Service: Hello World!" to the system output log.


Procedure

  1. Create an OSGi application project.

    1. Click File > New > Project.
      The Select a wizard panel is displayed.

    2. Click OSGi > OSGi Application Project, then click Next. The OSGi Application Project panel is displayed.

    3. Configure the project.

      • For Project name, enter com.ibm.ws.eba.helloWorld.
      • Ensure that Use default location is selected.
      • Ensure that Add project to working sets is cleared.

    4. Click Next.

    5. In the Contained Bundles list, select com.ibm.ws.eba.helloWorldService 1.0.0 and com.ibm.ws.eba.helloWorldClient 1.0.0

      For more information about these bundles, see Create a service bundle and Create a client bundle.

    6. Click Finish.

  2. Optional: Deploy and test the OSGi application project on a server running inside IBM Rational Application Developer.

    If you are using IBM Rational Application Developer Version 8, you can deploy to a local machine without first exporting the OSGi application as an EBA file. This is typically how you would test your application. If you are using another tool, omit this step.

    1. Right-click the project name then select Run As > Run on Server. The Run On Server panel is displayed.

    2. Configure the Run On Server panel:

      1. For server type, select IBM > WAS v8.0.

      2. For server host name, enter a valid TCP/IP host name. For example, enter the TCP/IP hostname for your computer or enter localhost.

      3. For server name, enter the name by which to refer to the server within IBM Rational Application Developer.

      4. For server runtime environment, select WAS v8.0. If this option is not available in the list, complete the following steps:

        1. Click Add.
        2. Navigate to the WAS_HOME directory for your installation of WAS; for more information on the WAS_HOME directory, see WAS_HOME.

        3. Click Finish.

      5. Click Next.

      The WAS Settings panel is displayed.

    3. Configure the WAS Settings panel:

      1. For profile name, select the profile that you use with OSGi Applications.

      2. For application server name, verify that the application server name for your profile is displayed. By default, the first application server for a profile is called server1.

      3. Click Next.

      The list of configured OSGi application projects is displayed.

    4. Check that your OSGi application project is in the list of configured projects.

    5. Click Finish.

    6. Start the OSGi application:

      1. Click the Servers tab. If the tab is not already visible, open it by selecting Window > Show View > Servers.

      2. On the Servers tab, right-click the application server then select Start.

    The server starts, the application is published and started, and the following output is displayed on the Console tab:

    [4/15/10 14:07:33:295 GMT] 00000023 SystemOut O Client: Start...
    [4/15/10 14:07:33:581 GMT] 00000023 SystemOut O OSGi Service: Hello World!
    [4/15/10 14:07:33:581 GMT] 00000023 SystemOut O Client: End... 
    
  3. Export the project as an EBA file.

    1. Right-click the project name, then select Export > OSGi > OSGi Application EBA.

    2. Enter a location for the exported file, then name the file com.ibm.ws.eba.helloWorld.eba.

    3. Click Finish.


Results

You have completed the detailed instructions for developing a simple OSGi application. This application consists of two bundles: one bundle defines a hello service, and the other is a client bundle that uses this service to produce the message "OSGi Service: Hello World!". Your application uses the OSGi service registry to share the hello service between the defining bundle and the client bundle. All interactions with the service registry are handled through Blueprint.


What to do next

You are now ready to deploy and start your OSGi application outside of IBM Rational Application Developer, by completing the following steps:

  1. Deploy your OSGi application as a business-level application.

  2. Start your business-level application.
  3. Check the system output log for the greeting message "OSGi Service: Hello World!".

We can use the administrative console or wsadmin commands to deploy and start an OSGi application in WAS. For information about how to do this for any OSGi application, see Deploy an OSGi application as a business-level application and Start your business-level application. When you check the system output log, you should see the following message:

[4/15/10 14:07:33:295 GMT] 00000023 SystemOut O Client: Start...
[4/15/10 14:07:33:581 GMT] 00000023 SystemOut O OSGi Service: Hello World!
[4/15/10 14:07:33:581 GMT] 00000023 SystemOut O Client: End... 


Subtopics

Parent topic: Develop and deploying an OSGi application

Related concepts:

About OSGi Applications
Enterprise bundle archives

Related tasks:

Create a service bundle
Create a client bundle
Deploy an OSGi application as a business-level application
Debug bundles at run time using the command-line console
Secure OSGi Applications
Develop a composite bundle

Related reference:

Best practices for developing and working with OSGi applications

Develop enterprise OSGi applications for WAS
OSGi Applications: Troubleshooting tips
Task topic Feedback
Copyright IBM Corporation 2009, 2011. All Rights Reserved.
This information center is powered by Eclipse technology.

+

Search Tips   |   Advanced Search