cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache CXF > Distributed OSGi Greeter Demo Walkthrough
Date Mon, 26 Jul 2010 16:13:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/15/_/styles/combined.css?spaceKey=CXF&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CXF/Distributed+OSGi+Greeter+Demo+Walkthrough">Distributed
OSGi Greeter Demo Walkthrough</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davidb@apache.org">David
Bosschaert</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Move from 1.1 -&gt; 1.2<br />
    </div>
        <br/>
                         <h4>Changes (7)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >In this walkthrough all the required
bundles are installed straight from the maven release repository, so no need to check out
SVN and build anything to get started with the Greeter Demo. <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">*NOTE*:
this demo requires CXF/DOSGi 1.1 <br></td></tr>
            <tr><td class="diff-unchanged" >h3. The Greeter demo design <br>The
demo is composed of 3 bundles: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >Welcome to Apache Felix Gogo <br>g!
install http://repo1.maven.org/maven2/org/osgi/org.osgi.compendium/4.2.0/org.osgi.compendium-4.2.0.jar
<br></td></tr>
            <tr><td class="diff-changed-lines" >g! start <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://www.apache.org/dist/cxf/dosgi/1.1/cxf-dosgi-ri-singlebundle-distribution-1.1.jar</span>
<span class="diff-added-words"style="background-color: #dfd;">http://www.apache.org/dist/cxf/dosgi/1.2/cxf-dosgi-ri-singlebundle-distribution-1.2.jar</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >... some log messages may appear...
<br>g! lb <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >Now let&#39;s start up the server-side
greeter bundles. Like with the DOSGi bundle itself, I&#39;m installing these straight
from the Maven release repository. <br>{code} <br></td></tr>
            <tr><td class="diff-changed-lines" >g! start <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-interface/1.1/cxf-dosgi-ri-samples-greeter-interface-1.1.jar</span>
<span class="diff-added-words"style="background-color: #dfd;">http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-interface/1.2/cxf-dosgi-ri-samples-greeter-interface-1.2.jar</span>
<br></td></tr>
            <tr><td class="diff-changed-lines" >g! start <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-impl/1.1/cxf-dosgi-ri-samples-greeter-impl-1.1.jar</span>
<span class="diff-added-words"style="background-color: #dfd;">http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-impl/1.2/cxf-dosgi-ri-samples-greeter-impl-1.2.jar</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >... some log messages will appear
... <br>g! lb <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >Bundle id is 1 <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >osgi&gt; install <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://www.apache.org/dist/cxf/dosgi/1.1/cxf-dosgi-ri-singlebundle-distribution-1.1.jar</span>
<span class="diff-added-words"style="background-color: #dfd;">http://www.apache.org/dist/cxf/dosgi/1.2/cxf-dosgi-ri-singlebundle-distribution-1.2.jar</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >Bundle id is 2 <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code} <br></td></tr>
            <tr><td class="diff-changed-lines" >osgi&gt; install <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-interface/1.1/cxf-dosgi-ri-samples-greeter-interface-1.1.jar</span>
<span class="diff-added-words"style="background-color: #dfd;">http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-interface/1.2/cxf-dosgi-ri-samples-greeter-interface-1.2.jar</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >Bundle id is 3 <br></td></tr>
            <tr><td class="diff-changed-lines" >osgi&gt; install <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-client/1.1/cxf-dosgi-ri-samples-greeter-client-1.1.jar</span>
<span class="diff-added-words"style="background-color: #dfd;">http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-client/1.2/cxf-dosgi-ri-samples-greeter-client-1.2.jar</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >Bundle id is 4 <br>osgi&gt;
ss <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>This is a walkthrough of the Distributed OSGi Greeter Demo. It should help
users of the Distributed OSGi get started with it. </p>

<p>The greeter demo can be found in the <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/greeter"
class="external-link" rel="nofollow">samples/greeter directory</a> of the SVN code
base and implements a simple OSGi Greeter Service and a consumer to that service with a trivial
UI. <br/>
In this walkthrough all the required bundles are installed straight from the maven release
repository, so no need to check out SVN and build anything to get started with the Greeter
Demo.</p>

<h3><a name="DistributedOSGiGreeterDemoWalkthrough-TheGreeterdemodesign"></a>The
Greeter demo design</h3>
<p>The demo is composed of 3 bundles:</p>
<ul>
	<li>The Greeter Interface bundle</li>
	<li>The Greeter Service Implementation bundle</li>
	<li>The Greeter Service Consumer bundle</li>
