Create an OSGi application
For the simple HelloWorld OSGi application, the bundle that defines the service and the bundle that uses the service are packaged together in an OSGi application.
This topic assumes that we have already completed the following tasks:
- Create a service bundle com.ibm.ws.eba.helloWorld.service.
- Create a client bundle com.ibm.ws.eba.helloWorld.client.
You can create the application as described in this topic, in which case the application manifest file is created for you by the tooling, or we can create the application using Apache Ant.
OSGi bundles are packaged as Java™ archive (JAR) files with a .jar extension. A single OSGi application is packaged in an enterprise bundle archive (EBA) file with a .eba extension, just as an enterprise application is packaged in an EAR file with a .ear extension. 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 WebSphere Application Server, 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 file, called APPLICATION.MF, containing the metadata that defines the application. It lists the bundles that the application uses, and says where each bundle is located.
Rational Application Developer v8.5 provides graphical support for creating and packaging bundles. The sample procedure that follows uses this tool. You 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.helloWorldApp, in which the three bundles com.ibm.ws.eba.helloWorld.api, com.ibm.ws.eba.helloWorld.service and com.ibm.ws.eba.helloWorld.client 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.
To create the application...
- Create an OSGi application project.
- Click File > New > OSGi Application Project. The OSGi Application Project panel is displayed.
- Configure the project.
- For Project name, enter com.ibm.ws.eba.helloWorldApp.
- Ensure that Use default location is selected.
- Ensure that Add project to working sets is cleared.
- Click Next.
- In ontained Bundles list, select com.ibm.ws.eba.helloWorld.api 1.0.0, com.ibm.ws.eba.helloWorld.client1.0.0 and com.ibm.ws.eba.helloWorld.service1.0.0.
For more information about these bundles, see Create a service bundle and Create a client bundle.
- Click Finish.
- Optional: Deploy and test the OSGi application project on a server running inside IBM Rational Application Developer.
If you are using Rational Application Developer Version 8, we 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. For another tool, omit this step.
- Right-click the com.ibm.ws.eba.helloWorldApp project, then select Run As > Run on Server. The Run On Server panel is displayed. If we have already configured a WAS in the Rational Application Developer workspace, we can select Choose an existing server, and then select that server. Otherwise, manually define a new server by completing the remaining steps.
- Configure the Run On Server panel:
The WebSphere Application Server Settings panel is displayed.
- For server type, select IBM > WebSphere Application Server v8.0.
- For server host name, enter a valid TCP/IP host name. For example, enter the TCP/IP hostname for the computer or enter localhost.
- For server name, enter the name by which to refer to the server within Rational Application Developer.
- For server runtime environment, select WebSphere Application Server v8.0. If this option is not available in the list...
- Click Add.
- Navigate to the app_server_root directory for your installation of WAS. For more information on the app_server_root directory, see app_server_root.
- Click Finish.
- Click Next.
- Configure the WAS Settings panel:
The list of configured OSGi application projects is displayed.
- For the profile name, select the profile that you use with OSGi Applications.
- For the application server name, verify that the application server name for the profile is displayed. By default, the first application server for a profile is called server1.
- If security is enabled on the application server, enter a user ID and password.
- Click Next.
- Check that the OSGi application project is in the list of configured projects.
- Click Finish. The server starts automatically, the application is published and started, and the following output is displayed on onsole 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...
- Export the project as an EBA file.
- Right-click the project name, then select Export > OSGi > OSGi Application EBA.
- Enter a location for the exported file, then name the file com.ibm.ws.eba.helloWorldApp.eba.
- Click Finish.
Results
You have completed the detailed instructions for developing a simple OSGi application. In this application, a client bundle that uses a service defined in a service bundle 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
We are now ready to deploy and start the OSGi application outside of Rational Application Developer, by completing the following steps:
You can use the administrative console or wsadmin commands to deploy and start an OSGi application in WebSphere Application Server. For information about how to do this for any OSGi application, see Deploy an OSGi application as a business-level application and Start the business-level application. When you check the system output log, you should see the following message:
- Deploy the OSGi application as a business-level application.
- Start the business-level application.
- Check the system output log for the greeting message OSGi Service: Hello World!.
[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
- Create an OSGi application using Apache Ant
You can use the Apache Ant (Ant) command-line tool to package bundles together into an OSGi application.
Parent topic: Develop an OSGi application
Related concepts
Enterprise bundle archives
Related tasks
Create a service bundle Create a client bundle Develop a composite bundle
Best practices for developing and working with OSGi applications
Develop enterprise OSGi applications for WebSphere Application ServerOSGi application design guidelines Writing extensible OSGi applications