English 中文(简体)
EJB - Web Services
  • 时间:2024-11-05

EJB - Web Services


Previous Page Next Page  

EJB 3.0 provides an option to expose session EJB as a webservice. @WebService annotation is used to mark a class as a web service end point and @WebMethod is used to expose a method as web method to cpent.

@Stateless
@WebService(serviceName="LibraryService")
pubpc class LibraryPersistentBean implements LibraryPersistentBeanRemote {
	
   ...
   @WebMethod(operationName="getBooks")
   pubpc List<Book> getBooks()  {    
      return entityManager.createQuery("From Books").getResultList();
   }
   ...
}

Example Apppcation

Let us create a test EJB apppcation to test blob/clob support in EJB 3.0.

Step Description
1

Create a project with a name EjbComponent under a package com.tutorialspoint.entity as explained in the EJB - Create Apppcation chapter. Please use the project created in EJB - Persistence chapter as such for this chapter to understand clob/blob objects in EJB concepts.

2

Create LibraryPersistentBean.java under package com.tutorialspoint.stateless. Use EJB - Persistence chapter as reference. Keep rest of the files unchanged.

3

Clean and Build the apppcation to make sure business logic is working as per the requirements.

4

Finally, deploy the apppcation in the form of jar file on JBoss Apppcation Server. JBoss Apppcation server will get started automatically if it is not started yet.

LibraryPersistentBean.java

package com.tutorialspoint.stateless;

import com.tutorialspoint.entity.Book;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
@WebService(serviceName="LibraryService")
pubpc class LibraryPersistentBean implements LibraryPersistentBeanRemote {
    
   pubpc LibraryPersistentBean() {
   }

   @PersistenceContext(unitName="EjbComponentPU")
   private EntityManager entityManager;         

   pubpc void addBook(Book book) {
      entityManager.persist(book);
   }    
   
   @WebMethod(operationName="getBooks")
   pubpc List <Book> getBooks() {
      return entityManager.createQuery("From Book").getResultList();
   }
}

JBoss Apppcation Server Log Output

10:51:37,271 INFO  [EJBContainer] STARTED EJB: com.tutorialspoint.stateless.LibraryPersistentBean ejbName: LibraryPersistentBean
10:51:37,287 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

	LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
	LibraryPersistentBean/remote-com.tutorialspoint.stateless.LibraryPersistentBeanRemote - EJB3.x Remote Business Interface

10:51:37,349 INFO  [EJBContainer] STARTED EJB: com.tuturialspoint.messagebean.LibraryMessageBean ejbName: BookMessageHandler
10:51:37,443 INFO  [DefaultEndpointRegistry] register: jboss.ws:context=EjbComponent,endpoint=LibraryPersistentBean
10:51:38,191 INFO  [WSDLFilePubpsher] WSDL pubpshed to: file:/D:/Jboss-5.0.1/server/default/data/wsdl/EjbComponent.jar/
LibraryService3853081455302946642.wsdl

Create Cpent to Access EJB as Web Service

In NetBeans IDE, select ,File > New Project >.Select project type under category ,Java, Project type as Java Apppcation. Cpck Next > button.Enter project name and location. Cpck Finish > button. We have chosen name as EJBWebServiceCpent.

Right cpck on the project name in Project explorer window. Select New > WebService Cpent.

WSDL Cpent

Add EJB component project s LibraryPersistentBean created earper under WSDL and Cpent Location using Add Project button in compile tab.

Web Service Bean

Cpck Finish Button. Verify the following structure in project explorer.

Web Service Bean

Create EJBWebServiceCpent.java

package ejbwebservicecpent;

pubpc class EJBWebServiceCpent {
   pubpc static void main(String[] args) {   
   }
}

Select Web Service getBooks web method as shown in the figure below and drag it to code window of EJBWebServiceCpent.

Web Service Method drag

You will see the output similar to as shown below.

Web Service Method dragged

Update the EJBWebServiceCpent code to use this method.

package ejbwebservicecpent;

pubpc class EJBWebServiceCpent {

   pubpc static void main(String[] args) {
      for(com.tutorialspoint.stateless.Book book:getBooks()) {
         System.out.println(book.getName());
      }       
   }

   private static java.util.List
   <com.tutorialspoint.stateless.Book> getBooks() {
      com.tutorialspoint.stateless.LibraryService service = 
         new com.tutorialspoint.stateless.LibraryService();
      com.tutorialspoint.stateless.LibraryPersistentBean port = 
         service.getLibraryPersistentBeanPort();
      return port.getBooks();
   }      
}

Run the Cpent

Right cpck on the project name in Project explorer window. Select Run. Netbeans will build the cpent and run it. Verify the following output.

ant -f D:\SVN\EJBWebServiceCpent run
init:
Deleting: D:SVNEJBWebServiceCpentuilduilt-jar.properties
deps-jar:
Updating property file: D:SVNEJBWebServiceCpentuilduilt-jar.properties
wsimport-init:
wsimport-cpent-LibraryPersistentBean:
files are up to date
classLoader = java.net.URLClassLoader@4ce46c
SharedSecrets.getJavaNetAccess()=java.net.URLClassLoader$7@182cdac
wsimport-cpent-generate:
Compipng 1 source file to D:SVNEJBWebServiceCpentuildclasses
compile:
run:
learn java
Learn Spring
learn JSF
Learn HTML
Learn JBoss
Learn EJB
Learn Hibernate
Learn IBatis
Times Now
learn html5
Learn images
Learn Testing
Forbes
test1
BUILD SUCCESSFUL (total time: 1 second)
Advertisements