</ul>


<p>The <b>Greeter Interface</b> bundle exports the <tt>GreeterService</tt>
interface which both other bundles depend on. This is the interface:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> <span
class="code-keyword">interface</span> GreeterService {
    Map&lt;GreetingPhrase, <span class="code-object">String</span>&gt;
greetMe(<span class="code-object">String</span> name);
    GreetingPhrase [] greetMe(GreeterData name) <span class="code-keyword">throws</span>
GreeterException;
}</pre>
</div></div>
<p>The <tt>GreeterData</tt> interface, <tt>GreetingPhase</tt>
and <tt>GreeterException</tt> classes are custom types defined in the Greeter
Interface bundle.</p>

<p>In this walkthrough, the following setup will be used:<br/>
<span class="image-wrap" style=""><img src="/confluence/download/attachments/107706/dosgi_cxf.png?version=1&amp;modificationDate=1232126904000"
style="border: 0px solid black" /></span><br/>
The client side bundles will be running in Equinox, invoking on a Distributed OSGi Service
running in Felix. Note that the choice of containers is completely trivial. Any OSGi container
that implements the Service Registry Hooks (a new feature in OSGi 4.2) can be used on either
side.</p>

<h3><a name="DistributedOSGiGreeterDemoWalkthrough-TheServerSide"></a>The
Server Side</h3>
<p>The <b>Greeter Service</b> implementation bundle provides a <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/greeter/impl/src/main/java/org/apache/cxf/dosgi/samples/greeter/impl/GreeterServiceImpl.java"
class="external-link" rel="nofollow">trivial implementation of the GreeterService interface</a>.
Additionally, it has an <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/greeter/impl/src/main/java/org/apache/cxf/dosgi/samples/greeter/impl/Activator.java"
class="external-link" rel="nofollow">Activator</a>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class Activator
<span class="code-keyword">implements</span> BundleActivator {
    <span class="code-keyword">private</span> ServiceRegistration registration;

    <span class="code-keyword">public</span> void start(BundleContext bc) <span
class="code-keyword">throws</span> Exception {
        Dictionary props = <span class="code-keyword">new</span> Hashtable();

        props.put(<span class="code-quote">"service.exported.interfaces"</span>,
<span class="code-quote">"*"</span>);
        props.put(<span class="code-quote">"service.exported.configs"</span>,
<span class="code-quote">"org.apache.cxf.ws"</span>);
        props.put(<span class="code-quote">"org.apache.cxf.ws.address"</span>,
<span class="code-quote">"http:<span class="code-comment">//localhost:9090/greeter"</span>);
</span>        
        registration = bc.registerService(GreeterService.class.getName(), <span class="code-keyword">new</span>
GreeterServiceImpl(), props);
    }

    <span class="code-keyword">public</span> void stop(BundleContext bc) <span
class="code-keyword">throws</span> Exception {
        registration.unregister();
    }
}</pre>
</div></div>
<p>Besides creating the service instance, the activator sets the additional properties
on the service that are required to make it available remotely:</p>
<ul>
	<li>The <tt>service.exported.interfaces</tt> property is set to <tt>*</tt>,
which means that all the interfaces passes to registerService should be made accessible remotely.
In this case it's just the <tt>GreeterService</tt> interface.</li>
	<li>The <tt>service.exported.configs</tt> is set to <tt>org.apache.cxf.ws</tt>,
which is a CXF specific configuration type that can be used to expose the OSGi Service as
a Web Service over SOAP/HTTP. The address of the service is specified via the <tt>org.apache.cxf.ws.address</tt>
property.</li>
</ul>


<p>Let's run the server in Felix 3.0.1. As a prerequisite it requires some of the OSGi
Compendium Specification interfaces. These don't come with the Felix download, but you can
install a bundle that contains these interfaces straight from Maven.<br/>
In this walkthrough I'm using the single-bundle distribution of CXF/DOSGi which can be installed
straight from the Maven release repository.</p>

<p>To set up my Felix environment, I'm running the following commands:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">C:\felix-framework-3.0.1&gt;

