Enterprise JavaBean 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, adapt the HelloServlet example into a session bean. Here is the code:

 package org.jboss.chap12.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, you do still need the Hello service endpoint interface 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 5, "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/j2ee" 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.chap12.hello.Hello</service-endpoint>             <ejb-class>org.jboss.chap12.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> 

A supporting webservices.xml needs to accompany this. The file, shown below, looks almost identical to the webservices.xml for the WAR file:

[View full width]

<webservices xmlns="http://java.sun.com/xml/ns/j2ee" 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" version="1.1"> <webservice-description> <webservice-description-name>HelloService</webservice-description-name> <wsdl-file>META-INF/wsdl/HelloService.wsdl</wsdl-file> <jaxrpc-mapping-file>META-INF/mapping.xml</jaxrpc-mapping-file> <port-component> <port-component-name>Hello</port-component-name> <wsdl-port>HelloPort</wsdl-port> <service-endpoint-interface>org.jboss.chap12.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. The WSDL file and JAX-RPC mapping files remain unchanged from the previous example.

To package and deploy the application, run the following command in the examples directory:

 [examples]$ ant -Dchap=chap12 -Dex=2 run-example ... run-example2: [copy] Copying 1 file to /tmp/jboss-4.0.1/server/default/deploy [echo] Waiting for 5 seconds for deploy... [java] Contacting webservice at http://localhost:8080/hello-ejb/Hello?wsdl [java] hello.hello(JBoss user) [java] output:Hello JBoss user! 

The test program run here is the same as the servlet example, except that it uses a different URL for the WSDL. JBoss composes the WSDL using the base name of the EJB JAR file and the name of the web service interface.

However, as with all web services in JBoss, you can use the http://localhost:8080/ws4ee/services service view shown in Figure 12.2 to verify the deployed URL of the WSDL.



JBoss 4. 0(c) The Official Guide
JBoss 4.0 - The Official Guide
ISBN: B003D7JU58
EAN: N/A
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net