| Sign In/My Account | View Cart |
Constructing Services with J2EE
Pages: 1, 2
Creating a web service as a distributed component that is
portable and interoperable is not a trivial task. As discussed
earlier, you can deploy either regular Java classes or stateless
EJBs as web services. Regular Java classes are packaged in a web
module and EJB web services are packaged in normal ejb-jar
modules.
Given these two deployment options, which one do you use?
It is a matter of prolonged debate whether you should choose a regular Java class or EJBs as your technology for building a web service. Java classes are easier to develop than EJBs, are pure Java objects, and do not have the "extra baggage" that EJBs do. However, EJBs provide several nice features, such as declarative transaction and security, and let the developer concentrate on building business logic without having to worry about the infrastructure services. EJB 3.0 is greatly simplifying the programming model and in that spec EJBs will look like regular Java classes.
Whether you decide to use a regular Java class or EJBs, you need
to package several artifacts into your WAR or ejb-jar to expose your
component as a Java web service. Following are the packaging
structures for web services based either on EJB or regular Java
classes.
ejb-jar for an EJB-based web service:
/META-INF/
ejb-jar.xml
webservices.xml
oracle-webservices.xml
mapping-file.xml
wsdl/ the wsdl file
ejb classes (includes endpoint and bean classes)
Web app (.war) for a regular Java web service:
/WEB-INF/
web.xml
webservices.xml
oracle-webservices.xml
mapping-file.xml
wsdl/ the wsdl file
/classes/(includes endpoint and bean classes)
/lib/
Let us discuss each of the deployment-time artifacts and descriptors:
java.rmi.Remote interface, and
every method exposed in the endpoint interface must throw
java.rmi.RemoteException. This end point needs to
registered into the standard deployment descriptor for the module
(ejb-jar.xml or web.xml). Your deployment
descriptor (e.g., ejb-jar.xml) needs to have the following entry:
<service-endpoint>
oracle.ejb21.ws.HelloServiceInf
</service-endpoint>
The following shows the code for the Web service endpoint for a
HelloWorld web service:
public interface HelloServiceInf
extends java.rmi.Remote {
java.lang.String sayHello(java.lang.String name)
throws java.rmi.RemoteException;
}
ejb-jar or to define these properties.You must package all these artifacts in the WAR or ejb-jar
module before you can deploy your component as a web service. Many
development tools, such as
Oracle JDeveloper, simplify development of web services by
doing mundane tasks such as generating deployment descriptors,
mapping files, etc. Furthermore, most application servers provide
web services assembly tools that take care of the J2EE web service
packaging requirements.
Beyond an understanding of the components that make up a web service and the associated packaging requirements, there are architectural issues you must deal with when developing a web service.
The main challenge in building a web service is to identify the service with the right granularity. You can either build a new service, or expose an existing component that is built as a Java class or EJB and expose that as a service. When building a service, you can take either the top-down or bottom-up approach:
Obviously, it is imperative that your web services be interoperable in nature. J2EE 1.4 mandates conformance to the Basic Profile 1.0 specified by the Web Services: Interoperability (WS-I) organization. When building web services, you must test for interoperability before you deploy them into production.
In addition to the design approaches and a need to field interoperable services, there are a few best practices that you can follow to maximize the utility of your web service.
Here are a few best practices for developing web services:
Collections,
HashMaps, and Lists as parameters for your web service if interoperability is important for your application.The J2EE Blueprint Application Java Adventure Builder provides a nice blueprint application for building Java-based web services application.
Once a web service has been designed, developed, and deployed, associated client components are generally created to interact with the given service.
The client for a web service can be any of the following types: static stub, dynamic proxy, or Dynamic Invocation Interface (DII).
Building a web service client may be as complex as building a simple web service. Fortunately, J2EE 1.4 makes it simpler for J2EE developers to use web services from any type of J2EE component--namely web clients or EJB components.
You can invoke a web service as you would any other resources using JNDI via the following:
service-ref element in the deployment
descriptor for your component. For example, if you access the
HelloWorldService web service from a web module, the module's
web.xml file may contain the following:
<service-ref>
<service-ref-name>service/HelloWorldService</service-ref-name>
<service-interface>oracle.ws.HelloWorldService</service-interface>
<wsdl-file>META-INF/HelloWorldService.wsdl</wsdl-file>
<service-qname>urn:oracle-ws</service-qname>
</service-ref>
<service-ref-mapping name="service/HelloWorldService">
<port-info>
<wsdl-port namespaceURI="urn: HelloWorldService"
localpart="HelloWorldServicePort"/>
<stub-property>
<name>javax.xml.rpc.service.endpoint.address</name>
<value>http://localhost:8888/hello/HelloWorldInf</value>
</stub-property>
</port-info>
</service-ref-mapping>
InitialContext ctx= new InitialContext();
HelloServiceInf hs = (HelloServiceInf)
ctx.lookup("java:comp/env/service/HelloWorldService");
HelloWorld hello= hs.getHelloWorldServicePort();
String myhello = hs.sayHello("Debu Panda") ;
The web services platform has grown to include reliability, security, transactions, manageability, policy and so on. There are several standards that has emerged or emerging in the web services space. There are several JSRs being worked on in the Java Community Process to design a new Java API to uptake these emerging standards and the following table lists some of these JSRs.
| Java Specification Request | Goal |
| 181 | Web Services Metadata |
| 208 | Java Business Integration |
| 261 | Java API for XML Web Services Addressing |
| 262 | Web Services Connector for JMX |
| 265 | API for using Web Services Policy |
In addition to these evolving standards, we are now getting a glimpse into the web services support afforded by the next major release of the J2EE platform.
Service-oriented applications are fairly difficult to build with J2EE, so J2EE 5.0 is designed to make development simpler by making use of Web Services Metadata annotations defined by JSR 181. EJB 3.0 and Web Services Metadata have the similar goals of providing developer friendliness.
For developing a simple Java web service in J2EE 1.4, you need several web service artifacts: WSDL, mapping files, and several verbose standard and proprietary web services deployment descriptors. The Web Services Metadata specification is taking a configuration-by-default approach similar to EJB 3.0 to make development easier. The Web Services Metadata annotation processor (or web services assembly tool) will generate these files for you so you only have to worry about the implementation class.
Here is how a simple Java web service looks when it's developed using Web Services Metadata:
package oracle.jr181.demo;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService(name = "HelloWorldService",
targetNamespace = "http://hello/targetNamespace" )
public class HelloWorldService {
@WebMethod public String sayhello(String name ) {
return "Hello” +name+ “ from jws";
}
}
As I mentioned previously, EJB 3.0 is simplifying the development of
EJBs by using regular Java classes. By making use of EJB 3.0
and Web Services Metadata, developing EJB-based web services is
going to be much simpler. Here is how a simple HelloWorld EJB web
service looks when using EJB 3.0 and web services metadata. You do
not have to worry about creating WSDL, deployment descriptors, etc.,
and the application server will take care of generating these
artifacts during deployment.
package oracle.ejb30.ws;
import javax.ejb.Remote;
import javax.jws.WebService;
@WebService
public interface HelloServiceInf extends java.rmi.Remote{
@WebMethod java.lang.String sayHello(java.lang.String name)
throws java.rmi.RemoteException;
}
The following is the implementation class for the HelloWorld EJB in EJB 3.0:
package oracle.ejb30.ws;
import java.rmi.RemoteException;
import javax.ejb.Stateless;
@Stateless(name="HelloServiceEJB")
public class HelloServiceBean implements HelloServiceInf {
public String sayHello(String name) {
return("Hello "+name +" from first EJB3.0 Web Service");
}
}
The above example clearly demonstrates that service development is going to be much simpler with web services metadata and EJB 3.0.
In this article, you learned the basics of building web services using the J2EE platform. You can start building and deploying your web services in your favorite J2EE-compliant application servers such as Oracle Application Server 10g, Sun Java System Application Sever, etc. today.
Debu Panda is a Senior Principal Product Manager of the Oracle Application Server development team.
Return to ONJava.com.
Showing messages 1 through 2 of 2.
Even though many companies are investing to move to service based architectures,with development of technologies like spring,ajax and hibernate should web services would remain as wrappers around ejb?
For SOA to be better implemented,souurounding technologies would need to stabilize
Regards
Ganesh