6.02. Intermediate - Creating the CXF SU

We are going to create the main SU of our project : the CXF EJB proxy.

The Maven pom.xml

First of all, we create the pom.xml :

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.apache.servicemix.examples</groupId>
    <artifactId>ejb-example</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <groupId>org.apache.servicemix.examples</groupId>
  <artifactId>ejb-cxf-su</artifactId>
  <packaging>jbi-service-unit</packaging>
  <name>Apache ServiceMix :: Examples :: EJB :: CXF SU</name>

  <dependencies>
    <dependency>
      <groupId>org.apache.servicemix</groupId>
      <artifactId>servicemix-cxf-se</artifactId>
      <version>2008.01</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>2.5.5</version>
    </dependency>
    <dependency>
      <groupId>jboss</groupId>
      <artifactId>jbossall-client</artifactId>
      <version>4.2.2.GA</version>
    </dependency>
    <dependency>
       <groupId>my.ejb.provider</groupId>
       <artifactId>ejb-client</artifactId>
       <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
 
  <build>
     <resources>
       <resource>
          <directory>src/main/resources</directory>
             <includes>
               <include>**/*</include>
             </includes>
        </resource>
      </resources>
      <plugins>
        <plugin>
          <groupId>org.apache.servicemix.tooling</groupId>
          <artifactId>jbi-maven-plugin</artifactId>
          <version>3.2.3</version>
          <extensions>true</extensions>
         </plugin>
       </plugins>
  </build>
   
</project>

We are using EJBs deploying in a JBoss application server : that's why we need the JBoss client dependency.
The EJB provider needs to provide the local and home interfaces (and eventually the stub) for the EJB server side.

JBI xbean.xml

Now we can setup the xbean.xml (in the src/main/resources directory) to expose our EJB :

xbean.xml

<beans xmlns:cxfse="http://servicemix.apache.org/cxfse/1.0"
       xmlns:myService="http://www.example.com/myService">

   <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
      <property name="environment">
         <props>
            <prop key="java.naming.factory.initial">
               org.jnp.interfaces.NamingContextFactory
            </prop>
            <prop key="java.naming.provider.url">
               jnp://localhost:1299
            </prop>
         </props>
      </property>
   </bean>

   <bean id="ejbProxy" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
      <property name="jndiName" value="ejb/myEJB"/>
      <property name="businessInterface" value="com.ejb.provider.MyEJB"/>
      <property name="homeInterface" value="com.ejb.provider.MyEJBHome"/>
      <property name="refreshHomeOnConnectFailure" value="true"/>
      <property name="cacheHome" value="true"/>
      <property name="lookupHomeOnStartup" value="true"/>
      <property name="resourceRef" value="false"/>
      <property name="jndiTemplate" ref="jndiTemplate"/>
   </bean>

   <cxfse:endpoint>
      <cxfse:pojo>
         <bean class="org.apache.servicemix.examples.MyEJBImpl">
            <property name="proxy" ref="ejbProxy"/>
         </bean>
      </cxfse:pojo>
   </cxfse:endpoint>

</beans>

Warning : only EJB Session Stateless can be exposed like this.

The EJB Proxy interface and implementation

You can see in the xbean that we need the service impl and interface for the CXF endpoint.

So we create the MyEJB interface :

MyEJB.java
package org.apache.servicemix.examples;

import javax.jws.WebService;

@WebService(targetNamespace = "http://www.example.com/myService", name = "MyEJB")
public interface MyEJB {

   public String echo(String message);

}

and the corresponding impl :

MyEJBImpl.java
/**
 * Service delegate to the AutomaticSimulationSession EJB
 *
 * @author <a href="mailto:[email protected]">Jean-Baptiste Onofré</a>
 */
package org.apache.servicemix.examples;

import javax.jws.WebService;
import javax.xml.ws.Holder;

import com.ejb.provider.MyEJB;

@WebService(serviceName = "myService", targetNamespace = "http://www.example.com/myService", endpointInterface = "org.apache.servicemix.examples.MyEJB")
public class MyEJBImpl implements MyEJB {

   // simple Spring remote EJB proxy
   private AutomaticSimulationSession proxy;

   // getters and setters

   public void setProxy(AutomaticSimulationSession proxy) {
      this.proxy = proxy;
   }

   public AutomaticSimulationSession getProxy() {
      return this.proxy;
   }

   // service delegation methods

   /**
    * Delegation to the <code>echo</code> method of the <code>MyEJB</code> EJB.
    */
   public String echo(String message) {
      try {
         return (proxy.echo(message).toString();
      }
      catch(Exception exception) {
         exception.printStackTrace();
         return "Error occurs : " + exception.getMessage();
      }
   }

}

Our EJB is now available in ServiceMix as a service registered in the NMR.

So now, we use the EJB service via a binding component

Proceed to the next step