Embed presentation
Downloaded 60 times













![Copying 3 files to C:EJBEjbComponentbuildclassesMETA-INF
Created dir: C:EJBEjbComponentbuildempty
Created dir: C:EJBEjbComponentbuildgenerated-sourcesap-source-output
Compiling 2 source files to C:EJBEjbComponentbuildclasses
warning: [options] bootstrap class path not set in conjunction with -source 1.6
Note: C:EJBEjbComponentsrcjavacomtutorialspointstateless
LibraryPersistentBean.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning
compile:
library-inclusion-in-archive:
Created dir: C:EJBEjbComponentdist
Building jar: C:EJBEjbComponentdistEjbComponent.jar
dist:
BUILD SUCCESSFUL (total time: 3 seconds)
Start the Application Server
Select JBoss application server under Servers in Services window
Right click on it to open context menu.
Select start.
You'll see the following output in NetBeans ,output under JBoss Application Server .
Calling C:jboss-5.1.0.GAbinrun.conf.bat
=====================================================================
====
JBoss Bootstrap Environment
JBOSS_HOME: C:jboss-5.1.0.GA
JAVA: C:Program Files (x86)Javajdk1.6.0_21binjava
JAVA_OPTS: -Dprogram.name=run.bat -Xms128m -Xmx512m -server
CLASSPATH: C:jboss-5.1.0.GAbinrun.jar
=====================================================================
====
16:25:50,062 INFO [ServerImpl] Starting JBoss (Microcontainer)...
16:25:50,062 INFO [ServerImpl] Release ID: JBoss [The Oracle] 5.1.0.GA (build:
SVNTag=JBoss_5_1_0_GA date=200905221634)
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-14-2048.jpg)
![16:26:40,420 INFO [TomcatDeployment] deploy, ctxPath=/admin-console
16:26:40,485 INFO [config] Initializing Mojarra (1.2_12-b01-FCS) for context '/admin-console'
16:26:42,362 INFO [TomcatDeployment] deploy, ctxPath=/
16:26:42,406 INFO [TomcatDeployment] deploy, ctxPath=/jmx-console
16:26:42,471 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-127.0.0.1-8080
16:26:42,487 INFO [AjpProtocol] Starting Coyote AJP/1.3 on ajp-127.0.0.1-8009
16:26:42,493 INFO [ServerImpl] JBoss (Microcontainer) [5.1.0.GA (build:
SVNTag=JBoss_5_1_0_GA date=200905221634)] Started in 52s:427ms
Deploy the Project
Select EjbComponent project in Project Explorer window
Right click on it to open context menu.
Select Deploy.
You'll see the following output in NetBeans console output.
ant -f C:EJBEjbComponent -DforceRedeploy=true -Ddirectory.deployment.supported=false -
Dnb.wait.for.caches=true run
init:
deps-jar:
compile:
library-inclusion-in-archive:
Building jar: C:EJBEjbComponentdistEjbComponent.jar
dist-directory-deploy:
pre-run-deploy:
Checking data source definitions for missing JDBC drivers...
Distributing C:EJBEjbComponentdistEjbComponent.jar to
[org.jboss.deployment.spi.LocalhostTarget@1e4f84ee]
Deploying C:EJBEjbComponentdistEjbComponent.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,963 INFO [DeployHandler] Begin start, [EjbComponent.jar]
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-15-2048.jpg)
![16:30:01,233 INFO [Ejb3DependenciesDeployer] Encountered deployment
AbstractVFSDeploymentContext@12038795{vfszip:/C:/jboss-
5.1.0.GA/server/default/deploy/EjbComponent.jar/}
...
16:30:01,281 INFO [JBossASKernel] jndi:LibrarySessionBean/remote-com.
tutorialspoint.stateless.LibrarySessionBeanRemote
16:30:01,281 INFO [JBossASKernel]
Class:com.tutorialspoint.stateless.LibrarySessionBeanRemote
16:30:01,281 INFO [JBossASKernel] jndi:LibrarySessionBean/remote
16:30:01,281 INFO [JBossASKernel] Added bean(jboss.j2ee:jar=EjbComponent.jar,name=
LibrarySessionBean,service=EJB3) to KernelDeployment of: EjbComponent.jar
16:30:01,282 INFO [JBossASKernel] installing bean:
jboss.j2ee:jar=EjbComponent.jar,name=BookMessageHandler,service=EJB3
16:30:01,282 INFO [JBossASKernel] with dependencies:
16:30:01,282 INFO [JBossASKernel] and demands:
16:30:01,282 INFO [JBossASKernel] jboss.ejb:service=EJBTimerService
...
16:30:01,283 INFO [EJB3EndpointDeployer] Deploy
AbstractBeanMetaData@5497cb{name=jboss.j2ee:jar=EjbComponent.jar,
name=LibrarySessionBean, service=EJB3_endpoint
bean=org.jboss.ejb3.endpoint.deployers.impl.EndpointImpl properties=[container]
constructor=null autowireCandidate=true}
...
16:30:01,394 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:01,395 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBean ejbName: LibrarySessionBean
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x
Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBean ejbName: LibrarySessionBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x
Remote Business Interface
Create Client to access EJB
In NetBeans IDE, select ,File > New Project >.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-16-2048.jpg)

![} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testStatelessEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testStatelessEjb(){
try {
int choice = 1;
LibrarySessionBeanRemote libraryBean =
(LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
libraryBean.addBook(bookName);
}else if (choice == 2) {
break;
}
}
List<String> booksList = libraryBean.getBooks();
System.out.println("Book(s) entered so far: " + booksList.size());
for (int i = 0; i < booksList.size(); ++i) {
System.out.println((i+1)+". " + booksList.get(i));
}
LibrarySessionBeanRemote libraryBean1 =
(LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
List<String> booksList1 = libraryBean1.getBooks();
System.out.println(
"***Using second lookup to get library stateless object***");](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-18-2048.jpg)




![ JBoss has automatically created a JNDI entry for our session bean -
LibrarySessionBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.stateless.LibrarySessionBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x
Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBeanRemote ejbName: LibrarySessionBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x
Remote Business Interface
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.LibrarySessionBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-23-2048.jpg)
![import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testStatelessEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testStatelessEjb(){
try {
int choice = 1;
LibrarySessionBeanRemote libraryBean =
LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
while (choice != 2) {](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-24-2048.jpg)





![import javax.ejb.Stateful;
@Stateful
public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote {
List<String> bookShelf;
public LibraryStatefulSessionBean(){
bookShelf = new ArrayList<String>();
}
public void addBook(String bookName) {
bookShelf.add(bookName);
}
public List<String> getBooks() {
return bookShelf;
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryStatefulSessionBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryStatefulSessionBean/remote - EJB3.x Default Remote Business Interface
LibraryStatefulSessionBean/remote-com.
tutorialspoint.stateful.LibraryStatefulSessionBeanRemote - EJB3.x Remote Business
Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryStatefulSessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote ejbName:
LibraryStatefulSessionBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryStatefulSessionBean/remote - EJB3.x Default Remote Business Interface
LibraryStatefulSessionBean/remote-com.
tutorialspoint.stateful.LibraryStatefulSessionBeanRemote - EJB3.x Remote Business
Interface
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-30-2048.jpg)
![EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateful session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;import java.util.List;import java.util.Properties;import
javax.naming.InitialContext;import javax.naming.NamingException; public class EJBTester
{
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-31-2048.jpg)









![public void addBook(Book book) {
entityManager.persist(book);
}
public List<Book> getBooks() {
return entityManager.createQuery("From Book").getResultList();
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryPersistentBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.stateless.LibraryPersistentBeanRemote
JBoss Application server log output
...
16:30:01,401 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
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBeanRemote,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 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
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-41-2048.jpg)
![EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateless.LibraryPersistentBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testEntityEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-42-2048.jpg)



![EJB - Message Driven Beans
A message driven bean is a type of enterprise bean which is invoked by EJB container when it
receives a message from queue or topic. Message driven bean is a stateless bean and is used to
do task asynchronously.
To demonstrate use of message driven bean, we'll make use of ejb-persistence chapter and we're
going to do the following tasks.
Step 1. Create table in database (Refer to EJB-Persistence chapter).
Step 2. Create Entity class corresponding to table (Refer to EJB-Persistence chapter).
Step 3. Create DataSource and Persistence Unit (Refer to EJB-Persistence chapter).
Step 4. Create a stateless ejb having EntityManager instance (Refer to EJB-Persistence chapter).
Step 5. Update stateless ejb.Add methods to add records and get records from database via entity
manager (Refer to EJB-Persistence chapter).
Step 6. Create a Queue named BookQueue in JBoss default application directory.
Step 7. A console based application client will send message to this queue.
Step 8. Create a Message driven bean which will use the stateless bean to persist the client data.
Step 9. EJB Container of jboss will call the above message driven bean and pass it the message
that client will be sending to.
Create Queue
Create a file named jbossmq-destinations-service.xml if not exists in <JBoss Installation
Folder> > server > default > deploy folder.
Here we're creating a queue named BookQueue
jbossmq-destinations-service.xml
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=BookQueue">
<depends optional-attribute-name="DestinationManager">
jboss.mq:service=DestinationManager
</depends>
</mbean>
When you start the JBoss, you'll see the a similar entry in jboss log
...
10:37:06,167 INFO [QueueService] Queue[/queue/BookQueue] started, fullSize=200000,
pageSize=2000, downCacheSize=2000
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-46-2048.jpg)



![import com.tutorialspoint.entity.Book;
import com.tutorialspoint.stateless.LibraryPersistentBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testMessageBeanEjb();
}](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-50-2048.jpg)













![JBoss Application server log output
...
16:30:01,401 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
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBeanRemote ejbName: LibraryPersistentBean
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.LibrarySessionBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-64-2048.jpg)
![try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testEntityEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testEntityEjb(){
try {
int choice = 1;
LibraryPersistentBeanRemote libraryBean =
(LibraryPersistentBeanRemote)
ctx.lookup("LibraryPersistentBean/remote");
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
Book book = new Book();
book.setName(bookName);](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-65-2048.jpg)

![Verify the following output in Netbeans console.
run:
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 1
Enter book name: Learn Java
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 2
Book(s) entered so far: 1
1. Learn Java
BUILD SUCCESSFUL (total time: 13 seconds)
JBoss Application server log output
You can find the following callback entries in JBoss log
14:08:34,293 INFO [STDOUT] postConstruct:: LibraryPersistentBean session bean created with
entity Manager object
...
16:39:09,484 INFO [STDOUT] BookCallbackListener.prePersist:: Book to be created with
book id: 0
16:39:09,531 INFO [STDOUT] BookCallbackListener.postPersist:: Book created with book id:
1
16:39:09,900 INFO [STDOUT] BookCallbackListener.postLoad:: Loaded Book: 1
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-67-2048.jpg)


![import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Timer;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
@Stateless
public class TimerSessionBean implements TimerSessionBeanRemote {
@Resource
private SessionContext context;
public void createTimer(long duration) {
context.getTimerService().createTimer(duration, "Hello World!");
}
@Timeout
public void timeOutHandler(Timer timer){
System.out.println("timeoutHandler : " + timer.getInfo());
timer.cancel();
}
}
TimerSessionBeanRemote.java
package com.tutorialspoint.timer;
import javax.ejb.Remote;
@Remote
public interface TimerSessionBeanRemote {
public void createTimer(long milliseconds);
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -TimerSessionBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.timer.TimerSessionBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
TimerSessionBean/remote - EJB3.x Default Remote Business Interface](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-70-2048.jpg)
![TimerSessionBean/remote-com.tutorialspoint.timer.TimerSessionBeanRemote - EJB3.x
Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=TimerSessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.timer.TimerSessionBeanRemote ejbName: TimerSessionBean
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.TimerSessionBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-71-2048.jpg)
![try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testTimerService();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testTimerService(){
try {
TimerSessionBeanRemote timerServiceBean =
(TimerSessionBeanRemote)ctx.lookup("TimerSessionBean/remote");
System.out.println("["+(new Date()).toString()+ "]" + "timer created.");
timerServiceBean.createTimer(2000);
} catch (NamingException ex) {
ex.printStackTrace();
}
}
}
EJBTester is doing the following tasks.
Load properties from jndi.properties and initialize the InitialContext object.
In testTimerService() method, jndi lookup is done with name - "TimerSessionBean/remote" to
obtain the remote business object (timer stateless ejb).
Then createTimer is invoked passing 2000 milliseconds as schedule time.
EJB Container calls the timeoutHandler method after 2 seconds.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-72-2048.jpg)
![Run Client to access EJB
Locate EJBTester.java in project explorer. Right click on EJBTester class and select run file.
Verify the following output in Netbeans console.
run:
[Wed Jun 19 11:35:47 IST 2013]timer created.
BUILD SUCCESSFUL (total time: 0 seconds)
JBoss Application server log output
You can find the following callback entries in JBoss log
...
11:35:49,555 INFO [STDOUT] timeoutHandler : Hello World!
...
EJB - Dependency Injection
EJB 3.0 specification provides annotations which can be applied on fields or setter methods to
inject dependencies. EJB Container uses the global JNDI registry to locate the dependency.
Following annotations are used in EJB 3.0 for dependency injection.
@EJB - used to inject other EJB reference.
@Resource - used to inject datasource or singleton services like sessionContext, timerService
etc.
Steps to use @EJB
@EJB can be used on fields or on methods in following manner.
public class LibraryMessageBean implements MessageListener {
//dependency injection on field.
@EJB
LibraryPersistentBeanRemote libraryBean;
...
}
public class LibraryMessageBean implements MessageListener {
LibraryPersistentBeanRemote libraryBean;
//dependency injection on method.
@EJB(beanName="com.tutorialspoint.stateless.LibraryPersistentBean")
public void setLibraryPersistentBean(
LibraryPersistentBeanRemote libraryBean)
{](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-73-2048.jpg)



![public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testMessageBeanEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testMessageBeanEjb(){
try {
int choice = 1;
Queue queue = (Queue) ctx.lookup("/queue/BookQueue");
QueueConnectionFactory factory =
(QueueConnectionFactory) ctx.lookup("ConnectionFactory");
QueueConnection connection = factory.createQueueConnection();
QueueSession session = connection.createQueueSession(
false, QueueSession.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender(queue);
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
Book book = new Book();
book.setName(bookName);
ObjectMessage objectMessage =
session.createObjectMessage(book);
sender.send(objectMessage);
} else if (choice == 2) {
break;
}
}
LibraryPersistentBeanRemote libraryBean =](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-77-2048.jpg)





![ JBoss has automatically created a JNDI entry for our session bean - LibraryBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.interceptor.LibraryBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryBean/remote - EJB3.x Default Remote Business Interface
LibraryBean/remote-com.tutorialspoint.interceptor.LibraryBeanRemote - EJB3.x Remote
Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.interceptor.LibraryBeanRemote ejbName: LibraryBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryBean/remote - EJB3.x Default Remote Business Interface
LibraryBean/remote-com.tutorialspoint.interceptor.LibraryBeanRemote - EJB3.x Remote
Business Interface
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.LibraryBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-83-2048.jpg)
![BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testInterceptedEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testInterceptedEjb(){
try {
int choice = 1;
LibraryBeanRemote libraryBean =
LibraryBeanRemote)ctx.lookup("LibraryBean/remote");
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-84-2048.jpg)

![ If user enters 2, system retrieves books using stateless session bean getBooks() method and exits.
Run Client to access EJB
Locate EJBTester.java in project explorer. Right click on EJBTester class and select run file.
Verify the following output in Netbeans console.
run:
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 1
Enter book name: Learn Java
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 2
Book(s) entered so far: 1
1. Learn Java
BUILD SUCCESSFUL (total time: 13 seconds)
JBoss Application server log output
Verify the following output in JBoss Application server log output.
....
09:55:40,741 INFO [STDOUT] *** Intercepting call to LibraryBean method: addBook
09:55:43,661 INFO [STDOUT] *** Intercepting call to LibraryBean method: getBoo](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-86-2048.jpg)





![void addBook(Book bookName);
List<Book> getBooks();
}
LibraryPersistentBean.java
package com.tutorialspoint.stateless;
import com.tutorialspoint.entity.Book;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class LibraryPersistentBean implements LibraryPersistentBeanRemote {
public LibraryPersistentBean(){
}
@PersistenceContext(unitName="EjbComponentPU")
private EntityManager entityManager;
public void addBook(Book book) {
entityManager.persist(book);
}
public List<Book> getBooks() {
return entityManager.createQuery("From Book").getResultList();
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryPersistentBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-92-2048.jpg)
![LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.LibraryBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-93-2048.jpg)
![{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testEmbeddedObjects();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testEmbeddedObjects(){
try {
int choice = 1;
LibraryPersistentBeanRemote libraryBean =
(LibraryPersistentBeanRemote)
ctx.lookup("LibraryPersistentBean/remote");
while (choice != 2) {
String bookName;
String publisherName;
String publisherAddress;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-94-2048.jpg)


![EJB - Blobs/Clobs
EJB 3.0 provides support for Blob and Clob types using @Lob annotation. Following java types
can be mapped using @Lob annotation.
java.sql.Blob
java.sql.Clob
byte[]
String
Serializable Object
@Entity
@Table(name="books")
@EntityListeners(BookCallbackListener.class)
public class Book implements Serializable{
...
private byte[] image;
@Lob @Basic(fetch= FetchType.EAGER)
public byte[] getImage() {
return image;
}](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-97-2048.jpg)

![import com.tutorialspoint.callback.BookCallbackListener;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
@Table(name="book")
public class Book implements Serializable{
private int id;
private String name;
private byte[] image;
private String xml;
public Book(){
}
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Lob @Basic(fetch= FetchType.EAGER)
public byte[] getImage() {](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-99-2048.jpg)
![return image;
}
public void setImage(byte[] image) {
this.image = image;
}
@Lob @Basic(fetch= FetchType.EAGER)
public String getXml() {
return xml;
}
public void setXml(String xml) {
this.xml = xml;
}
}
LibraryPersistentBeanRemote.java
package com.tutorialspoint.stateless;
import com.tutorialspoint.entity.Book;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibraryPersistentBeanRemote {
void addBook(Book bookName);
List<Book> getBooks();
}
LibraryPersistentBean.java
package com.tutorialspoint.stateless;
import com.tutorialspoint.entity.Book;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-100-2048.jpg)
![import javax.persistence.PersistenceContext;
@Stateless
public class LibraryPersistentBean implements LibraryPersistentBeanRemote {
public LibraryPersistentBean(){
}
@PersistenceContext(unitName="EjbComponentPU")
private EntityManager entityManager;
public void addBook(Book book) {
entityManager.persist(book);
}
public List<Book> getBooks() {
return entityManager.createQuery("From Book").getResultList();
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryPersistentBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-101-2048.jpg)

![}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testBlobClob();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testBlobClob(){
try {
int choice = 1;
LibraryPersistentBeanRemote libraryBean =
(LibraryPersistentBeanRemote)
ctx.lookup("LibraryPersistentBean/remote");
while (choice != 2) {
String bookName;
String publisherName;
String publisherAddress;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
String xml = "<book><name>"+bookName+"</name></book>";
Book book = new Book();
book.setName(bookName);
byte[] imageBytes = {0x32, 0x32,0x32, 0x32,0x32,
0x32,0x32, 0x32,
0x32, 0x32,0x32, 0x32,0x32, 0x32,0x32, 0x32,
0x32, 0x32,0x32, 0x32,0x32, 0x32,0x32, 0x32
};](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-103-2048.jpg)
![book.setImage(imageBytes);
book.setXml(xml);
libraryBean.addBook(book);
} else if (choice == 2) {
break;
}
}
List<Book> booksList = libraryBean.getBooks();
System.out.println("Book(s) entered so far: " + booksList.size());
int i = 0;
for (Book book:booksList) {
System.out.println((i+1)+". " + book.getName());
byte[] imageByts = book.getImage();
if(imageByts != null){
System.out.print("image bytes: [");
for(int j = 0; j < imageByts.length ; j++){
System.out.print("0x"
+ String.format("%x", imageByts[j]) +" ");
}
System.out.println("]");
}
System.out.println(book.getXml());
i++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}finally {
try {
if(brConsoleReader !=null){
brConsoleReader.close();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
EJBTester is doing the following tasks.
Load properties from jndi.properties and initialize the InitialContext object.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-104-2048.jpg)
![ In testInterceptedEjb() method, jndi lookup is done with name -
"LibraryPersistenceBean/remote" to obtain the remote business object (stateless ejb).
Then user is shown a library store User Interface and he/she is asked to enter choice.
If user enters 1, system asks for book name and saves the book using stateless session bean
addBook() method. Session Bean is storing the book in database.
If user enters 2, system retrieves books using stateless session bean getBooks() method and exits.
Run Client to access EJB
Locate EJBTester.java in project explorer. Right click on EJBTester class and select run file.
Verify the following output in Netbeans console.
run:
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 1
Enter book name: learn testing
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 2
Book(s) entered so far: 1
1. learn testing
image bytes: [
0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32
0x32 0x32 0x32 0x32 0x32 0x32 0x32 ]
<book><name>learn
testing</name></book>
BUILD SUCCESSFUL (total time: 20
seconds)](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-105-2048.jpg)






![ Binding - This refers to assigning a name to an ejb object which can be used later.
Lookup - This refers to looking up and getting an object of ejb.
In Jboss, session beans are bound in JNDI in following format by default.
local - ejb-name/local
remote - ejb-name/remote
In case, ejb are bundled with <application-name>.ear file then default format is as following.
local - application-name/ejb-name/local
remote - application-name/ejb-name/remote
Example of default binding
Refer to EJB - Create Application chapter's JBoss console output.
JBoss Application server log output
...
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBean ejbName: LibrarySessionBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x
Remote Business Interface
...
Customized binding
Following annotations can be used to customized the default JNDI bindings.
local - org.jboss.ejb3.LocalBinding
remote - org.jboss.ejb3.RemoteBindings
Update LibrarySessionBean.java. Refer to EJB - Create Application chapter
LibrarySessionBean
package com.tutorialspoint.stateless;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-112-2048.jpg)
![@Stateless
@LocalBinding(jndiBinding="tutorialsPoint/librarySession")
public class LibrarySessionBean implements LibrarySessionBeanLocal {
List<String> bookShelf;
public LibrarySessionBean(){
bookShelf = new ArrayList<String>();
}
public void addBook(String bookName) {
bookShelf.add(bookName);
}
public List<String> getBooks() {
return bookShelf;
}
}
LibrarySessionBeanLocal
package com.tutorialspoint.stateless;
import java.util.List;
import javax.ejb.Local;
@Local
public interface LibrarySessionBeanLocal {
void addBook(String bookName);
List getBooks();
}
Build the project. Deploy the application on Jboss and verify the following output in Jboss
console.
...
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBean ejbName: LibrarySessionBean](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-113-2048.jpg)
![16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
tutorialsPoint/librarySession - EJB3.x Default Local Business Interface
tutorialsPoint/librarySession-com.tutorialspoint.stateless.LibrarySessionBeanLocal - EJB3.x
Local Business Interface
...
EJB - Entity Relationships
EJB 3.0 provides option to define database entity relationships/mappings like one to one, one to
many, many to one and many to many relationships. Following are the relevant annotations.
OneToOne - Objects are having one to one relationship. For example, a passenger can travel
using a single ticket at time.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-114-2048.jpg)





![@Stateless
public class LibraryPersistentBean implements LibraryPersistentBeanRemote {
public LibraryPersistentBean(){
}
@PersistenceContext(unitName="EjbComponentPU")
private EntityManager entityManager;
public void addBook(Book book) {
entityManager.persist(book);
}
public List<Book> getBooks() {
return entityManager.createQuery("From Book").getResultList();
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryPersistentBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-120-2048.jpg)

![public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testEmbeddedObjects();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testEmbeddedObjects(){
try {
int choice = 1;
LibraryPersistentBeanRemote libraryBean =
(LibraryPersistentBeanRemote)
ctx.lookup("LibraryPersistentBean/remote");
while (choice != 2) {
String bookName;
String authorName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
System.out.print("Enter author name: ");
authorName = brConsoleReader.readLine();
Book book = new Book();
book.setName(bookName);
Author author = new Author();
author.setName(authorName);
Set<Author> authors = new HashSet<Author>();
authors.add(author);
book.setAuthors(authors);
libraryBean.addBook(book);
} else if (choice == 2) {
break;](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-122-2048.jpg)
![}
}
List<Book> booksList = libraryBean.getBooks();
System.out.println("Book(s) entered so far: " + booksList.size());
int i = 0;
for (Book book:booksList) {
System.out.println((i+1)+". " + book.getName());
System.out.print("Author: ");
Author[] authors = (Author[])books.getAuthors().toArray();
for(int j=0;j<authors.length;j++){
System.out.println(authors[j]);
}
i++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}finally {
try {
if(brConsoleReader !=null){
brConsoleReader.close();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
EJBTester is doing the following tasks.
Load properties from jndi.properties and initialize the InitialContext object.
In testInterceptedEjb() method, jndi lookup is done with name -
"LibraryPersistenceBean/remote" to obtain the remote business object (stateless ejb).
Then user is shown a library store User Interface and he/she is asked to enter choice.
If user enters 1, system asks for book name and saves the book using stateless session bean
addBook() method. Session Bean is storing the book in database.
If user enters 2, system retrieves books using stateless session bean getBooks() method and exits.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-123-2048.jpg)




![import javax.persistence.Query;
@Stateless
public class LibraryPersistentBean implements LibraryPersistentBeanRemote {
public LibraryPersistentBean(){
}
@PersistenceContext(unitName="EntityEjbPU")
private EntityManager entityManager;
public void addBook(Book book) {
entityManager.persist(book);
}
public List<Book> getBooks() {
//create an ejbql expression
String ejbQL = "From Book b where b.name like ?1";
//create query
Query query = entityManager.createQuery(ejbQL);
//substitute parameter.
query.setParameter(1, "%test%");
//execute the query
return query.getResultList();
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryPersistentBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.stateless.LibraryPersistentBeanRemote
JBoss Application server log output
...
16:30:01,401 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](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-128-2048.jpg)
![16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBeanRemote,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 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
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateless.LibraryPersistentBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-129-2048.jpg)
![} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testEntityEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testEntityEjb(){
try {
int choice = 1;
LibraryPersistentBeanRemote libraryBean =
LibraryPersistentBeanRemote)
ctx.lookup("LibraryPersistentBean/remote");
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
Book book = new Book();
book.setName(bookName);
libraryBean.addBook(book);](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-130-2048.jpg)





![}
@PersistenceContext(unitName="EjbComponentPU")
private EntityManager entityManager;
public void addBook(Book book) {
entityManager.persist(book);
}
@WebMethod(operationName="getBooks")
public List<Book> getBooks() {
return entityManager.createQuery("From Book").getResultList();
}
}
JBoss Application 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 [WSDLFilePublisher] WSDL published to: file:/D:/Jboss-
5.0.1/server/default/data/wsdl/EjbComponent.jar/
LibraryService3853081455302946642.wsdl
Create Client to access EJB as Web Service
In NetBeans IDE, select ,File > New Project >.Select project type under category,Java, Project
type asJava Application. Click Next > button.Enter project name and location. Click Finish
> button. We've chosen name as EJBWebServiceClient.
Right click on project name in Project exporer window. Select New > WebService Client .](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-136-2048.jpg)

![Create EJBWebServiceClient.java
package ejbwebserviceclient;
public class EJBWebServiceClient {
public static void main(String[] args) {
}
}
Select Web Service getBooks web method as shown in figure below and drag it to code window
of EJBWebServiceClient.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-138-2048.jpg)

![Update the EJBWebServiceClient code to use this method.
package ejbwebserviceclient;
public class EJBWebServiceClient {
public 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() {](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-140-2048.jpg)







The document provides instructions for setting up an environment to develop and deploy EJB applications using NetBeans IDE. It includes steps to install Java, NetBeans, JBoss application server, and a database. It also explains how to create an EJB project in NetBeans with a sample session bean, build and deploy the project on JBoss. The sample bean allows adding and retrieving books from a list.













![Copying 3 files to C:EJBEjbComponentbuildclassesMETA-INF
Created dir: C:EJBEjbComponentbuildempty
Created dir: C:EJBEjbComponentbuildgenerated-sourcesap-source-output
Compiling 2 source files to C:EJBEjbComponentbuildclasses
warning: [options] bootstrap class path not set in conjunction with -source 1.6
Note: C:EJBEjbComponentsrcjavacomtutorialspointstateless
LibraryPersistentBean.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning
compile:
library-inclusion-in-archive:
Created dir: C:EJBEjbComponentdist
Building jar: C:EJBEjbComponentdistEjbComponent.jar
dist:
BUILD SUCCESSFUL (total time: 3 seconds)
Start the Application Server
Select JBoss application server under Servers in Services window
Right click on it to open context menu.
Select start.
You'll see the following output in NetBeans ,output under JBoss Application Server .
Calling C:jboss-5.1.0.GAbinrun.conf.bat
=====================================================================
====
JBoss Bootstrap Environment
JBOSS_HOME: C:jboss-5.1.0.GA
JAVA: C:Program Files (x86)Javajdk1.6.0_21binjava
JAVA_OPTS: -Dprogram.name=run.bat -Xms128m -Xmx512m -server
CLASSPATH: C:jboss-5.1.0.GAbinrun.jar
=====================================================================
====
16:25:50,062 INFO [ServerImpl] Starting JBoss (Microcontainer)...
16:25:50,062 INFO [ServerImpl] Release ID: JBoss [The Oracle] 5.1.0.GA (build:
SVNTag=JBoss_5_1_0_GA date=200905221634)
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-14-2048.jpg)
![16:26:40,420 INFO [TomcatDeployment] deploy, ctxPath=/admin-console
16:26:40,485 INFO [config] Initializing Mojarra (1.2_12-b01-FCS) for context '/admin-console'
16:26:42,362 INFO [TomcatDeployment] deploy, ctxPath=/
16:26:42,406 INFO [TomcatDeployment] deploy, ctxPath=/jmx-console
16:26:42,471 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-127.0.0.1-8080
16:26:42,487 INFO [AjpProtocol] Starting Coyote AJP/1.3 on ajp-127.0.0.1-8009
16:26:42,493 INFO [ServerImpl] JBoss (Microcontainer) [5.1.0.GA (build:
SVNTag=JBoss_5_1_0_GA date=200905221634)] Started in 52s:427ms
Deploy the Project
Select EjbComponent project in Project Explorer window
Right click on it to open context menu.
Select Deploy.
You'll see the following output in NetBeans console output.
ant -f C:EJBEjbComponent -DforceRedeploy=true -Ddirectory.deployment.supported=false -
Dnb.wait.for.caches=true run
init:
deps-jar:
compile:
library-inclusion-in-archive:
Building jar: C:EJBEjbComponentdistEjbComponent.jar
dist-directory-deploy:
pre-run-deploy:
Checking data source definitions for missing JDBC drivers...
Distributing C:EJBEjbComponentdistEjbComponent.jar to
[org.jboss.deployment.spi.LocalhostTarget@1e4f84ee]
Deploying C:EJBEjbComponentdistEjbComponent.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,963 INFO [DeployHandler] Begin start, [EjbComponent.jar]
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-15-2048.jpg)
![16:30:01,233 INFO [Ejb3DependenciesDeployer] Encountered deployment
AbstractVFSDeploymentContext@12038795{vfszip:/C:/jboss-
5.1.0.GA/server/default/deploy/EjbComponent.jar/}
...
16:30:01,281 INFO [JBossASKernel] jndi:LibrarySessionBean/remote-com.
tutorialspoint.stateless.LibrarySessionBeanRemote
16:30:01,281 INFO [JBossASKernel]
Class:com.tutorialspoint.stateless.LibrarySessionBeanRemote
16:30:01,281 INFO [JBossASKernel] jndi:LibrarySessionBean/remote
16:30:01,281 INFO [JBossASKernel] Added bean(jboss.j2ee:jar=EjbComponent.jar,name=
LibrarySessionBean,service=EJB3) to KernelDeployment of: EjbComponent.jar
16:30:01,282 INFO [JBossASKernel] installing bean:
jboss.j2ee:jar=EjbComponent.jar,name=BookMessageHandler,service=EJB3
16:30:01,282 INFO [JBossASKernel] with dependencies:
16:30:01,282 INFO [JBossASKernel] and demands:
16:30:01,282 INFO [JBossASKernel] jboss.ejb:service=EJBTimerService
...
16:30:01,283 INFO [EJB3EndpointDeployer] Deploy
AbstractBeanMetaData@5497cb{name=jboss.j2ee:jar=EjbComponent.jar,
name=LibrarySessionBean, service=EJB3_endpoint
bean=org.jboss.ejb3.endpoint.deployers.impl.EndpointImpl properties=[container]
constructor=null autowireCandidate=true}
...
16:30:01,394 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:01,395 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBean ejbName: LibrarySessionBean
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x
Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBean ejbName: LibrarySessionBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x
Remote Business Interface
Create Client to access EJB
In NetBeans IDE, select ,File > New Project >.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-16-2048.jpg)

![} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testStatelessEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testStatelessEjb(){
try {
int choice = 1;
LibrarySessionBeanRemote libraryBean =
(LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
libraryBean.addBook(bookName);
}else if (choice == 2) {
break;
}
}
List<String> booksList = libraryBean.getBooks();
System.out.println("Book(s) entered so far: " + booksList.size());
for (int i = 0; i < booksList.size(); ++i) {
System.out.println((i+1)+". " + booksList.get(i));
}
LibrarySessionBeanRemote libraryBean1 =
(LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
List<String> booksList1 = libraryBean1.getBooks();
System.out.println(
"***Using second lookup to get library stateless object***");](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-18-2048.jpg)




![ JBoss has automatically created a JNDI entry for our session bean -
LibrarySessionBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.stateless.LibrarySessionBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x
Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBeanRemote ejbName: LibrarySessionBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x
Remote Business Interface
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.LibrarySessionBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-23-2048.jpg)
![import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testStatelessEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testStatelessEjb(){
try {
int choice = 1;
LibrarySessionBeanRemote libraryBean =
LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
while (choice != 2) {](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-24-2048.jpg)





![import javax.ejb.Stateful;
@Stateful
public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote {
List<String> bookShelf;
public LibraryStatefulSessionBean(){
bookShelf = new ArrayList<String>();
}
public void addBook(String bookName) {
bookShelf.add(bookName);
}
public List<String> getBooks() {
return bookShelf;
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryStatefulSessionBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryStatefulSessionBean/remote - EJB3.x Default Remote Business Interface
LibraryStatefulSessionBean/remote-com.
tutorialspoint.stateful.LibraryStatefulSessionBeanRemote - EJB3.x Remote Business
Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryStatefulSessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote ejbName:
LibraryStatefulSessionBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryStatefulSessionBean/remote - EJB3.x Default Remote Business Interface
LibraryStatefulSessionBean/remote-com.
tutorialspoint.stateful.LibraryStatefulSessionBeanRemote - EJB3.x Remote Business
Interface
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-30-2048.jpg)
![EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateful session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;import java.util.List;import java.util.Properties;import
javax.naming.InitialContext;import javax.naming.NamingException; public class EJBTester
{
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-31-2048.jpg)









![public void addBook(Book book) {
entityManager.persist(book);
}
public List<Book> getBooks() {
return entityManager.createQuery("From Book").getResultList();
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryPersistentBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.stateless.LibraryPersistentBeanRemote
JBoss Application server log output
...
16:30:01,401 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
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBeanRemote,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 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
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-41-2048.jpg)
![EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateless.LibraryPersistentBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testEntityEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-42-2048.jpg)



![EJB - Message Driven Beans
A message driven bean is a type of enterprise bean which is invoked by EJB container when it
receives a message from queue or topic. Message driven bean is a stateless bean and is used to
do task asynchronously.
To demonstrate use of message driven bean, we'll make use of ejb-persistence chapter and we're
going to do the following tasks.
Step 1. Create table in database (Refer to EJB-Persistence chapter).
Step 2. Create Entity class corresponding to table (Refer to EJB-Persistence chapter).
Step 3. Create DataSource and Persistence Unit (Refer to EJB-Persistence chapter).
Step 4. Create a stateless ejb having EntityManager instance (Refer to EJB-Persistence chapter).
Step 5. Update stateless ejb.Add methods to add records and get records from database via entity
manager (Refer to EJB-Persistence chapter).
Step 6. Create a Queue named BookQueue in JBoss default application directory.
Step 7. A console based application client will send message to this queue.
Step 8. Create a Message driven bean which will use the stateless bean to persist the client data.
Step 9. EJB Container of jboss will call the above message driven bean and pass it the message
that client will be sending to.
Create Queue
Create a file named jbossmq-destinations-service.xml if not exists in <JBoss Installation
Folder> > server > default > deploy folder.
Here we're creating a queue named BookQueue
jbossmq-destinations-service.xml
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=BookQueue">
<depends optional-attribute-name="DestinationManager">
jboss.mq:service=DestinationManager
</depends>
</mbean>
When you start the JBoss, you'll see the a similar entry in jboss log
...
10:37:06,167 INFO [QueueService] Queue[/queue/BookQueue] started, fullSize=200000,
pageSize=2000, downCacheSize=2000
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-46-2048.jpg)



![import com.tutorialspoint.entity.Book;
import com.tutorialspoint.stateless.LibraryPersistentBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testMessageBeanEjb();
}](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-50-2048.jpg)













![JBoss Application server log output
...
16:30:01,401 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
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBeanRemote ejbName: LibraryPersistentBean
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.LibrarySessionBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-64-2048.jpg)
![try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testEntityEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testEntityEjb(){
try {
int choice = 1;
LibraryPersistentBeanRemote libraryBean =
(LibraryPersistentBeanRemote)
ctx.lookup("LibraryPersistentBean/remote");
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
Book book = new Book();
book.setName(bookName);](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-65-2048.jpg)

![Verify the following output in Netbeans console.
run:
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 1
Enter book name: Learn Java
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 2
Book(s) entered so far: 1
1. Learn Java
BUILD SUCCESSFUL (total time: 13 seconds)
JBoss Application server log output
You can find the following callback entries in JBoss log
14:08:34,293 INFO [STDOUT] postConstruct:: LibraryPersistentBean session bean created with
entity Manager object
...
16:39:09,484 INFO [STDOUT] BookCallbackListener.prePersist:: Book to be created with
book id: 0
16:39:09,531 INFO [STDOUT] BookCallbackListener.postPersist:: Book created with book id:
1
16:39:09,900 INFO [STDOUT] BookCallbackListener.postLoad:: Loaded Book: 1
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-67-2048.jpg)


![import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Timer;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
@Stateless
public class TimerSessionBean implements TimerSessionBeanRemote {
@Resource
private SessionContext context;
public void createTimer(long duration) {
context.getTimerService().createTimer(duration, "Hello World!");
}
@Timeout
public void timeOutHandler(Timer timer){
System.out.println("timeoutHandler : " + timer.getInfo());
timer.cancel();
}
}
TimerSessionBeanRemote.java
package com.tutorialspoint.timer;
import javax.ejb.Remote;
@Remote
public interface TimerSessionBeanRemote {
public void createTimer(long milliseconds);
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -TimerSessionBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.timer.TimerSessionBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
TimerSessionBean/remote - EJB3.x Default Remote Business Interface](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-70-2048.jpg)
![TimerSessionBean/remote-com.tutorialspoint.timer.TimerSessionBeanRemote - EJB3.x
Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=TimerSessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.timer.TimerSessionBeanRemote ejbName: TimerSessionBean
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.TimerSessionBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-71-2048.jpg)
![try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testTimerService();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testTimerService(){
try {
TimerSessionBeanRemote timerServiceBean =
(TimerSessionBeanRemote)ctx.lookup("TimerSessionBean/remote");
System.out.println("["+(new Date()).toString()+ "]" + "timer created.");
timerServiceBean.createTimer(2000);
} catch (NamingException ex) {
ex.printStackTrace();
}
}
}
EJBTester is doing the following tasks.
Load properties from jndi.properties and initialize the InitialContext object.
In testTimerService() method, jndi lookup is done with name - "TimerSessionBean/remote" to
obtain the remote business object (timer stateless ejb).
Then createTimer is invoked passing 2000 milliseconds as schedule time.
EJB Container calls the timeoutHandler method after 2 seconds.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-72-2048.jpg)
![Run Client to access EJB
Locate EJBTester.java in project explorer. Right click on EJBTester class and select run file.
Verify the following output in Netbeans console.
run:
[Wed Jun 19 11:35:47 IST 2013]timer created.
BUILD SUCCESSFUL (total time: 0 seconds)
JBoss Application server log output
You can find the following callback entries in JBoss log
...
11:35:49,555 INFO [STDOUT] timeoutHandler : Hello World!
...
EJB - Dependency Injection
EJB 3.0 specification provides annotations which can be applied on fields or setter methods to
inject dependencies. EJB Container uses the global JNDI registry to locate the dependency.
Following annotations are used in EJB 3.0 for dependency injection.
@EJB - used to inject other EJB reference.
@Resource - used to inject datasource or singleton services like sessionContext, timerService
etc.
Steps to use @EJB
@EJB can be used on fields or on methods in following manner.
public class LibraryMessageBean implements MessageListener {
//dependency injection on field.
@EJB
LibraryPersistentBeanRemote libraryBean;
...
}
public class LibraryMessageBean implements MessageListener {
LibraryPersistentBeanRemote libraryBean;
//dependency injection on method.
@EJB(beanName="com.tutorialspoint.stateless.LibraryPersistentBean")
public void setLibraryPersistentBean(
LibraryPersistentBeanRemote libraryBean)
{](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-73-2048.jpg)



![public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testMessageBeanEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testMessageBeanEjb(){
try {
int choice = 1;
Queue queue = (Queue) ctx.lookup("/queue/BookQueue");
QueueConnectionFactory factory =
(QueueConnectionFactory) ctx.lookup("ConnectionFactory");
QueueConnection connection = factory.createQueueConnection();
QueueSession session = connection.createQueueSession(
false, QueueSession.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender(queue);
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
Book book = new Book();
book.setName(bookName);
ObjectMessage objectMessage =
session.createObjectMessage(book);
sender.send(objectMessage);
} else if (choice == 2) {
break;
}
}
LibraryPersistentBeanRemote libraryBean =](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-77-2048.jpg)





![ JBoss has automatically created a JNDI entry for our session bean - LibraryBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.interceptor.LibraryBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryBean/remote - EJB3.x Default Remote Business Interface
LibraryBean/remote-com.tutorialspoint.interceptor.LibraryBeanRemote - EJB3.x Remote
Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.interceptor.LibraryBeanRemote ejbName: LibraryBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryBean/remote - EJB3.x Default Remote Business Interface
LibraryBean/remote-com.tutorialspoint.interceptor.LibraryBeanRemote - EJB3.x Remote
Business Interface
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.LibraryBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-83-2048.jpg)
![BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testInterceptedEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testInterceptedEjb(){
try {
int choice = 1;
LibraryBeanRemote libraryBean =
LibraryBeanRemote)ctx.lookup("LibraryBean/remote");
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-84-2048.jpg)

![ If user enters 2, system retrieves books using stateless session bean getBooks() method and exits.
Run Client to access EJB
Locate EJBTester.java in project explorer. Right click on EJBTester class and select run file.
Verify the following output in Netbeans console.
run:
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 1
Enter book name: Learn Java
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 2
Book(s) entered so far: 1
1. Learn Java
BUILD SUCCESSFUL (total time: 13 seconds)
JBoss Application server log output
Verify the following output in JBoss Application server log output.
....
09:55:40,741 INFO [STDOUT] *** Intercepting call to LibraryBean method: addBook
09:55:43,661 INFO [STDOUT] *** Intercepting call to LibraryBean method: getBoo](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-86-2048.jpg)





![void addBook(Book bookName);
List<Book> getBooks();
}
LibraryPersistentBean.java
package com.tutorialspoint.stateless;
import com.tutorialspoint.entity.Book;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class LibraryPersistentBean implements LibraryPersistentBeanRemote {
public LibraryPersistentBean(){
}
@PersistenceContext(unitName="EjbComponentPU")
private EntityManager entityManager;
public void addBook(Book book) {
entityManager.persist(book);
}
public List<Book> getBooks() {
return entityManager.createQuery("From Book").getResultList();
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryPersistentBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-92-2048.jpg)
![LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.LibraryBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-93-2048.jpg)
![{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testEmbeddedObjects();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testEmbeddedObjects(){
try {
int choice = 1;
LibraryPersistentBeanRemote libraryBean =
(LibraryPersistentBeanRemote)
ctx.lookup("LibraryPersistentBean/remote");
while (choice != 2) {
String bookName;
String publisherName;
String publisherAddress;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-94-2048.jpg)


![EJB - Blobs/Clobs
EJB 3.0 provides support for Blob and Clob types using @Lob annotation. Following java types
can be mapped using @Lob annotation.
java.sql.Blob
java.sql.Clob
byte[]
String
Serializable Object
@Entity
@Table(name="books")
@EntityListeners(BookCallbackListener.class)
public class Book implements Serializable{
...
private byte[] image;
@Lob @Basic(fetch= FetchType.EAGER)
public byte[] getImage() {
return image;
}](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-97-2048.jpg)

![import com.tutorialspoint.callback.BookCallbackListener;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
@Table(name="book")
public class Book implements Serializable{
private int id;
private String name;
private byte[] image;
private String xml;
public Book(){
}
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Lob @Basic(fetch= FetchType.EAGER)
public byte[] getImage() {](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-99-2048.jpg)
![return image;
}
public void setImage(byte[] image) {
this.image = image;
}
@Lob @Basic(fetch= FetchType.EAGER)
public String getXml() {
return xml;
}
public void setXml(String xml) {
this.xml = xml;
}
}
LibraryPersistentBeanRemote.java
package com.tutorialspoint.stateless;
import com.tutorialspoint.entity.Book;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibraryPersistentBeanRemote {
void addBook(Book bookName);
List<Book> getBooks();
}
LibraryPersistentBean.java
package com.tutorialspoint.stateless;
import com.tutorialspoint.entity.Book;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-100-2048.jpg)
![import javax.persistence.PersistenceContext;
@Stateless
public class LibraryPersistentBean implements LibraryPersistentBeanRemote {
public LibraryPersistentBean(){
}
@PersistenceContext(unitName="EjbComponentPU")
private EntityManager entityManager;
public void addBook(Book book) {
entityManager.persist(book);
}
public List<Book> getBooks() {
return entityManager.createQuery("From Book").getResultList();
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryPersistentBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-101-2048.jpg)

![}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testBlobClob();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testBlobClob(){
try {
int choice = 1;
LibraryPersistentBeanRemote libraryBean =
(LibraryPersistentBeanRemote)
ctx.lookup("LibraryPersistentBean/remote");
while (choice != 2) {
String bookName;
String publisherName;
String publisherAddress;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
String xml = "<book><name>"+bookName+"</name></book>";
Book book = new Book();
book.setName(bookName);
byte[] imageBytes = {0x32, 0x32,0x32, 0x32,0x32,
0x32,0x32, 0x32,
0x32, 0x32,0x32, 0x32,0x32, 0x32,0x32, 0x32,
0x32, 0x32,0x32, 0x32,0x32, 0x32,0x32, 0x32
};](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-103-2048.jpg)
![book.setImage(imageBytes);
book.setXml(xml);
libraryBean.addBook(book);
} else if (choice == 2) {
break;
}
}
List<Book> booksList = libraryBean.getBooks();
System.out.println("Book(s) entered so far: " + booksList.size());
int i = 0;
for (Book book:booksList) {
System.out.println((i+1)+". " + book.getName());
byte[] imageByts = book.getImage();
if(imageByts != null){
System.out.print("image bytes: [");
for(int j = 0; j < imageByts.length ; j++){
System.out.print("0x"
+ String.format("%x", imageByts[j]) +" ");
}
System.out.println("]");
}
System.out.println(book.getXml());
i++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}finally {
try {
if(brConsoleReader !=null){
brConsoleReader.close();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
EJBTester is doing the following tasks.
Load properties from jndi.properties and initialize the InitialContext object.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-104-2048.jpg)
![ In testInterceptedEjb() method, jndi lookup is done with name -
"LibraryPersistenceBean/remote" to obtain the remote business object (stateless ejb).
Then user is shown a library store User Interface and he/she is asked to enter choice.
If user enters 1, system asks for book name and saves the book using stateless session bean
addBook() method. Session Bean is storing the book in database.
If user enters 2, system retrieves books using stateless session bean getBooks() method and exits.
Run Client to access EJB
Locate EJBTester.java in project explorer. Right click on EJBTester class and select run file.
Verify the following output in Netbeans console.
run:
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 1
Enter book name: learn testing
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 2
Book(s) entered so far: 1
1. learn testing
image bytes: [
0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32 0x32
0x32 0x32 0x32 0x32 0x32 0x32 0x32 ]
<book><name>learn
testing</name></book>
BUILD SUCCESSFUL (total time: 20
seconds)](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-105-2048.jpg)






![ Binding - This refers to assigning a name to an ejb object which can be used later.
Lookup - This refers to looking up and getting an object of ejb.
In Jboss, session beans are bound in JNDI in following format by default.
local - ejb-name/local
remote - ejb-name/remote
In case, ejb are bundled with <application-name>.ear file then default format is as following.
local - application-name/ejb-name/local
remote - application-name/ejb-name/remote
Example of default binding
Refer to EJB - Create Application chapter's JBoss console output.
JBoss Application server log output
...
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBean ejbName: LibrarySessionBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x
Remote Business Interface
...
Customized binding
Following annotations can be used to customized the default JNDI bindings.
local - org.jboss.ejb3.LocalBinding
remote - org.jboss.ejb3.RemoteBindings
Update LibrarySessionBean.java. Refer to EJB - Create Application chapter
LibrarySessionBean
package com.tutorialspoint.stateless;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-112-2048.jpg)
![@Stateless
@LocalBinding(jndiBinding="tutorialsPoint/librarySession")
public class LibrarySessionBean implements LibrarySessionBeanLocal {
List<String> bookShelf;
public LibrarySessionBean(){
bookShelf = new ArrayList<String>();
}
public void addBook(String bookName) {
bookShelf.add(bookName);
}
public List<String> getBooks() {
return bookShelf;
}
}
LibrarySessionBeanLocal
package com.tutorialspoint.stateless;
import java.util.List;
import javax.ejb.Local;
@Local
public interface LibrarySessionBeanLocal {
void addBook(String bookName);
List getBooks();
}
Build the project. Deploy the application on Jboss and verify the following output in Jboss
console.
...
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibrarySessionBean ejbName: LibrarySessionBean](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-113-2048.jpg)
![16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
tutorialsPoint/librarySession - EJB3.x Default Local Business Interface
tutorialsPoint/librarySession-com.tutorialspoint.stateless.LibrarySessionBeanLocal - EJB3.x
Local Business Interface
...
EJB - Entity Relationships
EJB 3.0 provides option to define database entity relationships/mappings like one to one, one to
many, many to one and many to many relationships. Following are the relevant annotations.
OneToOne - Objects are having one to one relationship. For example, a passenger can travel
using a single ticket at time.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-114-2048.jpg)





![@Stateless
public class LibraryPersistentBean implements LibraryPersistentBeanRemote {
public LibraryPersistentBean(){
}
@PersistenceContext(unitName="EjbComponentPU")
private EntityManager entityManager;
public void addBook(Book book) {
entityManager.persist(book);
}
public List<Book> getBooks() {
return entityManager.createQuery("From Book").getResultList();
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryPersistentBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote
JBoss Application server log output
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.interceptor.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote -
EJB3.x Remote Business Interface
...](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-120-2048.jpg)

![public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testEmbeddedObjects();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testEmbeddedObjects(){
try {
int choice = 1;
LibraryPersistentBeanRemote libraryBean =
(LibraryPersistentBeanRemote)
ctx.lookup("LibraryPersistentBean/remote");
while (choice != 2) {
String bookName;
String authorName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
System.out.print("Enter author name: ");
authorName = brConsoleReader.readLine();
Book book = new Book();
book.setName(bookName);
Author author = new Author();
author.setName(authorName);
Set<Author> authors = new HashSet<Author>();
authors.add(author);
book.setAuthors(authors);
libraryBean.addBook(book);
} else if (choice == 2) {
break;](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-122-2048.jpg)
![}
}
List<Book> booksList = libraryBean.getBooks();
System.out.println("Book(s) entered so far: " + booksList.size());
int i = 0;
for (Book book:booksList) {
System.out.println((i+1)+". " + book.getName());
System.out.print("Author: ");
Author[] authors = (Author[])books.getAuthors().toArray();
for(int j=0;j<authors.length;j++){
System.out.println(authors[j]);
}
i++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}finally {
try {
if(brConsoleReader !=null){
brConsoleReader.close();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
EJBTester is doing the following tasks.
Load properties from jndi.properties and initialize the InitialContext object.
In testInterceptedEjb() method, jndi lookup is done with name -
"LibraryPersistenceBean/remote" to obtain the remote business object (stateless ejb).
Then user is shown a library store User Interface and he/she is asked to enter choice.
If user enters 1, system asks for book name and saves the book using stateless session bean
addBook() method. Session Bean is storing the book in database.
If user enters 2, system retrieves books using stateless session bean getBooks() method and exits.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-123-2048.jpg)




![import javax.persistence.Query;
@Stateless
public class LibraryPersistentBean implements LibraryPersistentBeanRemote {
public LibraryPersistentBean(){
}
@PersistenceContext(unitName="EntityEjbPU")
private EntityManager entityManager;
public void addBook(Book book) {
entityManager.persist(book);
}
public List<Book> getBooks() {
//create an ejbql expression
String ejbQL = "From Book b where b.name like ?1";
//create query
Query query = entityManager.createQuery(ejbQL);
//substitute parameter.
query.setParameter(1, "%test%");
//execute the query
return query.getResultList();
}
}
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean -
LibraryPersistentBean/remote.
We'll using this lookup string to get remote business object of type -
com.tutorialspoint.stateless.LibraryPersistentBeanRemote
JBoss Application server log output
...
16:30:01,401 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](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-128-2048.jpg)
![16:30:02,723 INFO [SessionSpecContainer] Starting
jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBeanRemote,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB:
com.tutorialspoint.stateless.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 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
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
These properties are used to initialize the InitialContext object of java naming service
InitialContext object will be used to lookup stateless session bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateless.LibraryPersistentBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-129-2048.jpg)
![} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testEntityEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options n1. Add Bookn2. Exit nEnter Choice: ");
}
private void testEntityEjb(){
try {
int choice = 1;
LibraryPersistentBeanRemote libraryBean =
LibraryPersistentBeanRemote)
ctx.lookup("LibraryPersistentBean/remote");
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
Book book = new Book();
book.setName(bookName);
libraryBean.addBook(book);](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-130-2048.jpg)





![}
@PersistenceContext(unitName="EjbComponentPU")
private EntityManager entityManager;
public void addBook(Book book) {
entityManager.persist(book);
}
@WebMethod(operationName="getBooks")
public List<Book> getBooks() {
return entityManager.createQuery("From Book").getResultList();
}
}
JBoss Application 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 [WSDLFilePublisher] WSDL published to: file:/D:/Jboss-
5.0.1/server/default/data/wsdl/EjbComponent.jar/
LibraryService3853081455302946642.wsdl
Create Client to access EJB as Web Service
In NetBeans IDE, select ,File > New Project >.Select project type under category,Java, Project
type asJava Application. Click Next > button.Enter project name and location. Click Finish
> button. We've chosen name as EJBWebServiceClient.
Right click on project name in Project exporer window. Select New > WebService Client .](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-136-2048.jpg)

![Create EJBWebServiceClient.java
package ejbwebserviceclient;
public class EJBWebServiceClient {
public static void main(String[] args) {
}
}
Select Web Service getBooks web method as shown in figure below and drag it to code window
of EJBWebServiceClient.](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-138-2048.jpg)

![Update the EJBWebServiceClient code to use this method.
package ejbwebserviceclient;
public class EJBWebServiceClient {
public 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() {](https://image.slidesharecdn.com/ejbtutorial-140903034438-phpapp02/75/Free-EJB-Tutorial-VirtualNuggets-140-2048.jpg)





