Karaf Tutorial Part 1 - Installation and First application

Skip to end of sidebar Go to start of sidebar
Skip to end of metadata
Go to start of metadata

Getting Started

With this post I am beginning a series of posts about Apache Karaf. So what is Karaf and why should you be interested in it? Karaf is an OSGi container based on Equinox or Felix. The main difference to these fine containers is that it brings excellent management features with it.

Outstanding features of Karaf:

  • Extensible Console with Bash like completion features
  • ssh console
  • deployment of bundles and features from maven repositories
  • easy creation of new instances from command line

All together these features make developing server based OSGi applications almost as easy as regular java applications. Deployment and management is on a level that is much better than all applications servers I have seen till now. All this is combined with a small footprint as well of karaf as the resulting applications. In my opinion this allows a light weight development style like JEE 6 together with the flexibility of spring applications.

Installation and first startup

  • Download Karaf 2.1.3 from the Karaf web site.
  • Extract and start with karaf.bat

You should see the welcome screen:

        __ __                  ____
       / //_/____ __________ _/ __/
      / ,<  / __ `/ ___/ __ `/ /_
     / /| |/ /_/ / /  / /_/ / __/
    /_/ |_|\__,_/_/   \__,_/_/

  Apache Karaf (2.1.3)

Hit '<tab>' for a list of available commands
and '[cmd] \--help' for help on a specific command.
Hit '<ctrl-d>' or 'osgi:shutdown' to shutdown Karaf.

karaf@root>

Some handy commands

> list

Shows the installed bundles. By default you will see nothing a there is a start level threshold that hides the karaf base bundles.

> ls

Shows the active OSGi services. This list is quite long. Here it is quite handy that you can use unix pipes like "ls | grep admin"

> exports

Also very handy is exports which shows the bundle id for each exported package. This helps to find out where a package may come from.

> features:list

Shows which features are installed and can be installed.

> features:install webconsole

This allows to install features (a list of bundles and other features). Using the above command we install the Karaf webconsole.

Now open the webconsole on http://localhost:8181/system/console . Log in with karaf/karaf and take some time to see what it has to offer.

OSGi containers preserve state after restarts
Please note that Karaf like all osgi containers maintains it´s last state. So if something should not work anymore a restart is not sure to help. To really start fresh again stop karaf and delete the data directory.


Check the logs
Karaf is very silent. To not miss error messages always keep a tail -f data/karaf.log open !!

Tasklist - A small osgi application

Without any useful application Karaf is a nice but useless container. So let´s create our first application. The good news is that creating an OSGi application is quite easy and
maven can help a lot. The difference to a normal maven project is quite small. To write the application I recommend to use Eclipse 4 with the m2eclipse plugin.

Eclipse will now import the projects and wire all dependencies using m2eclipse.

The tasklist example consists of three projects

  • tasklist-persistence : model and persistence implementation
  • tasklist-ui : servlet that displays the tasklist
  • tasklist-features : Features descriptor for the application that makes installing in Karaf very easy

Tasklist-persistence

This project contains the domain model and the service implementation. The model is the Task class and a TaskService interface. The persistence implementation TaskServiceImpl manages tasks in a simple HashMap.
The TaskService is published as an OSGi service using a blueprint context. Blueprint is an OSGi standard for dependency injection and is very similar to a spring context.

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
	<bean id="taskService" class="net.lr.tasklist.persistence.impl.TaskServiceImpl" />
	<service ref="taskService" interface="net.lr.tasklist.model.TaskService" />
</blueprint>

The bean tag creates a single instance of the TaskServiceImpl. The service tag publishes this instance as an OSGi service with the TaskService interface.

The pom.xml is of packaging bundle and the maven-bundle-plugin creates the jar with an OSGi Manifest. By default the plugin imports all packages that are imported in java files or referenced in the blueprint context.
It also exports all packages that do not contain the string impl or internal. In our case we want the model package to be imported but not the persistence.impl package. As the naming convention is used
we need no additional configuration.

