Title: Developing an Enterprise Application using J2EE 5 and EJB3.0
1Developing an Enterprise Application using J2EE 5
and EJB3.0
- B. Ramamurthy
- This presentation is based on tutorials available
with Netbeans IDE 5.5 (www.netbeans.org and - http//testwww.netbeans.org/kb/55/ejb30.html)
2Creating an Enterprise Application
- File?New Project
- Select Enterprise Category, Enterprise
Application ? next - Name project ENewsApp, set server to JSAS
- Set J2EE version to EE5, click create EJB module
and create web application module - Click Finish
3Creating a Persistence Unit
- This will name the persistence unit name, data
source and persistence provider. In your case,
you may substitute the entity class and
persistence with DAO and/or direct access to
database from the EJBs. - Right click of EJB module of the project, choose
New?File/Folder - From Persistence category, select Persistent ?
Next - For Persistence Provider, choose TopLink
- For data source, choose JNDI name jdbc/sample
(This could be replaced by JNDI name for your
Oracle data source) - Check Use Java Transaction APIs to make it
transactional, Finish.
4Creating Entity Class
- Right click of EJB module of the project, choose
New?File/Folder - From Persistence category, select Entity class ?
Next - Type ENewsEntity for class name, ejb for the
package, Primary key type Long, Finish. - Add to the class
- String title
- String body
- Right click and refactor to generate getter and
setter for the members, id, title and body.
5Creating Message Driven Bean
- Right click of EJB module of the project, choose
New?File/Folder - From Enterprise category, select Message-Driven
Beans class ? Next - Type NewsMessage for name, slect ejb for package
name. - Select Queue as Destination Type, Finish.
- In the source editor map this bean to the JMS
queue resource referenced by its JNDI name. - Add content to onMessage method and code to
extract and save (persist) the message pushed on
to the queue by the user interface.
6Creating a Session Bean
- Right click of EJB module of the project, choose
New?File/Folder - From Enterprise category, select Session bean ?
Next - Choose NewsEntity class click Add, Next
- Check ejb as package and local interface, Finish.
7Web Module
- Web module consists of two ListNews and
PostMessage servlets. - ListNews gets the list of messages from the
database (thru session bean and entity bean) and
lists them - PostMessage servlet gathers the message input by
the user.
8Creating ListNews Servlet
- Right-click on the web module and choose New
?Servlet. - Type ListNews for class name
- Enter web for Package name and Finish.
- In the source code window, right click and select
Enterprise Resources? Call Enterprise Bean - Select NewsEntityFacade and click OK.
- Update the processRequest method and fix imports
9processRequest
List news newsEntityFacade.findAll()
for (Iterator it news.iterator()it.hasNext())
NewsEntity elem
(NewsEntity)it.next() out.println("
ltbgt" elem.getTitle() " lt/bgtltbr /gt")
out.println(elem.getBody() "ltbr /gt")
out.println("lta href'PostMessage'gtAdd
new messagelt/agt")
10Creating PostMessage
- JMS resources will be directly injected into the
servlet. - Create and add a new servlet PostMessage to web
module. - In the source editor add annotations to inject
ConnectionFactory and Queue resource - _at_Resource(mappedName"jms/NewMessageFactory")
- private ConnectionFactory connectionFactory
-
- _at_Resource(mappedName"jms/NewMessage")
- private Queue queue
11(contd.)
- Add code to processRequest methodString
titlerequest.getParameter("title") - String bodyrequest.getParameter("body")
- if ((title!null) (body!null))
- try
- Connection connection
connectionFactory.createConnection() - Session session connection.createSession
(false, Session.AUTO_ACKNOWLEDGE) - MessageProducer messageProducer
session.createProducer(queue) -
12(contd.)
- ObjectMessage message session.createObjectMessag
e() - // here we create NewsEntity, that will
be sent in JMS message - NewsEntity e new NewsEntity()
- e.setTitle(title)
- e.setBody(body)
- message.setObject(e)
- messageProducer.send(message)
- messageProducer.close()
- connection.close()
- response.sendRedirect("ListNews")
13Contd.
- Uncomment the print commands and fix imports.
- Projects Windows, right click on ENewsApp and
select Properties. - Select Run propery, and type relative URL to be
/ListNews, OK - Build and run the project.
14Architectural Model of News Application
Application
EJB Module
Web Module
ListNews Servlet
PostNews Servlet
Message-drivenBean
MsgQueue
SessionBean
EntityBean
Persistence API