+ All Categories
Home > Technology > Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

Date post: 04-Jul-2015
Category:
Upload: mfrancis
View: 795 times
Download: 14 times
Share this document with a friend
Description:
OSGi Community Event 2014 Abstract: Experience level: Beginner Apache Karaf is a small OSGi based runtime which provides a lightweight container onto which various components and applications can be deployed. This session gives an overview over Apache Karaf, how to manage and configure Apache Karaf and how to use the comprehensive command shell that Karaf provides. By the example of a simple pet clinic application we will demonstrate how to build and deploy a web application on Apache Karaf. This talk will introduce the different deployment mechanisms available in Karaf and how to deploy bundles from the file system, command shell or via a maven repository. You will learn how to manage external dependencies via features, use the Karaf maven plugin to create features and build and configure persistence bundles for Apache Karaf. We will further give an overview, how to use Karaf in a cloud environment and how to use the Apache Cellar project to build up an Apache Karaf cluster. Speaker Bios: Torsten Frank Torsten Frank is an entrepreneur and healthcare IT expert with international experience and a strong background in product and business development. He is CEO and founder of medisite, a company that delivers highly specialized healthcare IT solutions to their customers for more than 10 years. medisite is a winner of the Federal Ministry for Economic Affairs and Energy (BMWi) “Trusted Cloud” technology competition, geared toward secure cloud computing for SMEs and the public sector. Torsten Frank holds a medical degree from the Hannover Medical School, where he also has worked for several years as a physician at the Department of Cardiothoracic Surgery after completing his medical studies in Hannover, Germany and Chicago, USA. Alexander Grzesik Alexander is the head of development of medisite Systemhaus GmbH and responsible for the development of the the PaaS+ cloud platform the clinical information system m.life and software architect for the TRESOR Project. He has 15 years of work experience in medical Software development as team leader and software architect. Expert for Software Architecture, OSGi, Java and Java EE. Alexander has been a speaker at several conferences including EclipseCon Europe and the OpenShift Community Day.
33
Apache Karaf Develop OSGi Applications with Apache Karaf
Transcript
Page 1: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

Apache Karaf

Develop OSGi Applications with Apache Karaf

Page 2: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

2

Today‘s speakers

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Torsten Frank

CEO & Founder

Physician

15+ years experience in business and product development

Healthcare IT expert

Qualified medical device consultant

Alexander Grzesik

Head of software development,Chief Software Architect

Java Expert

15+ years experience in software development

Validator for computer validation (GAMP 5)

Page 3: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

3

medisite & Cloudyle

• Specialized for over 10 years in the development of software focused on the healthcare market.

• Our products m.life® and m.care® are comprehensive healthcare solutions.

• Expanded to the cloud with the spin-off Cloudyle, with offices in Berlin, Hannover, Germany and soon in the US.

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 4: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

4

Agenda

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Run

•Files & Folders

•Start and Stop

•Command Line

Develop

•Configuration

•Logging

•Web

Deploy

•Maven

•Features

•Kar Archives

Test

•Debug

• Integration Testing

Production•Clustering

•Cloud

Page 5: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

5

Apache Karaf Highlights

Flexible Provisioning and Deployment

Application provisioning by „Features“

Lightweight and modular

Advanced Logging

Dynamic Configuration

Command Line

Remote Management

Supports different OSGi Frameworks

Lots of available features

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 6: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

6

Apache Karaf Overview

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 7: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

7

Important Files and Folders

+---bin : Startup scripts and commands

+---data : Karaf work directory

| +---cache : Installed bundles

| +---log : Default log directory

| +---tmp : Temporary files

+---deploy : Directory for file system deployment

+---etc : Configuration files

+---instances : Instance management

+---lib : Core libraries

+---system : System bundle repository

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 8: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

8

Karaf Specific Environment Variables

KARAF_HOME: the location of your Apache Karaf installation (default isfound depending where you launch the startup script).

KARAF_BASE: the location of your Apache Karaf base (default isKARAF_HOME).

KARAF_DATA: the location of your Apache Karaf data folder (default isKARAF_BASE/data).

KARAF_ETC: the location of your Apache Karaf etc folder (default isKARAF_BASE/etc).

KARAF_OPTS: extra arguments passed to the Java command line (default isnull).

KARAF_DEBUG: if 'true', enable the debug mode (default is null).

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 9: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

9

Commandline

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 10: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

10

Command Groups (Karaf 3.x)

bundle:* - Install and control bundles dev:* - Development support feature:* - Manage features instance:* - Control of multiple instances jaas:* - JAAS role and rights management log:* - log display obr:* - Interaction with OSGi Bundle Repositories scr:* - Declarative Services management service:* - Manage OSGi Services shell:* - useful helper functions (i.e. grep, more, java info) ssh:* - SSH connections system:* - OSGi framework management web:* - WAR bundle management wrapper:* - OS service installation

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 11: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

11

Remote Management

Use command line via SSH:

Configuration via: org.apache.karaf.shell.cfg

User and Role management via JAAS Configure via users.properties

org.apache.karaf.command.acl.* for command specific permissions

