220 likes | 382 Views
TDDD05 EJB Lab (Part of slides reused from Mikhail’s). Lu Li lu.li@liu.se. Agenda. EJB brief introduction Practical hints. Why EJB. EJBs is a powerful component model for building distributed, server-side, and Java-based enterprise application components.
E N D
TDDD05 EJB Lab(Part of slides reused from Mikhail’s) Lu Li lu.li@liu.se
Agenda • EJB brief introduction • Practical hints
Why EJB • EJBs is a powerful component model for building distributed, server-side, and Java-based enterprise application components. • Code reuse by middleware service, such as object life-cycle management, resource (beans, database connection) mapping etc.
Connect Glassfish to Database • Configure MySQL database • Copy driver to Glassfish directory • Configure a connection pool (For example “TDDD05_MySQL”), and do a ping test • Configure JDBC resource (For example “TDDD05_JDBC”) to use the connection pool (“TDDD05_MySQL”)
Create an JPA to use database • JPA is a kind of EJB for interaction with database. • Configure persistence.xml under META-INF folder, if no such xml file found, create one.
Create an Stateless Bean • Create a EJB project • Add library appserv-rt.jar, j2ee.jar
Create an Stateless Bean • Create an interface
Create an Stateless Bean • Create an implementation
Deploy EJB • Export jar • Start Glassfish • Copy jar file into the autodeploy directory • Check the file generated • There is another way to deploy. (Covered later)
DI Application • Deployment (Glassfish_Home)/bin/asadmin deploy –retrieve . Ex1-ee.ear • Execution (Glassfish_Home)/bin/Appclient –client ./ex1-eeClient.jar –mainclassclient.AppClient
Summary • You needs a MySQL with a designed table. • You needs to make a Glassfish to know the connection to your MySQL • You needs a JPA to use the connection and interact with MySQL • You needs a Stateless session bean to use the JPA.
Summary • You needs to declare an interface to expose functionality to external clients. • You needs a JNDI lookup client which can run independently • You needs a DI client in connection with EJB (Two components are package in EAR project), to be deployed in Glassfish and invoke them by Appclient.
Some suggestions • Try to understand how different components interact with each other at implementation level. • Do a thorough unit testing before integrating all components together • Today’s lesson covers most but not all steps needed for lab 2. Feel free to change some steps if you think it is necessary.