Welcome to Apache Felix Gogo
g! install http:<span class="code-comment">//repo1.maven.org/maven2/org/osgi/org.osgi.compendium/4.2.0/org.osgi.compendium-4.2.0.jar
</span>g! start http:<span class="code-comment">//www.apache.org/dist/cxf/dosgi/1.2/cxf-dosgi-ri-singlebundle-distribution-1.2.jar
</span>... some log messages may appear...
g! lb
    0|Active     |    0|org.apache.felix.framework (3.0.1)
    1|Active     |    1|org.apache.felix.bundlerepository (1.6.2)
    2|Active     |    1|org.apache.felix.gogo.command (0.6.0)
    3|Active     |    1|org.apache.felix.gogo.runtime (0.6.0)
    4|Active     |    1|org.apache.felix.gogo.shell (0.6.0)
    5|Resolved   |    1|osgi.cmpn (4.2.0.200908310645)
    6|Active     |    1|cxf-dosgi-ri-singlebundle-distribution (1.2.0)</pre>
</div></div>
<p>Some log messages may come up now.</p>

<p>Now let's start up the server-side greeter bundles. Like with the DOSGi bundle itself,
I'm installing these straight from the Maven release repository.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
g! start http:<span class="code-comment">//repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-<span
class="code-keyword">interface</span>/1.2/cxf-dosgi-ri-samples-greeter-<span class="code-keyword">interface</span>-1.2.jar
</span>g! start http:<span class="code-comment">//repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-impl/1.2/cxf-dosgi-ri-samples-greeter-impl-1.2.jar
</span>... some log messages will appear ...
g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|<span class="code-object">System</span> Bundle (3.0.1)
    1|Active     |    1|Apache Felix Bundle Repository (1.6.2)
    2|Active     |    1|Apache Felix Gogo Command (0.6.0)
    3|Active     |    1|Apache Felix Gogo <span class="code-object">Runtime</span>
(0.6.0)
    4|Active     |    1|Apache Felix Gogo Shell (0.6.0)
    5|Resolved   |    1|osgi.cmpn (4.2.0.200908310645)
    6|Active     |    1|Distributed OSGi Distribution Software Single-Bundle Distribution
    7|Active     |    1|CXF Distributed OSGi Greeter Demo Interface Bundle
    8|Active     |    1|CXF Distributed OSGi Greeter Demo Service Implementation Bundle</pre>
</div></div>