Tasklist-ui

The ui project contains a small servlet TaskServlet to display the tasklist and individual tasks. To work with the tasks the servlet needs the TaskService.

To inject the TaskService and to publish the servlet the following blueprint context is used:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
	<reference id="taskService" availability="mandatory" interface="net.lr.tasklist.model.TaskService" />
	<bean id="taskServlet" class="net.lr.tasklist.ui.TaskListServlet">
		<property name="taskService" ref="taskService"></property>
	</bean>
	<service ref="taskServlet" interface="javax.servlet.http.HttpServlet">
		<service-properties>
			<entry key="alias" value="/tasklist" />
		</service-properties>
	</service>
</blueprint>

The reference tag makes blueprint search and eventually wait for a service that implements the TaskService interface and creates a bean "taskService".
The bean taskServlet instantiates the servlet class and injects the taskService. The service tag publishes the servlet as an OSGi service with the HttpServlet interface and sets a property alias.
This way of publishing a servlet is not yet standardized but is supported by the pax web whiteboard extender. This extender registers each service with interface HttpServlet with the OSGi http service. It uses the alias
property to set the path where the servlet is available.

See also: http://wiki.ops4j.org/display/paxweb/Whiteboard+Extender

Tasklist-features

The last project only installs a feature descriptor to the maven repository so we can install it easily in Karaf. The descriptor defines a feature named tasklist and the bundles to be installed from
the maven repository.

<features>

    <feature name='tasklist' version='${pom.version}'>
        <bundle>mvn:org.ops4j.pax.web/pax-web-extender-whiteboard/0.7.4</bundle>
        <bundle>mvn:net.lr.tasklist/tasklist-persistence/${pom.version}</bundle>
        <bundle>mvn:net.lr.tasklist/tasklist-ui/${pom.version}</bundle>
    </feature>

</features>

Installing the Application in Karaf

> features:addurl mvn:net.lr.tasklist/tasklist-features/1.0.0-SNAPSHOT/xml

Add the features descriptor to Karaf so it is added to the available features

> features:install example-tasklist

Install and start the tasklist feature. After this command the tasklist application should run

> list

Check that all bundles of tasklist are active. If not try to start them and check the log

Now create a text file "org.ops4j.pax.web.cfg" with the content "org.osgi.service.http.port=8080". This will tell the http service to start and use the port 8080.

Now the tasklist application should be available at http://localhost:8080/tasklist

Summary

In this tutorial we have installed Karaf and learned some commands. Then we created a small OSGi application that shows servlets, OSGi services, blueprint and the whiteboard pattern.

In the next tutorial we take a look at using Apache Camel and Apache CXF on OSGi.

Back to Karaf Tutorials

Labels

karaf karaf Delete
osgi osgi Delete
tutorial tutorial Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Oct 10, 2011

    Anonymous says:

    I made the tutorial step by step, install M2 Eclipse plugin, download the sample...

    I made the tutorial step by step, install M2 Eclipse plugin, download the samples, build with maven, etc. But when I try to add the feature URL I am gettign this error:

    karaf@root> features:addurl mvn:net.lr.tasklist/tasklist-features/1.0/xml
    Could not add Feature Repository:
    java.lang.RuntimeException: URL [mvn:net.lr.tasklist/tasklist-features/1.0/xml] could not be resolved.
    karaf@root> features:addurl mvn:net.lr.tasklist/tasklist-features/1.0/xml
    

    I can see the compiled JAR files in my local maven repository.

    1. Oct 10, 2011

      Christian Schneider says:

      Karaf uses your local maven to resolve the feature. So you have to make sure tha...

      Karaf uses your local maven to resolve the feature. So you have to make sure that karaf knows about your maven on startup. So make sure your MAVEN_HOME is set correctly. Best thing is to use the same shell to mvn install the takslist project and start karaf.