Post on 04-Jul-2015
description
Apache Karaf
Develop OSGi Applications with Apache Karaf
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)
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
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
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
6
Apache Karaf Overview
Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf
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
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
9
Commandline
Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf
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
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 karaf@karaf-mydomain.paasplus.com
12
Webconsole
Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf
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 } ;
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>
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
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
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>
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>
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
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
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>
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
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>
24
Deployment
Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf
Deployment of:
Bundles
Blueprint files (xml)
Features
Kar Archives
„plain“ jar files
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>
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;
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>
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
29
JMX
Eclipsecon Europe 2014 Develop OSGi Applications with Apache Karaf
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(){
…
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
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
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