Default roles: viewer, manager, admin

Connect to other Karaf using ssh command

Connect to local running Karaf using the client script

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

ssh -p 38031 [email protected]

Page 12: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

12

Webconsole

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 13: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

13

Scripting

Scripting allows customizing console Variable assignment:

msg = “Welcome to PaaS+“

Lists, Mapsmap = [Jan=1 Feb=2 Mar=3]

Pipe commands($.context bundles) | grep -i PaaS+

Flow controlshell:if, shell:each

Startup scripts configured in shell.init.script

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

paasplus:refresh={

feature:repo-remove -u mvn:com.cloudyle.paasplus/$args/LATEST/xml/features;

feature:repo-add mvn:com.cloudyle.paasplus/$args/LATEST/xml/features;

feature:install $args } ;

Page 14: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

14

Custom Commands

Add custom commands from your bundles Extend org.apache.karaf.shell.console.OsgiCommandSupport

Add @org.apache.karaf.shell.commands.Command Annotation

Register command via Blueprint

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

public class StartInstanceCommand extends OsgiCommandSupport {

@Argument(index = 0, name = "key", description = "Process definition key",

required = true, multiValued = false)

String key;

@Override

protected Object doExecute() throws Exception {

final ProcessInstance instance = startProcessInstance (this.key);

System.out.println("Instance id: " + instance.getProcessInstanceId());

return null;

}

<shell:command-bundle>

<shell:command>

<shell:action class="com.cloudyle.paasplus.commands.StartInstanceCommand">

<shell:property name="runtimeService" ref="runtimeBean" />

</shell:action>

</shell:command>

</shell:command-bundle>

Page 15: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

15

Developer Commands dev:dump-create

Creates a system dump of Karaf

bundle:diag Gives details why bundle did not start

bundle:dynamic-import enable or disable the dynamic import of a given bundle

log:exception-display Displays the last occurred exception from the log

log:tail Continuously display log entries

bundle:tree-show Show bundle dependency tree

bundle:watch Automatic update from maven repo

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 16: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

16

Pet Clinic

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

PetClinic

PersistenceClinic

Service

API Provider

GUI Feature

https://github.com/Cloudyle/petclinic

JPA-Bundle

Configuration,Logging

Web App Bundle

Feature generation

Page 17: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

17

Persistence

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

https://github.com/Cloudyle/aries/tree/cloudyle

JPA APIJPA

ContainerContainerContext

ContainerBlueprint Aries JPA

Hibernate OpenJPA EclipseLink

Persistence Bundle

•Entities

•Persistence.xml

•Meta-Persistenceflag

<Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>

<bean id="persistenceUnit"class=„... " init-method="init"> <jpa:unit property="entityManagerFactory" unitname="persistence-petclinic-nosql" /></bean>

Page 18: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

18

Database Connection

Configuration via Blueprint

Simply deploy blueprint.xml into Karaf

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

<bean id="dataSource" class="org.postgresql.ds.PGPoolingDataSource"

destroy-method="close">

<property name="serverName" value="${host}:${port}/${database}"/>

<property name="user" value="${user}"/>

<property name="password" value="${password}"/>

<property name="dataSourceName" value="defaultConnectionPool"/>

<property name="initialConnections" value="${initialConnections}"/>

<property name="maxConnections" value="${maxConnections}" />

</bean>

<service interface="javax.sql.DataSource" ref="dataSource">

<service-properties>

<entry key="osgi.jndi.service.name" value="jdbc/default"/>

<entry key="datasource.name" value="postgres"/>

</service-properties>

</service>

Page 19: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

19

Logging

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

log4j

slf4j

commonslogging

Java utillogging

logback

OSGi LoggingService

log4jPax logging

etc/org.ops4j.pax.logging.cfg

Commands:

log:display

log:tail

log:set

log:get

log:log

Import-package: org.slf4j;provider=paxlogging

Page 20: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

20

Configuration

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Bundle

ConfigAdmin

Bundle Config

Global Config

Pid.cfg

config.properties

custom.properties

System Config

system.properties

Java -D

Commands:

config:list

config:edit

config:property-list

config:property-set

Page 21: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

21

Usage with Blueprint

Import configuration in blueprint:

Property Placeholders:

Managed Properties

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

<cm:property-placeholder id="property-placeholder" persistent-

id="com.cloudyle.paasplus.petclinic" update-strategy="reload">

<cm:default-properties>

<cm:property name="dataDir" value="data"/>

<cm:property name="createSampleData" value="true"/>

</cm:default-properties>

</cm:property-placeholder>

<bean id="reportHelper" class="com.cloudyle.paasplus.petclinic.impl.ReportHelper">

<property name="dataDir" value="${dataDir}"/>

</bean>

<bean id="clinicS" class="com.cloudyle.paasplus.petclinic.impl.ClinicServiceImpl">

<cm:managed-properties persistent-id=clinicServiceConfig"

update-strategy="container-managed"/>

</bean>

Page 22: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

22

Web Applications

Webapp support via Pax Web feature

Integrated Jetty etc/jetty.xml

OSGi http Service (Whiteboard pattern) Register Web application resources as OSGi services

Full Webapp Deploy war or wab

CDI support

Commands web:list, stop, start (Webapps)

http:list (Servlets)

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 23: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

23

Web Application Bundle

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Web

Ap

p B

un

dle

META-INF MANIFEST

WEB-INF

classes

web.xml

<Web-ContextPath>/petclinic

</Web-ContextPath>

<_wab>src/main/resources</_wab>

<groupId>org.apache.felix</groupId>

<artifactId>maven-bundle-plugin</artifactId>

<extensions>true</extensions>

<configuration>

<supportedProjectTypes>

<supportedProjectType>jar</supportedProjectType>

<supportedProjectType>bundle</supportedProjectType>

<supportedProjectType>war</supportedProjectType>

</supportedProjectTypes>

Page 24: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

24

Deployment

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Deployment of:

Bundles

Blueprint files (xml)

Features

Kar Archives

„plain“ jar files

Page 25: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

25

Features

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Bundle A

Bundle C

Bundle B

Config A

Feature Dependency

Feature XML

<feature name="com.cloudyle.paasplus.samples.petclinic.feature" version="0.9.0"

description="PaaS+ Sample - PetClinic - Deployment feature" resolver="(obr)"

install="auto">

<details>Pet Clinic Feature.</details>

<configfile finalname="/etc/com.cloudyle.paasplus.petclinic.cfg">

mvn:com.cloudyle.paasplus.samples/com.cloudyle.paasplus.samples.petclinic.provider/0.

9.0/cfg</configfile>

<feature>paasplus-persistence-service</feature>

<bundle start-level="75">mvn:com.cloudyle.paasplus.samples/

com.cloudyle.paasplus.samples.petclinic.persistence/0.9.0</bundle>

<bundle start-level="75">mvn:com.cloudyle.paasplus.samples/

com.cloudyle.paasplus.samples.petclinic.api/0.9.0</bundle>

</feature>

Page 26: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

26

Maven Deployment

Deploy via any Maven repository Bundles

Features

Configurations

Add additional repos to org.ops4j.pax.url.mvn.cfg

Install features or bundles

Useful Commands bundle:install, update, list

feature:install, listEclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

org.ops4j.pax.url.mvn.repositories= \

http://nexus.paasplus.com:8081/nexus/content/groups/public/, \

http://repo1.maven.org/maven2@id=central, \

repo-add mvn:com.cloudyle.paasplus/com.cloudyle.paasplus.karaf.deployment.services/

LATEST/xml/features;

feature:install paasplus-persistence-service;

bundle:install mvn:com.cloudyle.paasplus.samples/

com.cloudyle.paasplus.samples.petclinic.api;

Page 27: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

27

Karaf Maven Plugin

Generate feature.xml with project dependencies

Generate command documentation

Kar packaging

Custom Karaf distribution

Kar and feature packaging

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

<plugin>

<groupId>org.apache.karaf.tooling</groupId>

<artifactId>karaf-maven-plugin</artifactId>

<version>3.0.2</version>

<extensions>true</extensions>

</plugin>

Page 28: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

28

Integrated Development Process

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Develop

Debug, Integration Tests mvn install

bundle:install, update

Manage

Feature XML

mvn:…/..

repo-add

Page 29: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

29

JMX

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 30: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

30

Integration Testing

Automated Testing via PaxExam

JUnit tests running in Apache Karaf instance

Configure and manage Karaf via JUnit test

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

@RunWith(PaxExam.class)

public class ServiceProviderIT extends AbstractBaseTest{

@Configuration

public Option[] config(){

return combine(super.config(), new Option[] {

editConfigurationFileExtend("etc/org.apache.karaf.features.cfg", "featuresRepositories",

",mvn:com.cloudyle.paasplus:com.cloudyle.paasplus.karaf.deployment.services:xml:features"),

new KarafDistributionConfigurationFileReplacementOption("etc/org.elasticsearch.connection.cfg",

new File("src/test/resources/etc/org.elasticsearch.connection.cfg")),

features("mvn:com.cloudyle.paasplus/com.cloudyle.paasplus.karaf.deployment.services/LATEST/xml/f

eatures", "paasplus-report-service"),

vmOption("-Djava.net.preferIPv4Stack=true"), vmOption("-XX:MaxPermSize=256M") });}

@Test

public void doTest(){

Page 31: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

31

Cellar Clustering

Cluster solution for Apache Karaf

Based on Hazelcast

Support for different topologies

Synchronization of deployments and configurations

Cross-Node Event publishing

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Node

Node Node

Node

Node NodeCluster

Manager

Page 32: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

32

PaaS+ with Karaf support

Cloudyle PaaS+ allows easy creation of Karaf applications

Try out PetClinichttp://devcenter.cloudyle.com/display/public/PAAS/Pet+Clinic+Tutorial

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf

Page 33: Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

33

See you tomorrow:

Build bundles in the cloud - How CloudylePaaS+ helps creating OSGi applications in the cloud

Wednesday, October 29, 2014

14:30 to 15:05

Seminarräume 1 - 3

Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf


Recommended