cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache CXF Documentation > Invokers
Date Mon, 28 Jan 2013 17:18:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CXF20DOC&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/CXF20DOC/Invokers">Invokers</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~coheigea@apache.org">Colm
O hEigeartaigh</a>
    </h4>
        <br/>
                         <h4>Changes (4)</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" >If you are using an EJB3 container
you can use the following invoker, which is just a simplified version of the above: <br>{code}
<br></td></tr>
            <tr><td class="diff-unchanged" >public class EJB3Invoker extends AbstractInvoker
{
    private Object ejb; <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-unchanged" >    public EJB3Invoker(String jndiName)
throws NamingException 	{
        ejb = new InitialContext().lookup(jndiName);
    } <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" >public Object getServiceObject(final
Exchange context) <span class="diff-added-words"style="background-color: #dfd;">{</span>
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
       return ejb; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">{</span>
<span class="diff-added-words"style="background-color: #dfd;">}</span> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">
	return ejb; <br>	} <br></td></tr>
            <tr><td class="diff-unchanged" >} <br>{code} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Invokers allow you to customise how a particular method is executed. This
is particular useful if your underlying service objects are not plain javabeans and instead
need to be created or looked up via a custom factory.</p>

<p>CXF does provide a number of bundled invokers to handle simple cases. One of these
simple cases is when it is desirable to have a singleton for the service object. In this case,
you would like to provide a single object instance that should be used for all service invocations.
The provided BeanInvoker covers this functionality, and would be used as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Service service = ...; 
service.setInvoker(<span class="code-keyword">new</span> BeanInvoker(<span
class="code-keyword">new</span> MyCustomBean(someParams)));
</pre>
</div></div>

<p>You can access the underlying Service object in two ways. If you've created your
service using a ServerFactoryBean, this will yield a Server object which can be used to gain
access to the Service:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ServerFactoryBean factory = <span class="code-keyword">new</span> ServerFactoryBean();
....
Server server = factory.create()
Service service = server.getEndpoint().getService();
</pre>
</div></div>
<p>If you've created a JAX-WS Endpoint object, you can access the Service like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
EndpointImpl endpoint = (EndpointImpl) Endpoint.publish(<span class="code-quote">"http:<span
class="code-comment">//host/service"</span>, <span class="code-keyword">new</span>
MyService());
</span>....
Server server = endpoint.getServer();
Service service = server.getEndpoint().getService();
</pre>
</div></div>

<p>The following example illustrates how an invoker can be used to allow CXF to expose
remote stateless session beans as a webservice. Given the method to invoke, this invoker will
create a stateless session bean instance to invoke the method on. The same technique can be
used to enable service calls to any object that requires custom creation/lookup.</p>

<p>The invoker implementation 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> class EJBInvoker <span class="code-keyword">extends</span>
AbstractInvoker
{
  <span class="code-keyword">private</span> EJBHome home;
  <span class="code-keyword">private</span> Method createMethod;
  <span class="code-keyword">private</span> <span class="code-keyword">static</span>
<span class="code-keyword">final</span> <span class="code-object">Object</span>[]
EMPTY_OBJECT = <span class="code-keyword">new</span> <span class="code-object">Object</span>[0];

  <span class="code-keyword">public</span> EJBInvoker(EJBHome home)
  {
    <span class="code-keyword">this</span>.home = home;
    <span class="code-keyword">try</span>
    {
      <span class="code-keyword">if</span>(!home.getEJBMetaData().isSession()
|| !home.getEJBMetaData().isStatelessSession())
      {
        <span class="code-keyword">throw</span> <span class="code-keyword">new</span>
IllegalArgumentException(<span class="code-quote">"home must be <span class="code-keyword">for</span>
a stateless session bean"</span>);
      }
      createMethod = home.getClass().getMethod(<span class="code-quote">"create"</span>,
<span class="code-keyword">new</span> <span class="code-object">Class</span>[0]);
    }
    <span class="code-keyword">catch</span>(Exception ex)
    {
      <span class="code-keyword">throw</span> <span class="code-keyword">new</span>
IllegalArgumentException(<span class="code-quote">"Unable to initialize invoker: "</span>
+ ex);
    }
  }

  <span class="code-keyword">public</span> <span class="code-object">Object</span>
getServiceObject(<span class="code-keyword">final</span> Exchange context)
  {
     <span class="code-keyword">return</span> createMethod.invoke(home, EMPTY_OBJECT);
  }
}
</pre>
</div></div>
<p>Invokers, once defined, need to be registered with the Service. Once a handle onto
a Service object has been obtained, the example invoker above can be registered on the binding
like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Service ejbService = ....;
ejbService.setInvoker(<span class="code-keyword">new</span> EJBInvoker(ejbHome));
</pre>
</div></div>
<p>If you are using an EJB3 container you can use the following invoker, which is just
a simplified version of the above:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class EJB3Invoker <span class="code-keyword">extends</span>
AbstractInvoker {
    <span class="code-keyword">private</span> <span class="code-object">Object</span>
ejb;

    <span class="code-keyword">public</span> EJB3Invoker(<span class="code-object">String</span>
jndiName) <span class="code-keyword">throws</span> NamingException 	{
        ejb = <span class="code-keyword">new</span> InitialContext().lookup(jndiName);
    }

    <span class="code-keyword">public</span> <span class="code-object">Object</span>
getServiceObject(<span class="code-keyword">final</span> Exchange context) {
        <span class="code-keyword">return</span> ejb;
    }
}
</pre>
</div></div>

<h2><a name="Invokers-Executors"></a>Executors</h2>
<p>In addition to providing your own Invokers, you can also supply Executors for your
service. Executors are a way to control scheduling for your service. To supply your own executor
for a service just do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Service service = ....; <span class="code-comment">// look up the service from CXF,
or create it
</span>service.setExecutor(<span class="code-keyword">new</span> MyExecutor());
</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/CXF20DOC/Invokers">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=49905&revisedVersion=2&originalVersion=1">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/Invokers?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message