This sample will give you a simple working experience with EJB client server . here I’m using netbeans as IDE and JBoss 5.1 as my EJB Container. and I created seperate client for Stateful an Stateless EJB sessions in order to make easy the development .We'll create a ejb module project named EJBTestServer.
1. In NetBeans IDE, select ,File > New Project Select project type under category,Java EE, Project type as Ejb Module. Click Next > button. Enter project name as EJBTestServer and location. Click Next > button.
Select Server as JBoss Application Server. Click Finish button.
above step will give the simple skeleton for the ejb project. our next step is to add the beans to ejb so lets create a Session Bean.
Create a sample EJB
To create a simple EJB, we'll use NetBeans "New" wizard. In example below, We'll create a stateless ejb class named NewSessionBean under EJBTestServer project. Select project EJBTestServer in project explorer window and right click on it. Select, New > Session Bean.Enter session bean name and package name. Click Finish button. You'll see the following ejb classes created by NetBeans.
- NewSessionBean - stateless session bean
- NewSessionBeanLocal - local interface for session bean
NewSessionBeanLocal is renamed to NewSessionBeanRemote and NewSessionBean implements NewSessionBeanRemote interface.
NewSessionBeanRemote.java
package org1; import javax.ejb.Remote; /** * * @author rajjaz */ @Remote public interface NewSessionBeanRemote { public String display(); public String sayHello(String name); }
NewSessionBean.java
package org1; import javax.ejb.Stateless; /** * * @author rajjaz */ @Stateless public class NewSessionBean implements NewSessionBeanRemote { // Add business logic below. (Right-click in editor and choose // "Insert Code > Add Business Method") public String display(){ return "Hello JBoss"; } public String sayHello(String name){ this.count++; return "Hello " + name; } }
Do the above way to create the bean for the Stateful Session also
- NewStatefulSessionBean - stateful session bean
- NewStatefulSessionBeanRemote - local interface for session bean
NewStatefulSessionBeanRemote.java
package org1; import javax.ejb.Remote; /** * * @author rajjaz */ @Remote public interface NewStatefulSessionBeanRemote { void increment(); void decrement(); int getCount(); }
NewStatefulSessionBean.java
package org1; import javax.ejb.Remote; import javax.ejb.Stateful; /** * * @author rajjaz */ @Stateful @Remote(NewStatefulSessionBeanRemote.class) public class NewStatefulSessionBean implements NewStatefulSessionBeanRemote { // Add business logic below. (Right-click in editor and choose // "Insert Code > Add Business Method") private int count = 0; @Override public void increment() { this.count++; } @Override public void decrement() { this.count--; } @Override public int getCount() { return this.count; } }
Now Right Click on project and click on Clean and Build then you will get the below output
ant -f /home/rajjaz/NetBeansProjects/EJBTestServer -Dnb.internal.action.name=rebuild clean dist
init:
undeploy-clean:
deps-clean:
Deleting directory /home/rajjaz/NetBeansProjects/EJBTestServer/build
Deleting directory /home/rajjaz/NetBeansProjects/EJBTestServer/dist
clean:
init:
deps-jar:
Created dir: /home/rajjaz/NetBeansProjects/EJBTestServer/build/classes
Copying 2 files to /home/rajjaz/NetBeansProjects/EJBTestServer/build/classes/META-INF
Created dir: /home/rajjaz/NetBeansProjects/EJBTestServer/build/empty
Compiling 4 source files to /home/rajjaz/NetBeansProjects/EJBTestServer/build/classes
compile:
library-inclusion-in-archive:
Created dir: /home/rajjaz/NetBeansProjects/EJBTestServer/dist
Building jar: /home/rajjaz/NetBeansProjects/EJBTestServer/dist/EJBTestServer.jar
dist:
BUILD SUCCESSFUL (total time: 0 seconds)
next to that deply the project. if project was successfully deployed you will get the below output
14:33:30,677 INFO [EJBContainer] STARTED EJB: org1.NewSessionBean ejbName: NewSessionBean
14:33:30,680 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
NewSessionBean/remote - EJB3.x Default Remote Business Interface
NewSessionBean/remote-org1.NewSessionBeanRemote - EJB3.x Remote Business Interface
14:33:30,693 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EJBTestServer.jar,name=NewStatefulSessionBean,service=EJB3
14:33:30,693 INFO [EJBContainer] STARTED EJB: org1.NewStatefulSessionBean ejbName: NewStatefulSessionBean
14:33:30,697 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
NewStatefulSessionBean/remote - EJB3.x Default Remote Business Interface
NewStatefulSessionBean/remote-org1.NewStatefulSessionBeanRemote - EJB3.x Remote Business Interface
14:33:38,933 WARN [InterceptorsFactory] EJBTHREE-1246: Do not use InterceptorsFactory with a ManagedObjectAdvisor, InterceptorRegistry should be used via the bean container
14:33:38,933 WARN [InterceptorsFactory] EJBTHREE-1246: Do not use InterceptorsFactory with a ManagedObjectAdvisor, InterceptorRegistry should be used via the bean container
That's all now our project was successfully deployed on Jboss Server our next step step is create simple client to test this server
Server Source Code
Create a simple Client
No comments:
Post a Comment