OSGi Counter Service

This sample demonstrates how to declare services in the blueprint file of a bundle, which makes it accessible by other bundles in the application. The OSGi application consists of an OSGi web bundle that contains a servlet that accesses a service that is provided in another bundle project. The application runs on either WebSphere® Application Server traditional or Liberty. This sample is an introduction to using OSGi application development tools.

Tip: You can run this sample on WebSphere Application Server traditional Versions 7.0, 8.0, and 8.5. You can also run this sample on WebSphere Application Server Liberty.
Learn more about installing WebSphere Application Server Version 7.0: To run this sample on WebSphere Application Server traditional Version 7.0, you must install the Feature Pack for OSGi Applications and Java™ Persistence API 2.0. To Install the feature pack:
  1. Open the IBM® Installation Manager.
  2. Click Install. The Install Packages page opens.
  3. In the package list, select IBM WebSphere Application Server traditional Version 7.0 Test Environment, then click Next.
  4. Read the license agreements. Accept the license agreements then click Next.
  5. Follow the instructions in the Installation Manager to install WebSphere Application Server traditional Version 7.0.
  6. In the Features list, ensure that you select OSGi Applications under IBM WebSphere Application Server traditional Version 7.0 Feature Pack for OSGi Applications and Java Persistence API 2.0.
Learn more about installing WebSphere Application Server Version 8.0: To run this sample on WebSphere Application Server traditional Version 8.0, you must install the server:
  1. Open the IBM Installation Manager.
  2. Click Install. The Install Packages page opens.
  3. In the package list, select IBM WebSphere Application traditional Server Version 8.0, then click Next.
  4. Read the license agreements. Accept the license agreements then click Next.
  5. Follow the instructions in the Installation Manager to install WebSphere Application Server traditional Version 8.0.
Learn more about installing WebSphere Application Server Version 8.5: To run this sample on WebSphere Application Server traditional Version 8.5, you must install the server:
  1. Open the IBM Installation Manager.
  2. Click Install. The Install Packages page opens.
  3. In the package list, select IBM WebSphere Application Server traditional Version 8.5, then click Next.
  4. Read the license agreements. Accept the license agreements then click Next.
  5. Follow the instructions in the Installation Manager to install WebSphere Application Server traditional Version 8.5.
Learn more about installing WebSphere Application Server Liberty: To run this sample on WebSphere Application Server Liberty, you must install the server. See Installing Liberty for more information.
To deploy your OSGi Counter application to a server:
  1. In Enterprise Explorer, expand CounterWebBundle > CounterWebBundle > Servlets.
  2. Right-click CounterServlet and select Run As > Run on Server. The Run On Server dialog opens.
  3. Click WebSphere Application Server from the list of servers and click Finish.

The string greet.getText()=0 Hello World! is displayed in the browser. Each time the page is reloaded the value increments.

Switch to the Console view (Window > Show View > Console) to view the output from the server. A successful outcome displays the output from CounterImpl.init(), GreetImpl.init(), and WorldImpl.init(), based on the initialization method entries for the CounterImpl, GreetImpl, and WorldImpl beans in the blueprint file:
[7/21/10 11:42:18:109 EDT] 0000002f StepStartBLA  A   CWWMH0300I: Starting business-level application "WebSphere:blaname=CounterApp".
[7/21/10 11:42:19:421 EDT] 0000002f webapp        I com.ibm.ws.webcontainer.webapp.WebGroupImpl WebGroup SRVE0169I: Loading Web Module: CounterWebBundle.
[7/21/10 11:42:19:515 EDT] 0000002f WASSessionCor I SessionContextRegistry getSessionContext SESN0176I: Will create a new session context for application key default_host/CounterWebBundle
[7/21/10 11:42:19:531 EDT] 0000002f webcontainer  I com.ibm.ws.wswebcontainer.VirtualHost addWebApplication SRVE0250I: Web Module CounterWebBundle has been bound to default_host[*:9080,*:80,*:9443,*:5060,*:5061,*:443].
[7/21/10 11:42:19:593 EDT] 0000002f StepStartBLA  A   CWWMH0196I: Business-level application "WebSphere:blaname=CounterApp" was started successfully.
[7/21/10 11:42:19:640 EDT] 0000001d SystemOut     O WorldImpl.init() called.
[7/21/10 11:42:19:640 EDT] 00000032 SystemOut     O CounterImpl.init() called.
[7/21/10 11:42:19:687 EDT] 00000032 SystemOut     O GreetImpl.init() called.
Note: If the output from the CounterImpl.init(), GreetImpl.init(), and WorldImpl.init() does not display in the console output, check the output for error messages during deployment or startup of the application and then check the blueprint files for possible errors in the bean and service definitions.
Icon that indicates the type of topic Sample topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: counterExamples.html