Show Table of Contents
11.2. EJB Endpoints
Web services can also be provided from the EJB tier. Any stateless session bean can serve as the endpoint for a web service in almost the same way as the JAX-RPC endpoints. To see how this works, we will adapt the
HelloServlet example into a session bean. Here is the code:
package org.jboss.ws.hello;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class HelloBean
implements SessionBean
{
public String hello(String name)
{
return "Hello " + name + "!";
}
public void ejbCreate() {};
public void ejbRemove() {};
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext ctx) {}
}
This is a very trivial session bean. Session beans normally require a home interface and either a local or remote interface. However, it is possible to omit them if the session bean is only serving as a web services endpoint. However, we do still need the
Hello service endpoint interface that we used in the JSE example.
The
ejb-jar.xml file is very standard for a session bean. The normal session bean parameters are explained in Chapter 21, EJBs on JBoss. The only new element is the service-endpoint element, which declares the service endpoint interface for the web service.
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/"Whats_new_in_JBoss_4-J2EE_Certification_and_Standards_Compliance" version="2.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>chapter 12 EJB JAR</display-name>
<enterprise-beans>
<session>
<ejb-name>HelloBean</ejb-name>
<service-endpoint>org.jboss.ws.hello.Hello</service-endpoint>
<ejb-class>org.jboss.ws.hello.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<method-permission>
<unchecked/>
<method>
<ejb-name>HelloBean</ejb-name>
<method-name>*</method-name>
</method>
</method-permission>
<container-transaction>
<method>
<ejb-name>HelloBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
The accompanying deployment descriptor files can again be generated by the
wstool program. The configuration file is nearly identical, except that instaead of linking to the pseudo-servlet, we link to the HelloBean EJB.
<configuration xmlns="http://www.jboss.org/jbossws-tools">
<java-wsdl>
<service name="HelloService"
style="rpc"
endpoint="org.jboss.ws.hello.Hello"/>
<namespaces target-namespace="http://hello.ws.jboss.org/"
type-namespace="http://hello.ws.jboss.org/types"/>
<mapping file="jaxrpc-mapping.xml"/>
<webservices ejb-link="HelloBean"/>
</java-wsdl>
</configuration>
The generated files are nearly identical to the ones for the previous example, except for the
webservice.xml file webservices.xml. The file, shown below, contains configuration options appropriate to the EJB endpoint.
<webservices version="1.1"
xmlns="http://java.sun.com/xml/ns/"Whats_new_in_JBoss_4-J2EE_Certification_and_Standards_Compliance"
xmlns:impl="http://hello.ws.jboss.org/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd">
<webservice-description>
<webservice-description-name>HelloService</webservice-description-name>
<wsdl-file>META-INF/wsdl/HelloService.wsdl</wsdl-file>
<jaxrpc-mapping-file>META-INF/jaxrpc-mapping.xml</jaxrpc-mapping-file>
<port-component>
<port-component-name>HelloPort</port-component-name>
<wsdl-port>impl:HelloPort</wsdl-port>
<service-endpoint-interface>org.jboss.ws.hello.Hello</service-endpoint-interface>
<service-impl-bean>
<ejb-link>HelloBean</ejb-link>
</service-impl-bean>
</port-component>
</webservice-description>
</webservices>
The first difference is that the WSDL file should be in the
META-INF/wsdl directory instead of the WEB-INF/wsdl directory. The second difference is that the service-impl-bean element contains an ejb-link that refers to the ejb-name of the session bean.
To package and deploy the application, run the following command in the examples directory:
[examples]$ ant -Dchap=ws -Dex=2 run-example
...
run-example2:
[echo] Waiting for 5 seconds for deploy...
[java] Contacting webservice at http://localhost:8080/hello-ejb/HelloBean?wsdl
[java] hello.hello(JBoss user)
[java] output:Hello JBoss user!
The test program run here is the same as with the servlet example, except that we use a different URL for the WSDL. JBoss composes the WSDL using the base name of the EJB JAR file and the name of the service interface. However, as with all web services in JBoss, you can use the
http://localhost:8080/jbossws/services service view shown in Figure 11.2, “The web services list” to verify the deployed URL of the WSDL.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.