cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache CXF > DOSGi Spring-DM Demo page
Date Fri, 04 Dec 2009 14:59:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/11/_/styles/combined.css?spaceKey=CXF&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CXF/DOSGi+Spring-DM+Demo+page">DOSGi
Spring-DM Demo page</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davidb@apache.org">David
Bosschaert</a>
    </h4>
     Changing to version 1.1
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     Changing to version 1.1<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <p>This page describes the CXF Distributed OSGi with Spring-DM demo.</p>

<p>The Spring-DM demo uses Spring-DM to create a remoted OSGi service from a Spring
Bean. The consumer side uses Spring-DM to create a consumer to a remote OSGi service. By using
Spring-DM you don't need to write code to interact with the OSGi Service Registry. That's
all handled through injection which hugely simplifies the code. Besides, you can use all the
features of Spring together with OSGi.</p>

<p>It uses the CXF/DOSGi multi bundle distribution since that provides Spring-DM as
a dependency. By installing the multi bundle distribution, you have all the prerequisites
to run this demo. See <a href="/confluence/display/CXF/DOSGi+Multi+Bundle+Setup" title="DOSGi
Multi Bundle Setup">here for instructions on installing the multi bundle distribution</a>.</p>
<h2><a name="DOSGiSpring-DMDemopage-Demodesign"></a>Demo design</h2>
<p>From a high level the demo is very similar to the greeter demo. It comprises of 3
bundles:</p>
<ul>
	<li>The demo interface bundle providing the Dinner Service interface.</li>
	<li>The Dinner Service implementation bundle.</li>
	<li>The Dinner Service consumer bundle.</li>
</ul>


<p><img src="/confluence/download/attachments/119322/dosgi_cxf_springdm.png" align="absmiddle"
border="0" /></p>

<p>The <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/spring_dm/interface/src/main/java/org/apache/cxf/dosgi/samples/springdm/DinnerService.java"
rel="nofollow">Dinner Service interface</a> is as follows:</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> DinnerService {
    List&lt;Restaurant&gt; findRestaurants(<span class="code-object">String</span>
searchQuery);
}</pre>
</div></div>

<h2><a name="DOSGiSpring-DMDemopage-TheDinnerServiceProvider"></a>The Dinner
Service Provider</h2>

<p>The service implementation provides a <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/spring_dm/impl/src/main/java/org/apache/cxf/dosgi/samples/springdm/impl/DinnerServiceImpl.java"
rel="nofollow">simplistic implementation of the DinnerService interface</a> which
is instantiated via Spring as a bean. </p>

<p>The interesting bit is in the <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/spring_dm/impl/src/main/resources/META-INF/spring/spring.xml"
rel="nofollow"><tt>META-INF/spring/spring.xml</tt></a> file. This file
creates the DinnerServiceImpl bean and registers it with the OSGi Service Registry. It also
sets the <tt>osgi.remote.interfaces</tt> property on the service to mark is as
suitable for remoting. Here are the important parts of the <tt>spring.xml</tt>
file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;beans <span class="code-keyword">xmlns:osgi</span>=<span
class="code-quote">"..."</span>&gt;</span>
  <span class="code-tag">&lt;osgi:service interface=<span class="code-quote">"org.apache.cxf.dosgi.samples.springdm.DinnerService"</span>&gt;</span>
    <span class="code-tag">&lt;osgi:service-properties&gt;</span>
      <span class="code-tag">&lt;entry key=<span class="code-quote">"osgi.remote.interfaces"</span>
value=<span class="code-quote">"*"</span> /&gt;</span>
    <span class="code-tag">&lt;/osgi:service-properties&gt;</span>
    
    <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.cxf.dosgi.samples.springdm.impl.DinnerServiceImpl"</span>
/&gt;</span>
  <span class="code-tag">&lt;/osgi:service&gt;</span>                
     
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>
<p>Unlike in the Greeter demo <tt>osgi.remote.configuration...</tt> properties
are not set in the configuration, this means that the service is exposed on the default location
of <tt><a href="http://localhost:9000/org/apache/cxf/dosgi/samples/springdm/DinnerService"
rel="nofollow">http://localhost:9000/org/apache/cxf/dosgi/samples/springdm/DinnerService</a></tt>.
The default location is based on the interface name of the service being remoted.</p>

<p>In this example, the bundles are installed in Felix.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">-&gt; start http:<span class="code-comment">//repo2.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-spring-dm-<span
class="code-keyword">interface</span>/1.1/cxf-dosgi-ri-samples-spring-dm-<span
class="code-keyword">interface</span>-1.1.jar
</span>-&gt; start http:<span class="code-comment">//repo2.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-spring-dm-impl/1.1/cxf-dosgi-ri-samples-spring-dm-impl-1.1.jar
</span>... log messages may appear ...
-&gt; ps
START LEVEL 32
   ID   State         Level  Name
[   0] [Active     ] [    0] <span class="code-object">System</span> Bundle (1.8.0)
... bundles ...
[  35] [Active     ] [    1] CXF Distributed OSGi Spring-DM Sample Interface Bundle
[  36] [Active     ] [    1] CXF Distributed OSGi Spring-DM Sample Implementation Bundle
</pre>
</div></div>
<p>At this point the service should be available. You can check this by obtaining the
WSDL:</p>

