Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools

Practical Lecture: Software Architecture

After this lecture you will have created an OSGi Service and a Web Service.

Tool Requirements

  • Eclipse IDE
  • Java JDK (also added to the binary path)

How to create and consume a simple Web Service using JAX WS

In this example, we create a SOAP based web service for a simple Java Calculator class with operations ‘add’ and ‘subtract’. We then create a web service client which consumes the web service and displays the result of the invoked web service.

Creating and Publishing the Web Service

  1. Create a Java Project “CalcWS” and create a package “”.
  2. Create a Java class “Calculator” in this package with the following code:
import javax.jws.WebService;
public class Calculator {
    public int add(int a, int b) {
        return (a + b);
    public int sub(int a, int b) {
        return (a - b);
  1. The @WebService annotation at the beginning of the class definition tells the Java interpreter that we intend to publish ALL the methods of this class as a web service. If we want to publish only particular methods then we can use @WebMethod annotation before the method signature.
  2. In order to publish our class and its methods as web service we need to create appropriate stub files or artifacts for web service deployment and invocation. Fortunately Java provides a tool called ‘wsgen’ which generates JAX-WS portable artifacts used in JAX-WS web services.
  3. Open command prompt go to the project folder “CalcWS”.
  4. Now issue the following command:
wsgen -s src -cp bin  -d bin

The –cp option specifies the classpath for our Calculator class which is in “bin” folder, the –d option specifies where to place generated output files which is also the ‘bin’ folder in our case and the “-s src” tells the generator to create the source class in the src-folder.

  1. In this step we want to publish our class as a web service endpoint. For that we use the static publish() method of the class to publish our “Calculator” class as a web service in the specified context root. Create a class ‘CalcEndpointPublisher’ with main method and type the following code:
public class CalcEndpointPublisher {

    public static void main(String[] args) {
                        new Calculator());
  1. Run this class as “Java Application”.
  2. You may not get output in the Console. To check whether our class is published as web service, open a browser and type the URL mentioned in the endpoint with a parameter ?wsdl appended.


  1. When you run the application, the Java SE 6 platform has a small web application server that will publish the web service at the address http://localhost:8080/CalcWS/Calculator while the JVM is running. If you see a large amount of XML that describes the functionality behind the web service, then the deployment is successful.

Creating and Consuming a Web Service Client

  1. Having published the web service, we now create a client which communicates with the service and displays the result. Create a Java project “CalcWSClient”.
  2. Just like ‘wsgen’, JAX-WS also provides a tool called “wsimport” for generating the artifacts required for creating and consuming a web service. “wsimport” takes a wsdl file as input.
  3. From the project folder “CalcWSClient” in command prompt or terminal, issue the following command: “wsimport -s src -d bin http://localhost:8080/CalcWS/Calculator?wsdl”. (Note: Your Web Service needs to be running)
  4. After refreshing your Eclipse workspace you should see the generated files.
  5. Let us now create a Java class with main method to run this client. Create a package “”. In this package create the class “CalcClient” with the following code:
public class CalcClient {
     public static void main(String[] args) {
        int a = 10;
        int b = 12;
        CalculatorService calcService = new CalculatorService();
        Calculator calc = calcService.getCalculatorPort();
        System.out.println(a + " + " + b + " = " + calc.add(a, b));
        System.out.println(a + " - " + b + " = " + calc.sub(a, b));
  1. Run this class as Java Application. You will get the following output in the console:
 10 + 12 = 22
 10 – 12 = -2
  1. Congratulation for consuming your first web service! :)

Hello OSGi

The OSGi components are named bundles. Eclipse offers excellent support for developing OSGi bundles. Not only does it provide wizards for creating OSGi bundles, it also has an embedded Equinox OSGi container that you can use to execute and debug OSGi plugins.Every Eclipse plug-in is essentially an OSGi bundle with some additional Eclipse-specific code. Eclipse also allows you to build standard-compliant OSGi bundles without code specific to Eclipse. In the following you'll learn how to develop a Hello World OSGi bundle using the Eclipse IDE.

Creating the bundle

Follow the steps below to create a Hello World bundle using OSGi and Eclipse.

  1. In Eclipse, click on File –> New –> Project. A New Project dialog will open.
  2. In the New Project dialog, select Plug-in Project and click Next. The Plug-in Project dialog will open.
  3. In the Plug-in Project dialog, enter the following values:
    • Project Name: edu.bonn.osgi.HelloWorld
    • Target Platform: OSGi framework –> Standard
  4. Use default values for the remaining input and click Next. The Plug-in Context dialog will open.
  5. Select the default values for the Plug-in Context dialog and click Next.
  6. In the Templates dialog you'll find only one entry in Available Templates: Hello OSGi Bundle. Select it and click Finish.

Eclipse will take few seconds to generate template code for the Hello World bundle. It will create two files: and MANIFEST.MF.

Your file should look like:

package edu.bonn.osgi.helloworld;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {
	public void start(BundleContext context) throws Exception {
		System.out.println("Hello World!!");

	public void stop(BundleContext context) throws Exception {
		System.out.println("Goodbye World!!");


If your bundle needs to be notified at the time of bundle startup or shutdown then you should create a class implementing the BundleActivator interface. Follow these rules when creating the class:

  • The BundleActivator class must have a public constructor that takes no parameters. The OSGi framework can create a BundleActivator object by calling Class.newInstance().
  • The container will call the start() method of your Activator class to start the bundle. The bundle can take this opportunity to perform resource initialization such as getting a database connection for future use. The start() method takes one argument, the BundleContext object. This object allows bundles to interact with the framework by providing access to OSGi-container-related information. If an exception is thrown for a particular bundle the container will mark that bundle as stopped and will not put it into service.
  • The container will call the stop() method of your Activator class to report that it is shutting down a bundle. You can use this opportunity to perform cleanup tasks such as releasing the database connection.

The MANIFEST.MF file acts as deployment descriptor for your bundle. The format for this file is the same as that of a normal JAR file, so it consists of a set of headers with values. The OSGi specification defines a set of headers that you can use to describe your bundle to the OSGi container. The MANIFEST.MF file for your Hello World bundle should look like:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloWorld
Bundle-SymbolicName: edu.bonn.osgi.HelloWorld
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: edu.bonn.osgi.helloworld.Activator
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6

Let's take a closer look at what each of these headers is used for:

  • Bundle-ManifestVersion: The Bundle-ManifestVersion header tells the OSGi container that this bundle follows the rules of the OSGi specification. A value of 2 means that the bundle is compliant with OSGi specification Release 4; a value of 1 means that it is compliant with Release 3 or earlier.
  • Bundle-Name: The Bundle-Name header defines a short, human-readable name for the bundle.
  • Bundle-SymbolicName: The Bundle-SymbolicName header specifies a unique, non-localizable name for the bundle. This is the name you will use while referring a given bundle from other bundles.
  • Bundle-Version: The Bundle-Version header specifies the version of the bundle.
  • Bundle-Activator: The Bundle-Activator header specifies the name of the optional listener class to be notified of bundle start and stop events. In the given Manifest, the value is edu.bonn.osgi.helloworld.Activator.
  • Import-Package: The Import-Package header defines imported packages for the bundle. You'll learn more about this when I discuss dependency management, later in the article.
  • Bundle-RequiredExecutionEnvironment: The needed execution environment for the bundle.

Executing a bundle

The Eclipse IDE has an embedded Equinox OSGi container that you can use to execute or debug OSGi bundles. Follow these steps to execute the Hello World bundle:

  1. Click on Run –> Run.
  2. Eclipse will open the dialog called “Create, manage and run configuration.” In that dialog, double-click the Equinox OSGi Framework button and it will open a runtime configuration dialog box.
  3. In that dialog, change the value of the Name field to Hello World Bundle.
  4. You will notice that in the Plug-ins section under the Workspace plug-in there is an entry for the edu.bonn.osgi.HelloWorld plugin, which is checked. Under Target Platform, make sure that the checkbox next to the org.eclipse.osgi plugin is also checked. Additionally with Eclipse 4.2 (Juno) you need to check manually: org.apache.felix.gogo.command , org.apache.felix.gogo.runtime , , org.eclipse.equinox.console. Afterwards you further need to click Add required Bundles to add missing dependencies. You should have around 8 selected bundles.
  5. Now click the Run button. You should see a “Hello world” message in the IDE's console view. Note that Eclipse actually opens the OSGi console in its console view.

Continued Tutorial

Due to time constraints of our lecture, we refer to the complete tutorial which also covers creation of an additional bundle with a HelloWorld Service and usage of this bundle in the above edu.bonn.osgi.HelloWorld bundle. The full tutorial can be accessed here: JavaWorld Tutorial: Hello OSGi, Part 1 Bundles for beginners.

teaching/lectures/atsc/2012/practical_lecture_architecture.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2024