<p>At the end of the log messages you will see one appear that says: <br/>
<tt>INFO: TopologyManager: export sucessful Endpoints:[Endpoint Desctiption for ServiceReference
<a href="/confluence/pages/createpage.action?spaceKey=CXF&amp;title=org.apache.cxf.dosgi.samples.greeter.GreeterService&amp;linkCreation=true&amp;fromPageId=107706"
class="createlink">org.apache.cxf.dosgi.samples.greeter.GreeterService</a></tt><br/>
This means that the service is successfully exposed remotely, and you can verify this by requesting
the WSDL:</p>

<p><span class="image-wrap" style=""><img src="/confluence/download/attachments/107706/greeter_wsdl.jpg?version=2&amp;modificationDate=1253008429000"
style="border: 0px solid black" /></span></p>

<p>This WSDL was dynamically generated from the exposed <tt>GreeterService</tt>
Java interface. Under the hood, the Aegis data binding is used for this.</p>

<h3><a name="DistributedOSGiGreeterDemoWalkthrough-TheServiceConsumerside."></a>The
Service Consumer side.</h3>
<p>The Service Consumer Java code contains no specific Distribution-related elements.
It's simply an <tt>Activator</tt> that creates a <tt>ServiceTracker</tt>
listening for services that implement the <tt>GreeterService</tt> interface. When
the service becomes available it opens a little GUI window that asks you for an argument that
can be sent to the Greeter service. <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/greeter/client/src/main/java/org/apache/cxf/dosgi/samples/greeter/client/Activator.java"
class="external-link" rel="nofollow">See here for the actual code</a></p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>Note that in many cases using
an OSGi Component Framework such as Spring-DM, iPojo or OSGi DS is highly recommeded when
writing OSGi Service Consumers. OSGi Services are highly dynamic in nature. They can come
and go. Using a component framework will generally save you from writing <tt>ServiceTracker</tt>
code as the framework will generally provide the service consumer with a reference to the
service via injection.</td></tr></table></div>

<p>So the remote service is simply looked up the normal way, via the OSGi Service Registry.
<br/>
<b>How does it get there?</b> The fact that a lookup on a service is done internally
triggers a <b>Service Registry Hook</b>. This will go out to any registered Distributed
OSGi Discovery implementations and query them for any matching services. <br/>
However, <b>in our setup we haven't yet registered a Discovery implementation</b>.
There is an alternative, more static way to provide discovery type information, in case this
info is not available via discovery. It can be specified in a <tt>OSGI-INF/remote-service/*.xml</tt>
file. This the content of the Greeter Service Consumer <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/greeter/client/src/main/resources/OSGI-INF/remote-service/remote-services.xml"
class="external-link" rel="nofollow">remote-services.xml</a> file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;endpoint-descriptions xmlns=<span
class="code-quote">"http://www.osgi.org/xmlns/rsa/v1.0.0"</span>&gt;</span>
  <span class="code-tag">&lt;endpoint-description&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"objectClass"</span>&gt;</span>
      <span class="code-tag">&lt;array&gt;</span>
        <span class="code-tag">&lt;value&gt;</span>org.apache.cxf.dosgi.samples.greeter.GreeterService<span
class="code-tag">&lt;/value&gt;</span>
      <span class="code-tag">&lt;/array&gt;</span>
    <span class="code-tag">&lt;/property&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"endpoint.id"</span>&gt;</span>http://localhost:9090/greeter<span
class="code-tag">&lt;/property&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"service.imported.configs"</span>&gt;</span>org.apache.cxf.ws<span
class="code-tag">&lt;/property&gt;</span>
  <span class="code-tag">&lt;/endpoint-description&gt;</span>
<span class="code-tag">&lt;/endpoint-descriptions&gt;</span></pre>
</div></div>

<p>Let's run the consumer in Equinox, so that we have the bundles running in Equinox
talking to a remoted service running in Felix!<br/>
As with Felix, we will have to load the bundle with the OSGi compendium interfaces. There's
one that ships with Equinox.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
/eclipse&gt; java -jar plugins/org.eclipse.osgi_3.6.0.v20100517.jar -console
osgi&gt; install install file:plugins/org.eclipse.osgi.services_3.2.100.v20100503.jar
Bundle id is 1

osgi&gt; install http:<span class="code-comment">//www.apache.org/dist/cxf/dosgi/1.2/cxf-dosgi-ri-singlebundle-distribution-1.2.jar
</span>Bundle id is 2

osgi&gt; start 2
... some log messages may appear...
</pre>
</div></div>
<p>Some logging messages may appear. You can also automatically load the DOSGi bundles
by appending the target/equinox.config.ini.append to you equinox config.ini file.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
osgi&gt; install http:<span class="code-comment">//repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-<span
class="code-keyword">interface</span>/1.2/cxf-dosgi-ri-samples-greeter-<span class="code-keyword">interface</span>-1.2.jar
</span>Bundle id is 3
osgi&gt; install http:<span class="code-comment">//repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-greeter-client/1.2/cxf-dosgi-ri-samples-greeter-client-1.2.jar
</span>Bundle id is 4
osgi&gt; ss

Framework is launched.

id      State       Bundle
0       ACTIVE      org.eclipse.osgi_3.6.0.v20100517
1       RESOLVED    org.eclipse.osgi.services_3.2.100.v20100503
2       ACTIVE      cxf-dosgi-ri-singlebundle-distribution
3       INSTALLED   cxf-dosgi-ri-samples-greeter-<span class="code-keyword">interface</span>
4       INSTALLED   cxf-dosgi-ri-samples-greeter-client

osgi&gt; start 4</pre>
</div></div>
<p>After a few moments the following window appears:</p>

<p><span class="image-wrap" style=""><img src="/confluence/download/attachments/107706/greeterui2.jpg?version=1&amp;modificationDate=1253166749000"
style="border: 0px solid black" /></span></p>

<p>The window appears once the ServiceTracker in the consumer has received a callback
that the <tt>GreeterService</tt> has been found. The value entered will used to
invoke the (remote) OSGi service, with a call like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Map&lt;GreetingPhrase, <span class="code-object">String</span>&gt;
result = greeterService.greetMe(<span class="code-quote">"foobar"</span>);</pre>
</div></div>
<p>I can see that the invocation has been received by the service implementation as
in the Felix window the following appears.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">-&gt; Invoking: greetMe(foobar)</pre>
</div></div>
<p>In the Equinox window I can see the response:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">osgi&gt; greetMe(<span class="code-quote">"foobar"</span>)
returns:
  Hola foobar
  Bonjour foobar
  Hoi foobar
  Hello foobar</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CXF/Distributed+OSGi+Greeter+Demo+Walkthrough">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=107706&revisedVersion=30&originalVersion=29">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CXF/Distributed+OSGi+Greeter+Demo+Walkthrough?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message