<p><img src="/confluence/download/attachments/119322/spring-dm-wsdl.jpeg" align="absmiddle"
border="0" /></p>

<h2><a name="DOSGiSpring-DMDemopage-TheDinnerServiceConsumer"></a>The Dinner
Service Consumer</h2>
<p>As on the remote service provider side, the service consumer is also created using
spring. Spring creates a <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/spring_dm/client/src/main/java/org/apache/cxf/dosgi/samples/springdm/client/DinnerServiceConsumer.java"
rel="nofollow">DinnerServiceConsumer</a> bean which is injected with the a proxy
to the remote DinnerService. The injection is all done by Spring, which makes the code extremely
simple. When Spring is done injecting, it calls the <tt>start()</tt> method where
the remote service is used. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class DinnerServiceConsumer
{
    DinnerService dinnerService;
    
    <span class="code-keyword">public</span> void setDinnerService(DinnerService
ds) {
        dinnerService = ds;
    }
    
    <span class="code-keyword">public</span> void start() {
        <span class="code-object">System</span>.out.println(<span class="code-quote">"Found
the following restaurants:"</span>);
        <span class="code-keyword">for</span> (Restaurant r : dinnerService.findRestaurants(<span
class="code-quote">"nice and not too expensive!"</span>)) {
            <span class="code-object">System</span>.out.format(<span class="code-quote">"
 %s (%s) Rating: %d\n"</span>, r.getName(), r.getAddress(), r.getRating());
        }
    }
}
</pre>
</div></div>

<p>The client side <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/spring_dm/client/src/main/resources/META-INF/spring/client-spring.xml"
rel="nofollow"><tt>META-INF/spring/spring.xml</tt></a> file is also really
simple. It simply declares a dependency on the OSGi DinnerService, which is injected into
the DinnerServiceConsumer bean. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;beans&gt;</span>
  <span class="code-tag">&lt;osgi:reference id=<span class="code-quote">"dinnerServiceRef"</span>
interface=<span class="code-quote">"org.apache.cxf.dosgi.samples.springdm.DinnerService"</span>/&gt;</span>
  
  &lt;bean class=<span class="code-quote">"org.apache.cxf.dosgi.samples.springdm.client.DinnerServiceConsumer"</span>
        init-method=<span class="code-quote">"start"</span>&gt;
    <span class="code-tag">&lt;property name=<span class="code-quote">"dinnerService"</span>
ref=<span class="code-quote">"dinnerServiceRef"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>
<p>Like in the Greeter demo, the client side needs to be configured to know where the
remote service actually is. When using a Discovery system this configuration is provided dynamically
via Discovery (see the Discovery demo - TODO). In this demo this information is provided statically
in a <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/spring_dm/client/src/main/resources/OSGI-INF/remote-service/remote-services.xml"
rel="nofollow"><tt>OSGI-INF/remote-service/remote-services.xml</tt></a>
file. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;service-descriptions xmlns=<span class="code-quote">"http://www.osgi.org/xmlns/sd/v1.0.0"</span>&gt;</span>
  <span class="code-tag">&lt;service-description&gt;</span>
    <span class="code-tag">&lt;provide interface=<span class="code-quote">"org.apache.cxf.dosgi.samples.springdm.DinnerService"</span>
/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"osgi.remote.interfaces"</span>&gt;</span>*<span
class="code-tag">&lt;/property&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"osgi.remote.configuration.type"</span>&gt;</span>pojo<span
class="code-tag">&lt;/property&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"osgi.remote.configuration.pojo.address"</span>&gt;</span>http://localhost:9000/org/apache/cxf/dosgi/samples/springdm/DinnerService<span
class="code-tag">&lt;/property&gt;</span>
  <span class="code-tag">&lt;/service-description&gt;</span>
<span class="code-tag">&lt;/service-descriptions&gt;</span>
</pre>
</div></div>
<p>Install and run the consumer side of the demo in a separate Felix instance:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">-&gt; start http:<span class="code-comment">//repo2.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-spring-dm-<span
class="code-keyword">interface</span>/1.1/cxf-dosgi-ri-samples-spring-dm-<span
class="code-keyword">interface</span>-1.1.jar
</span>-&gt; start http:<span class="code-comment">//repo2.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-spring-dm-client/1.1/cxf-dosgi-ri-samples-spring-dm-client-1.1.jar
</span>... log messages may appear, at some point the consumer will make an invocation
on the remote service, you will see:
Found the following restaurants:
  Jojo's (1 food way) Rating: 3
  Boohaa's (95 forage ave) Rating: 1
  MicMac (Plastic Plaza) Rating: 1</pre>
</div></div>
<p>And on the service provider side, you can see that it has been invoked as the following
message appears: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">-&gt; Hey! Someone's using the Dinner Service! Query: nice
and not too expensive!</pre>
</div></div>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CXF/DOSGi+Spring-DM+Demo+page">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=119322&revisedVersion=8&originalVersion=7">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CXF/DOSGi+Spring-DM+Demo+page?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message