You are on page 1of 8


http://www.tuto rialspo m/e jb/e jb_cre ate _applicatio n.htm
Co pyrig ht © tuto rials po m

T o create a simple EJB module, we'll use NetBeans "New project" wizard. In example below, We'll create a ejb module project named Component.

Create Project
In NetBeans IDE, select ,File > New Projec t >. You'll see the following screen.

Select project type under categ ory,J ava EE , Project type as Ejb Module . Click Next > button. You'll see the following screen.

Select Server as J Boss Applic ation Server . You'll see the following screen.Enter project name and location. You'll see the following project created by NetBeans. Click Next > button. Click Finish button. .

we'll use NetBeans "New" wizard. Select.local interface for session bean I am chang ing local interface to remote interface as we're g oing to access our ejb in a console based application. . We'll create a stateless ejb class named librarySessionBean under EjbComponent project. You'll see the following ejb classes created by NetBeans. LibrarySessionBean .stateless session bean LibrarySessionBeanLoc al . Enter session bean name and packag e name.Create a sample EJB T o create a simple EJB. Click Finish button. New > Session Bean. Select project EjbComponent in project explorer window and rig ht click on it. LibrarySessionBeanLocal is renamed to LibrarySessionBeanRemote and LibrarySessionBean implements LibrarySessionBeanRemote interface. Remote/Local interface are used to expose business methods that an ejb has to implement.You'll see the New Session Beanwizard. In example below.

