com
CONTENTS INCLUDE:
n
About ServiceMix 4.0
ServiceMix 4.0 Architecture
Getting Started with
ServiceMix 4.0
n
n
Configuration of ServiceMix 4.0 Components
n
Routing in ServiceMix 4.0
n
ServiceMix and Web services
n
Threads Coordination and more... By Jos Dirksen
This DZone Refcard will provide an overview of the core OSGi runtime
elements of ServiceMix 4.0 and will show you how to use ServiceMix runs on an OSGi based kernel, but what is
ServiceMix 4 by providing example configurations. OSGi? In short an OSGi container provides a service
based in-VM platform on which you can deploy
www.dzone.com
ActiveMQ: ActiveMQ, another Apache project, is the message n Hot tips & examples
ServiceMix 4 configuration is mostly done through Spring XML ServiceMix provides a number of different options which you
files supported by XML schemas for easy code completion. can use to deploy artifacts. In this section we’ll look at these
Let’s look at two simple examples. The first one uses the File options, and show you how to use these.
Binding component to poll a directory and the second one
exposes a Web service using ServiceMix’s CXF support. ServiceMix 4, deployment options
Name Description
<beans xmlns:file=”http://servicemix.Apache.org/file/1.0”
OSGi Bundles ServiceMix 4 is built around OSGi and ServiceMix 4 also allows you to
xmlns:dzone=”http://servicemix.org/dzone/”>
deploy your configurations as an OSGi bundle with all the advantages
<file:poller service=”foo:filePoller”
OSGi provides.
endpoint=”filePoller”
targetService=”foo:fileSender” Spring XML files ServiceMix 4 support plain Spring XML files.
file=”inbox” />
</beans> JBI artifacts You can also deploy artifacts following the JBI standard (service
assemblies and service units) to ServiceMix 4.
In this listing you can see that we define a poller. A poller Feature This is a Karaf specific way for installing applications. It will install the
is one of the standard components that is provided by descriptors necessary OSGi bundles and will add configuration defaults. This is mostly
used to install core parts of the ServiceMix distribution.
ServiceMix’s file-binding-component. If we deploy this
configuration to ServiceMix, ServiceMix will start polling the OSGi bundle deployment
inbox directory for files. If it finds one, the file will be sent to The easiest way to create an OSGi based ServiceMix bundle
the specified targetService. is by using Maven 2. To create a bundle you need to take a
couple of simple steps. The first one is adding the maven- <manifest>
bundle-plugin to your pom.xml file. This is shown in the Bundle-Version = 1.0.0
Bundle-Name = Dzone :: Dzone test application
following code fragment. Bundle-SymbolicName = dzone.refcards.test
Bundle-Description = An example for servicemix refcard
Bundle-Vendor = jos.dirksen@gmail.com
... Require-Bundle = servicemix-file, servicemix-eip
<dependencies> </manifest>
<dependency>
<groupId>org.Apache.felix</groupId>
<artifactId>org.osgi.core</name>
<version>1.0.0</version> Using a manifest configuration element allows you to specify
</dependency>
... how your application is registered in ServiceMix.
</dependencies>
... JBI artifacts deployment
<build>
<plugins> If you’ve already invested in JBI based applications, you
<plugin>
<groupId>org.Apache.felix</groupId>
can still use ServiceMix 4 to run them in. Just deploy your
<artifactId>maven-bundle-plugin</artifactId> Service Assembly (SA) in the ServiceMix deploy directory and
<configuration>
<instructions> ServiceMix will deploy your application.
<Bundle-SymbolicName>${pom.artifactId}</Bundle
SymbolicName> Feature descriptor based deployment
<Import-Package>*,org.Apache.camel.osgi</Import-Package>
<Private-Package>org.Apache.servicemix.examples.camel</ If you’ve got an application which contains many bundles and
Private-Package>
</instructions> that requires additional configuration you can use a feature
</configuration> to easily manage this. A feature contains a set of bundles and
</plugin>
</plugins> configuration which can be easily installed from the ServiceMix
</build>
...
console. The following listing shows the feature descriptor of
the nmr component.
The important part here is the instructions section. This
determines how the plugin packages your project. For more <features>
<feature name=”nmr” version=”1.0.0>
information on these settings see the maven OSGi bundle <bundle>mvn:org.Apache.servicemix.document/org.Apache.
servicemix.document/1.0.0</bundle>
plugin page at http://cwiki.Apache.org/FELIX/Apache- <bundle>mvn:org.Apache.servicemix.nmr/org.Apache.servicemix.
felixmaven-bundle-plugin-bnd.html. nmr.api/1.0.0</bundle>
<bundle>mvn:org.Apache.servicemix.nmr/org.Apache.servicemix.
nmr.core/1.0.0</bundle>
The next step is to make sure your project is bundled as a <bundle>mvn:org.Apache.servicemix.nmr/org.Apache.servicemix.
OSGi bundle. You do this by setting the <packaging> element nmr.osgi/1.0.0</bundle>
<bundle>mvn:org.Apache.servicemix.nmr/org.Apache.servicemix.
in your pom.xml to bundle. nmr.spring/1.0.0</bundle>
<bundle>mvn:org.Apache.servicemix.nmr/org.Apache.servicemix.
Now you can use mvn install to create an OSGi bundle, nmr.commands/1.0.0</bundle>
<bundle>mvn:org.Apache.servicemix.nmr/org.Apache.servicemix.
which you can copy to the deploy directory of ServiceMix and nmr.management/1.0.0</bundle>
</feature>
your bundle will be installed. If you use Spring to configure </features>
your application, make sure the Spring configuration files are
located in the META-INF/spring directory. That way the Spring If you want to install this feature you can just type features/
application context will be automatically created based on install nmr from the ServiceMix console.
these files.
Routing in ServiceMix 4.0
If you don’t want to do this by hand you can also use a Maven
archetype. ServiceMix provides a set of archetypes you can
For routing in ServiceMix you’ve got two options:
use. A good starting point for a project is the Camel OSGi
archetype which you can use by executing the following • EIP: ServiceMix provides a JBI component that
following Maven command: implements a number of Enterprise Integration Patterns.
• Camel: You can use Camel routes in ServiceMix. Camel
mvn archetype:create -DarchetypeGroupId=org.Apache.servicemix.tooling
-DarchetypeArtifactId=servicemix-osgi-camel-archetype provides the most flexible and exhaustive routing options
-DarchetypeVersion=4.0.0.2-fuse for ServiceMix
-DgroupId=com.yourcompany -DartifactId=camel-router
-DremoteRepositories=http://repo.fusesource.com/maven2/
EIP Component Routing
There are many other archetypes available. For an overview of This routing is provided by the EIP component. To check
the available archetypes see: whether this is installed in your ServiceMix runtime you can
http://repo.fusesource.com/maven2/org/Apache/servicemix/tooling/ execute features/list from the ServiceMix commandline. This
will show you a list of installed features. If you see [installed]
Spring XML Files Deployment
[ 2009.01] servicemix-eip the component is installed. If
It’s also possible to deploy Spring files without OSGi. Just drop
it shows uninstalled instead of installed, you can use the
a Spring file into the deploy directory. There are two points to
features/install servicemix-eip to install this component.
take into account. First, you need to add the following to your
You can now use this router using a simple XML file:
Spring configuration file:
<eip:static-routing-slip service=”test:routingSlip”
<bean class=”org.Apache.servicemix.common.osgi.EndpointExporter” /> endpoint=”endpoint”>
<eip:targets>
This will register the endpoints you’ve configured in your <eip:exchange-target service=”test:echo” />
<eip:exchange-target service=”test:echo” />
Spring file. The next element is optional but is good practice </eip:targets>
</eip:static-routing-slip>
to add:
When installed this component provides the following routing In these two listings you can see how we can easily integrate
options (this information is also available in the XSD of this the Camel routes with the other components from ServiceMix.
component): We use the nmr prefix to tell Camel to send the message to
the NMR. The other service, which can be seperately deployed
XML Element Description
will then pick-up this message since it’s also configured to
async-bridge The async bridge pattern is used to bridge an In-Out exchange with
two In-Only (or Robust-In-Only) exchanges. This pattern is the opposite
listen to a nmr prefixed service.
of the pipeline.
content-basedrouter Component that can be used for content based routing of the Now let’s look at two listings that use Camel’s Java based
message. You can configure this component with a set of predicates
which define how the message is routed.
DSL to configure the routes. For this we need a small XML
file describing where the routes can be found, and a Java file
content-enricher A content enricher can be used to add extra information to the
message from a different source. which contains the routing.
message-filter With a message filter you specify a set of predicates which determine
whether to process the message or not.
Camel Java configuration - Listing 1: Spring configuration
pipeline The pipeline component is a bridge between an In-Only (or Robust-In-
Only) MEP and an In-Out MEP. This is the opposite of the async bridge. <beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
resequencer A resequencer can be used to re-order a set of incoming messages xsi:schemaLocation=”
before passing them on in a the new order. http://www.springframework.org/schema/beans http://www.
springframework.org/schema/
split-aggregator A split aggregator is used to reassemble messages that have been split beans/spring-beans-2.0.xsd
by a splitter. http://activemq.Apache.org/camel/schema/spring
http://activemq.Apache.org/camel/schema/spring/camel-spring.xsd “>
static-recipient-list A static recipient list will forward the incoming message to a set of
predefined destinations. <import resource=”classpath:org/Apache/servicemix/camel/nmr/camel-
nmr.xml” />
static-routing-slip The static routing slip routes a message through a set of services. It
<camelContext xmlns=”http://activemq.Apache.org/camel/
uses the result of the first invocation as input for the next.
schema/spring”>
wire-tap The wire-tap will copy and forward a message to the specified <package>dzone.refcards.camel.routes</package>
destination. </camelContext>
</beans>
xpath-splitter This splitter uses an xpath expression to split an incoming message in
multiple parts. Camel Java configuration - Listing 2: Java route
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”> Support for Web services is an important feature for an ESB.
<import resource=”classpath:org/Apache/servicemix/camel/nmr/ ServiceMix uses the CXF project for this. Since CXF is also
camel-nmr.xml” />
<camelContext xmlns=”http://camel.Apache.org/schema/ completely spring based, using CXF to deploy Web services is
spring”>
<route> very easy.
<from uri=”ftp://gertv@localhost/testfile?password
=secret”/> Hosting Web services
<to uri=”nmr:IncomingOrders”/>
</route> When you want to expose a service as a webservice you can
</beans> easily do this using CXF. Just create a CXF OSGi bundle using
Camel XML configuration - Listing 2: Target service the archetype: servicemix-osgicxf-code-first-archetype. This
<beans xmlns:file=”http://servicemix.Apache.org/file/1.0” will create an OSGi and CXF enabled maven project which you
xmlns:dzone=”http://servicemix.org/dzone/”>
<import resource=”classpath:org/Apache/servicemix/camel/nmr/ can use to develop webservices. Now just edit the src/main/
camel-nmr.xml” /> resources/META-INF/spring/beans.xml file and after you’ve
<file:sender service=”nmr:IncomingOrders”
directory=”file:target/pollerFiles” /> run the mvn install command you can deploy the bundle to
</beans>
ServiceMix. The following listing shows such an example. This
will create a Web service and host it on You can also consume a Web service using Camel. For more
http://localhost:8080/cfx/HelloDzone. information on how you can configure the Camel route for
this look at the Camel CXF integration section of the Camel
CXF Host Web service example using CXF
website: http://camel.Apache.org/cxf.html.
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:jaxws=”http://cxf.Apache.org/jaxws” For Web services ServiceMix provides the following useful
xsi:schemaLocation=”
http://www.springframework.org/schema/beans archetypes:
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.Apache.org/jaxws http://cxf.Apache.org/schemas/jaxws. Name Description
xsd”>
servicemix-cxf-bc-service-unit Create a maven project which uses the JBI CXF binding
<import resource=”classpath:META-INF/cxf/cxf.xml” /> component.
<import resource=”classpath:META-INF/cxf/cxf-extension-soap.
xml” />
servicemix-cxf-se-service-unit Create a maven project which uses the JBI CXF service
<import resource=”classpath:META-INF/cxf/cxf-extension-http.
engine.
xml” />
<import resource=”classpath:META-INF/cxf/osgi/cxf-extension- servicemix-cxf-se-wsdl- Create a maven project which uses the JBI CXF service
osgi.xml” /> firstservice-unit engine. This project is based on WSDL first development.
<jaxws:endpoint id=”helloDZone”
implementor=”dzone.examples.ws.HelloDZoneImpl”
address=”/HelloDzone”/> servicemix-osgi-cxf-code- Create a maven project which uses CXF and OSGi together.
</beans> firstarchetype This project is based on code first development.
servicemix-osgi-cxf-wsdl- Create a maven project which uses CXF and OSGi together.
In the previous example we hoseted a Web service which could firstarchetype This project is based on wsdl first development.
be called from outside the container. You can also configure
CXF to host the Web service internally by prefixing the address
with nmr. That way you can easily expose JAX-WS annotated Servicemix Components
java beans to the other services inside the ESB. The following
example shows this: Besides integration with Web services through CXF, ServiceMix
provides a lot of components you can use out of the box to
CXF Host Web service internally
integrate with various other standards and technologies. In this
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
section we’ll give an overview of these components. This list is
xmlns:jaxws=”http://cxf.apache.org/jaxws” based on the 2009.1 versions. Most of this information can also
xsi:schemaLocation=”
http://www.springframework.org/schema/beans be found in the XML schemas of these components.
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws. ServiceMix Components
xsd”>
<import resource=”classpath:META-INF/cxf/cxf.xml” /> XML Element Description
<import resource=”classpath:META-INF/cxf/cxf-extension-soap.
xml” /> ServiceMix Bean
<import resource=”classpath:META-INF/cxf/transport/nmr/cxf-
transportnmr.xml” /> Endpoint Allows you to define a simple bean that can receive and send message
<jaxws:endpoint id=”helloDzone” exchanges.
implementor=”dzone.examples.ws.HelloDZoneImpl”
ServiceMix File
address=”nmr:helloDZone” />
</beans> Poller A polling endpoint that looks for a file or files in a directory and sends the
files to a target service. You can configure various options on this endpoint
You can also host a Web services using the servicemix-cxf-bc such as archiving, filters, use of subdirectories etc.
component. Sender An endpoint that receives messages from the NMR and writes them to a
specific file or directory.
Host Web service using the servicemix-cxf-bc component ServiceMix CXF Binding Component
<beans xmlns:cxfbc=”http://servicemix.Apache.org/cxfbc/1.0” consumer A consumer endpoint that is capable of using SOAP/HTTP or SOAP/JMS.
xmlns:dzone=”http://dzone.org/refcard/example”>
Provider A provider endpoint that is capable of exposing SOAP/HTTP or SOAP/JMS
<cxfbc:consumer wsdl=”classpath:dzone-example.wsdl” services.
targetService=”dzone:ExampleService”
targetInterface=”dzone:Example”/> ServiceMix CXF Service Engine
</beans>
Endpoint With the Drools Endpoint you can use a drools rule set as a service or
as a router.
Consuming Web services
ServiceMix FTP
Consuming Web services in ServiceMix is just as easy.
Poller This endpoint can be used to poll an FTP directory for files, download
ServiceMix provides two different options for this. You can use them and send them to a service.
Camel or use the servicemix-cxf-bc component: Sender With a sender endpoint you can store a message on an FTP server.
ServiceMix HTTP
Consume Web servicemix using the servicemix-cxf-bc component
Consumer Plain HTTP consumer endpoint. This endpoint can be used to handle
<beans xmlns:cxfbc=”http://servicemix.Apache.org/cxfbc/1.0”
plain HTTP request (without SOAP) or to be able to process the request
xmlns:dzone=”http://dzone.org/refcard/example”>
in a non standard way.
<cxfbc:provider wsdl=”classpath:target-service.wsdl”
locationURI=”http://webservice.com/Service” Provider A plain HTTP provider. This type of endpoint can be used to send non-
endpoint=”ServicePort” SOAP requests to HTTP endpoints.
service=”dzone:ServicePortService”/>
</beans> Soap-Consumer An HTTP consumer endpoint that is optimized to work with SOAP
messages.
With this configuration you can consume a Web service which Soap-Provider An HTTP provider endpoint that is optimized to work with SOAP
messages.
is located at http://webservice.com/Service and which
ServiceMix JMS
is defined by the WSDL file target-service.wsdl. Other
Consumer An endpoint that can receive messages from a JMS broker.
services can use this component by making a call to the
Provider An endpoint that can send messages to a JMS broker.
dzone:ServicePortService.
Soap-Consumer A JMS consumer that is optimized to work with SOAP messages. ServiceMix SMPP
Soap-Provider A JMS provider that is optimized to work with SOAP messages. Consumer A polling component which bind with jSMPP and receive SMPP
messages and sends the SMPPs into the NMR as messages.
JCA-Consumer A JMS consumer that uses JCA to connect to the JMS broker.
Provider A provider component receives XML message from the NMR and
ServiceMix Mail converts into SMPP packet and sends it to SMPP server.
Poller An endpoint which can be used to retrieve messages. ServiceMix SNMP
Sender An endpoint which you can use to send messages. Poller With this poller you can receive SNMP events by using the SNMP4J
library.
ServiceMix OSWorkflow
ServiceMix Validation
Endpoint This endpoint can be used to start an OSWorkflow proces.
Endpoint With this endpoint you can provide schema validation of documents
ServiceMix Quartz using JAXP 1.3 and XMLSchema or RelaxNG.
Endpoint The Quartz endpoint can be used to fire messages into the NMR at ServiceMix-VFS
specific intervals.
Poller An polling endpoint that looks for a file or files in a virtual file system
ServiceMix Saxon (based on Apache commons-vfs) and sends the files to a target service.
XSLT With the XSLT endpoint you can apply an XSLT transformation to the Sender An endpoint which receives messages from the NMR and writes the
received message. message to the virtual file system.
Proxy The proxy component allows you to transform an incoming message ServiceMix-wsn2005
and send it to an endpoint. You can also configure a transformation that
needs to be applied to the result of that invocation. Create-pull- Lets you create a WS-Notification pull point that can be used by a
point requester to retrieve accumulated notification messages.
XQuery The XQuery endpoint can be used to apply a selected XQuery to the
input document. Publisher Sends messages to a specific topic.
ServiceMix Scripting Register- An endpoint that can be used by publishers to register themselves.
publisher
Endpoint With the scripting endpoint you can create a service which is
implemented using a scripting language. The following languages are Subscribe Lets you create subscriptions to a specific topic using the WSNotification
supported: Groovy, JRuby, Rhino JavaScript specification.
A BOUT t h e A u t h o r RECOMME N D E D B o o k
Jos Dirksen is a software architect for Atos Origin, Open-Source ESBs in Action
where he has been the architect for a number of large describes how to use ESBs in real-
integration projects over the last couple of years. Jos has world situations. You will learn how
worked with various integration products, commercial and the various features of an ESB such
open source, for the last five years. He co-authored the as transformation, routing, security,
book Open Source ESBs in Action, and regularly presents connectivity, and more can be
on topics ranging from enterprise integration patterns implemented on the example of two
to JavaFX and OSGi, at such conferences as Devoxx and open-source ESB implementations:
JavaOne. Mule and ServiceMix.
BUY NOW
books.dzone.com/books/opensource-esb
Bro
ugh
t to
you
by...
Professional Cheat Sheets You Can Trust
tt erns “Exactly what busy developers need:
n Pa
ld
ona
sig son
McD
simple, short, and to the point.”
De
Ja
By
#8
ired
Insp e
by th
GoF ller con
tinu
ed
ndle
r doe
sn’t
have
to
in tab
Cha d ultip ecific o should cep pat
this if the m up the
man
n M
an ac
z.co
n
sp s ents
be a ject ime. d is see passed
Download Now
Com reter of ob at runt handle plem ks to de to
...
n
uest som tion proces e no m
Itera tor ore req
n A g in metho
d
cep
dm ndlin
e e ar
tho e to ptio th
Exce ion is sm to ha up th tered or
e ca
n
Ob
se Me S renc ral
Refcardz.com
plate RN refe listed in mp
le ce pt ni
echa n pa ssed co un avio
Beh
TTE
n
ex
is en
ick
BIRT Windows Powershell
am
Tem Exa he .
A s has ack. W tion uest to ject
NP a qu s, a ct- cep Ob
it
n
st q
IG e s e rn b je call e ex e re
vid patt able O le th nd th
DES
! V is
pro s, hand s to ha
UT ard ign us ram .
des diag
JSF 2.0 Dependency Injection with EJB 3
ct
ABO refc oF) f Re le obje
erns ts o lass exa
mp oke
r
Patt ur (G lemen es c Inv
arz
n F o d rl d h
Des
ig go
f s: E inclu al w
o suc
Th is G a n
l 23 sign Pa
tt e rn e rn
patt , and a
re je ts
t ob mentin
c g AN
D
Adobe AIR Netbeans IDE JavaEditor
c
a h c M M
ef
in c u
orig kD
e
re.
Ea tion ons
tr ple CO ma
nd
boo Softwa rma to c their im om
BPM&BPMN Getting Started with Eclipse
re R
( low
e x p o n la rg cute is al ch
ati an b rm ts. +exe . Th
bject nship
s su
Cre y c fo je c an o
t th
e
ed
to ob ms, d as ed rela
tio
Get
DZone, Inc.
allo traditi
to m betwe
s.
ctu twe sed
uest
req ndled in nt or
der
Stru s be s: U
s can sa varia n.
cture ilitie that
late e ha . or in catio
stru ttern
l Pa d respo
nsib
nsh
ips
psu
Enca quest
re
to b llbacks
and
ca
nalit
y.
riant
times
ling
the
invo
ISBN-13: 978-1-934238-65-3
av
Beh nships
io ra
,a n
ob je c t re
la ti o
a n b e
P ur pose the
as q
ue uing
callb
ack
fu
b
nctio led at va
nd
e ha eded. m the
ob ject hand
ro
pro
nous nality
ing
cess be
to
ed
140 Preston Executive Dr. ISBN-10: 1-934238-65-1
at c
ed to fro y ne
tio You ne s need
ne
sts is couple
d ynch nctio
rela with s th st que
n
e th
e as the fu
itho
ut an is
eals
it
ship
de
Suite 100
Reque y of re ld be litat pattern ssing w tation articul
ar
e: D tion or faci
e.
50795
Use
n
A hist shou d ce en p
rela ed to mman for pro implem ents its ting.
t S cop runtim s s to type Whe
n
e invo
ker
el y us e co
n
ue al le m pec
jec at cla Pro Th w id th
are utilizing a job q of the
ue ac tu
d im
p
ue is
ex
Ob ged with
n
C ues ueue e que
han eals me.
Cary, NC 27513
y dge enq
que s. B en to th
e c : D P roxy Job orithm be giv knowle that is terface
b e ti
cop
g n ct
pile r S in
rato er mp
le of al ed ca to have d obje of the
ss S at com Deco serv Exa ut an nes
.com
Abst tory
C C
Fac
B
State
t
pte
r eigh y
teg
Ada Flyw Stra od
919.678.0300
z
Meth
more than 3.3 million software developers, architects and decision
S S r B
w. d
e rp rete plate
B ridg Inte Tem
S B B
er tor or
ww
C B r B
“DZone is a developer’s
AIN
O
dream,” says PC Magazine.
FR
ES
Sponsorship Opportunities
CH ace
>>
terf r
<<in andle
H st ( ) sales@dzone.com
Copyright © 2009 DZone, Inc. All rights reserved.
Clie
nt
No part of this publication
dle
r2
d lere
que
may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical,
+ha
n
Version 1.0
Han
photocopying, or otherwise, without prior written permission Cof the
oncr
ete publisher.
que
st ( ) Reference:
s
re
ndle
+ha
ern
1 ki ng
ler by lin .c o
m
and uest one
teH req z
cre () le a w.d
Con uest hand | ww