.6 Note: C:\EJB\EjbComponent\src\java\com\tutorialspoint\stateless \LibraryPersistentBean.Remote.util.ejb.util.Stateless. @Remote public interface LibrarySessionBeanRemote { void addBook(String bookName).List. } public void addBook(String bookName) { bookShelf. import javax. You'll see the following output in NetBeans console uses unchecked or unsafe operations. } LibrarySessionBean package com. import java.tutorialspoint.util.stateless.add(bookName). import java. @Stateless public class LibrarySessionBean implements LibrarySessionBeanRemote { List<String> bookShelf.List.tutorialspoint. ant -f C:\\EJB\\EjbComponent clean dist init: undeploy-clean: deps-clean: Deleting directory C:\EJB\EjbComponent\build Deleting directory C:\EJB\EjbComponent\dist clean: init: deps-jar: Created dir: C:\EJB\EjbComponent\build\classes Copying 3 files to C:\EJB\EjbComponent\build\classes\META-INF Created dir: C:\EJB\EjbComponent\build\empty Created dir: C:\EJB\EjbComponent\build\generated-sources\ap-source-output Compiling 2 source files to C:\EJB\EjbComponent\build\classes warning: [options] bootstrap class path not set in conjunction with -source 1. import javax.ArrayList. } public List<String> getBooks() { return bookShelf. List getBooks().ejb. import java. public LibrarySessionBean(){ bookShelf = new ArrayList<String>().LibrarySessionBeanRemote package com.stateless. Select clean and build. } } Build the Project Select EjbComponent project in Project Explorer window Rig ht click on it to open context menu.

0.GA\bin\run.0. Distributing C:\EJB\EjbComponent\dist\EjbComponent.Note: Recompile with -Xlint:unchecked for details.GA (build: SVNTag=JBoss_5_1_0_GA date=200905221634)] Started in 52s:427ms Deploy the Project Select EjbComponent project in Project Explorer window Rig ht click on it to open context menu.0. 16:25:50.1..0_21\bin\java JAVA_OPTS: -Dprogram. Select Deploy.0.. ctxPath=/jmx-console 16:26:42.1-8080 16:26:42.3 on ajp-127.for..062 INFO [ServerImpl] Release ID: JBoss [The Oracle] 5.493 INFO [ServerImpl] JBoss (Microcontainer) [5.0.GA (build: SVNTag=JBoss_5_1_0_GA date=200905221634) . You'll see the following output in NetBeans console output.jar ========================================================================= 16:25:50. 1 warning compile: library-inclusion-in-archive: Created dir: C:\EJB\EjbComponent\dist Building jar: C:\EJB\EjbComponent\dist\EjbComponent.0.output under JBoss Application Server .jar to .wait.deployment.487 INFO [AjpProtocol] Starting Coyote AJP/ 16:26:40.1. Select start.GA\bin\run.GA JAVA: C:\Program Files (x86)\Java\jdk1.406 INFO [TomcatDeployment] deploy.jar dist: BUILD SUCCESSFUL (total time: 3 seconds) Start the Application Server Select JBoss application server under Servers in Services window Rig ht click on it to open context menu.2_12-b01-FCS) for context '/admin-console' 16:26:42.1 on http-127.0..485 INFO [config] Initializing Mojarra (1. ant -f C:\\EJB\\EjbComponent -DforceRedeploy=true -Ddirectory.6. Calling C:\jboss-5.0.062 INFO [ServerImpl] Starting JBoss (Microcontainer). ctxPath=/admin-console 16:26:40.jar dist-directory-deploy: pre-run-deploy: Checking data source definitions for missing JDBC drivers.1.0.caches=true run init: deps-jar: compile: library-inclusion-in-archive: Building jar: C:\EJB\EjbComponent\dist\EjbComponent. You'll see the following output in NetBeans .362 INFO [TomcatDeployment] deploy.1-8009 16:26:42..conf.supported=false Dnb..471 INFO [Http11Protocol] Starting Coyote HTTP/1.1.420 INFO [TomcatDeployment] deploy. ctxPath=/ 16:26:42.bat ========================================================================= JBoss Bootstrap Environment JBOSS_HOME: C:\jboss-5.bat -Xms128m -Xmx512m -server CLASSPATH: C:\jboss-5.

.963 INFO [DeployHandler] Begin start..j2ee:jar=EjbComponent. We've chosen name as EjbT ester..service=EJB3) to KernelDeployment of: EjbComponent.LibrarySessionBeanRemote .x Default Remote Business Interface LibrarySessionBean/remote-com.jboss. 16:30:01. service=EJB3_endpoint bean=org.LocalhostTarget@1e4f84ee] Deploying C:\EJB\EjbComponent\dist\ Click Next > button.stateless.File > New Projec t >.LibrarySessionBean ejbName: LibrarySessionBean 16:30:01.281 INFO [JBossASKernel] jndi:LibrarySessionBean/remotecom...EJB3. 16:30:01.LibrarySessionBeanRemote 16:30:01.tutorialspoint.LibrarySessionBean ejbName: LibrarySessionBean 16:30:02. Rig ht click on project name in Project explorer window.jar] .282 INFO [JBossASKernel] jboss.395 INFO [EJBContainer] STARTED EJB: com.endpoint.x Remote Business Interface Create Client to access EJB In NetBeans IDE. Enter project name and location.tutorialspoint.service=EJB3 16:30:01. select .stateless.EndpointImpl properties=[container] constructor=null autowireCandidate=true} ...deployment.282 INFO [JBossASKernel] installing bean: jboss.281 INFO [JBossASKernel] Class:com.281 INFO [JBossASKernel] Added bean(jboss.x Default Remote Business Interface LibrarySessionBean/remote-com.1.jar.stateless.723 INFO [SessionSpecContainer] Starting jboss.impl.service=EJB3 16:30:01.j2ee:jar=EjbComponent.ejb3.jar 16:30:01.j2ee:jar=EjbComponent. 16:30:01.jar. Click Finish > LibrarySessionBean. Project type as J ava Applic ation.jboss.233 INFO [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@12038795{vfszip:/C:/jboss5. Select project type under categ ory.282 INFO [JBossASKernel] with dependencies: 16:30:01. name=LibrarySessionBean.GA/server/default/deploy/EjbComponent.[org.j2ee:jar=EjbComponent.401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI: LibrarySessionBean/remote . Add jboss libraries using Add jar/folder button in c ompile tab. Jboss libraries can be located at <jboss installation folder>> client folder.service=EJB3 16:30:02.tutorialspoint. Select properties .J ava.LibrarySessionBeanRemote .LibrarySessionBeanRemote 16:30:01. [EjbComponent.281 INFO [JBossASKernel] jndi:LibrarySessionBean/remote 16:30:01.394 INFO [SessionSpecContainer] Starting jboss.723 INFO [EJBContainer] STARTED EJB: com.282 INFO [JBossASKernel] and demands: 16:30:01.jar.283 INFO [EJB3EndpointDeployer] Deploy AbstractBeanMetaData@5497cb{ Add ejb component project created earlier under libraries using Add Projec t button in c ompile tab.spi.EJB3.731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI: LibrarySessionBean/remote .x Remote Business Interface 16:30:02.EJB3.jar.stateless..tutorialspoint.stateless.deployers. .EJB3.jar/} .0.ejb:service=EJBTimerService .name=BookMessageHandler.tutorialspoint.jar Applicaton Deployed Operation start started Operation start completed post-run-deploy: run-deploy: run: BUILD SUCCESSFUL (total time: 2 seconds) JBoss Application server log output 16:30:00. 16:30:01.

libraryBean.FileInputStream. import import import import import import import import import com.create jndi.BufferedReader.out.printStackTrace().print("Enter book name: ").properties")). java.naming. String strChoice = brConsoleReader. java.stateless. } private void showGUI(){ System.util.IOException. System.readLine(). EJBTester.test.readLine(). } try { ctx = new InitialContext(props). try { props. System.println("**********************").jboss.List.tutorialspoint.println("**********************") Exit \nEnter Choice: "). .url=localhost Create packag e com. } private void testStatelessEjb(){ try { int choice = 1.interfaces.pkgs=org. } catch (NamingException ex) { ex.out.lookup("LibrarySessionBean/remote"). Properties props.initial=org.naming:org.println("Welcome to Book Store").LibrarySessionBeanRemote. java. javax. { props = new Properties().parseInt(strChoice). class under it. if (choice == 1) { System. javax.printStackTrace().load(new FileInputStream("jndi. showGUI().addBook(bookName).out.tutorialspoint. while (choice != 2) { String bookName.factory. jndi.out. } brConsoleReader = new BufferedReader(new InputStreamReader(System.test and EJBT package com.factory. Add Book\n2. InitialContext ctx.print("Options \n1. } catch (IOException ex) { } public static void main(String[] args) { EJBTester ejbTester = new EJBTester().tutorialspoint.naming.InitialContext.naming. LibrarySessionBeanRemote libraryBean = (LibrarySessionBeanRemote)ctx.util. under project say EjbT ester. public class EJBTester { BufferedReader brConsoleReader = null.url. java.testStatelessEjb() ejbTester.interfaces java. choice = Integer. bookName = brConsoleReader.jnp.NamingContextFactory

getBooks(). }finally { try { if(brConsoleReader !=null){ brConsoleReader. for (int i = 0.get(i)).size()). " + booksList.size()).size().out. } LibrarySessionBeanRemote libraryBean1 = (LibrarySessionBeanRemote)ctx. i < booksList. System. ++i) { System.println(ex.println("Book(s) entered so far: " + booksList. Learn Java ***Using second lookup to get library stateless object*** Book(s) entered so far: 0 BUILD SUCCESSFUL (total time: 13 seconds) In following chapters.out.out. i < booksList1.println(e. Verify the following output in Netbeans console. " + booksList1. System.println( "***Using second lookup to get library stateless object***").get(i)).println((i+1)+". List<String> booksList1 = libraryBean1. in project explorer.getMessage()).out.close().out.}else if (choice == 2) { break. Rig ht click on EJBT ester class and select run file . Add Book 2.println( "Book(s) entered so far: " + booksList1.lookup("LibrarySessionBean/remote"). } } } } Run Client to access EJB Locate EJBT ester. Exit Enter Choice: 1 Enter book name: Learn Java ********************** Welcome to Book Store ********************** Options 1. run: ********************** Welcome to Book Store ********************** Options 1. } } catch (Exception e) { System. Exit Enter Choice: 2 Book(s) entered so far: 1 1.println((i+1)+".getMessage()). we'll cover multiple aspects of this complete ejb application.getBooks(). } } List<String> booksList = libraryBean.printStackTrace().size(). } } catch (IOException ex) { System. ++i) { System. System.out.out. Add Book 2. . for (int i = 0.