Title: JBoss Fundamentals with JBuilder: Session
1JBoss Fundamentals with JBuilder Session 3110
Ken Sipe Code Mentor, Inc.Nov. 5, 2005
2Agenda
- JBoss Introduction
- JBuilder JBoss Server Configuration
- JBoss Services
- EJB 3 Introduction
- Annotations
- EJB Annotation
- Persistence Model
3What is JBoss?
- Open Source J2EE Application Server
- Versions
- 2.4.x
- J2EE 1.3 Spec
- 4.0.1
- J2EE 1.4 Spec
- Refactor Server
- 4.0.3 (current)
- EJB 3 Capable
4JBoss Installation
- Version lt 4.0.3
- unzip or extract tar to a directory
- /usr/local sudo tar xf jboss-4.0.3.tar
- Version 4.0.3
- New Java-based installer
- From Web Start or
- /downloads jar ./jboss-4.0.3RC1-installer.jar
5JBoss Directory Structure
- jboss-4.0.3
- bin run / stops scripts
- client client needed jars
- docs dtds, schemas and examples
- lib server side jars
- server
- default
- conf server instance configuration files
- data server instance db file (hypersonic)
- deploy deployment directory
- lib server instance libraries
- log server instance logs
- tmp location deployments are extracted to
- work web work directory
6Configuration of JBuilder
- Enterprise --gt Configure Servers
- Select Jboss 4.0.1
- Configure home directory
7Starting a Project
- Copy the preferred server configuration
- Create JB project select this new server
configuration for the deployment. - Create EJBs... etc.
8Managing JBoss
- Jboss is running and ready when you see
- 145911,304 INFO Server JBoss (MX
MicroKernel) 4.0.3 (build CVSTagJBoss_4_0_3
date200510042341) Started in 34s106ms - Web administration
- http//localhost8080/
- Tomcat status
- JMX Console
- Jboss Web Console
9DEMO Stateless Session
10Tricks and Tips
- Multi-Server
- List deployments
- Disable services
11Multi-Server
- Configure JBoss for EJBs
- Configure Tomcat for WAR.
DEMO
12Agenda
- JBoss Introduction
- JBuilder JBoss Server Configuration
- JBoss Services
- EJB 3 Introduction
- Annotations
- EJB Annotation
- Persistence Model
13SAR Service ARchive
- JAR with a SAR extension
- JBoss specific
- JMX components
14Creating SAR
- Create JMX MBean
- Create SAR
- New Object Gallery
- Select Enterprise --gt Jboss Service Module
- Assign Mbean class
- Assign Name
- JBuilder is misleading the name must have a
domain!!!
15Agenda
- JBoss Introduction
- JBuilder JBoss Server Configuration
- JBoss Services
- EJB 3 Introduction
- Annotations
- EJB Annotation
- Persistence Model
16Because of EJBs...
- I became an expert at
- class loading
- Transaction management
- I got to jump through hoops
- ejbPassivate.... for my stateless session bean
??? - In the end ...
- was I more productive?
- was the end result portable?
17EJB 3 Specification
- JSR 220
- public Review 27 Jun, 2005
- 3 Specification Documents
- EJB 3.0 Simplified API
- Quick Reference with great sample code
- Java Persistence API
- Lightweight (replacement) persistence framework
- EJB Core Contracts
- EJB Details outside of persistence
18EJB 3 Contributions
- The EJB 2.1 Specification (JSR 153, Enterprise
JavaBeans 2.1) - The J2EE 1.4 Platform Specification (JSR 151)
- JSR 14 Add Generic Types to the JavaTM
Programming Language - JSR 175 A Metadata Facility for the JavaTM
Programming Language - JSR 181 Web Services Metadata for the JavaTM
Platform - JSR 201 Enumerations, Autoboxing, Enhanced for
loops and Static Import - JAX-RPC 2.0
- JDBC 4.0
19EJB 3 Goals
- The EJB 3.0 release is focused on a
simplification of the Enterprise JavaBeans
architecture from the developers point of view. - Elimination of requirements for
- home and component interfaces
- the specification of the javax.ejb.EnterpriseBean
interfaces. - Definition of a dependency injection
- Facility and simpler look-up APIs.
- Java metadata annotations vs. deployment
descriptors. - Simplification of object persistence by the
definition of a light-weight object/relational
mapping - facility based on the direct use of Java classes
rather than persistent components.
20Agenda
- EJB 3 Introduction
- Annotations
- EJB Annotation
- Persistence Model
21MetaData is Required in EJB 3
- Annotations (JSR-175) were incorporated in JDK
1.5 - Annotations seem like JavaDoc but are quite
different.
Annotations _at_deprecated ??? public void
doThis() ...
JavaDoc / _at_deprecated / public void
doThis() ...
22Annotation Types
- Marker Annotations
- _at_MarkerAnnotation
- Single-value Annotations
- _at_SingleValueAnnotation(value)
- Full Annotations
- _at_FullAnnotation( value1x, value2y)
23JDK Built-in Annotations
- Override
- Marker to indicate the intention to override
- Deprecated
- Marker (which is a duplication of the _at_deprecated
javadoc) - SuppressWarnings
- Suppresses warnings
public class DeprecatedUser2
_at_SuppressWarnings("deprecation") public
static void main(String args)
DeprecatedExample2.foo()
24Agenda
- EJB 3 Introduction
- Annotations
- EJB Annotation
- Persistence Model
25EJB 3 Annotations
- _at_EJB
- _at_Stateless
- _at_Stateful
- _at_Remote
- _at_NamedQuery
- _at_MessageDriven
- _at_ActivationConfigProperty
- _at_TransactionManagement
- _at_TransactionAttribute
- _at_RemoteHome
- _at_Local
- _at_LocalHome
- _at_Interceptors
- _at_AroundInvoke
- _at_Inject
- _at_Resource
- _at_EJBs
26EJB 3 Annotations
- _at_CallbackListener
- _at_PostConstruct
- _at_Timeout
- _at_ApplicationException
- _at_RolesReferenced
- _at_RolesAllowed
- _at_PermitAll
- _at_DenyAll
- _at_RunAs
- _at_SecurityRoles
- _at_Init
- _at_PreDestroy
- _at_PrePassivate
- _at_PostConstruct
- _at_PostActivate
- _at_Remove
27Field Level Injection
package com.codementor.devcon import
javax.ejb. import javax.annotation. _at_Statef
ul public class StatefulBean implements
RemoteStatefulInterface _at_Resource
javax.sql.DataSource ejbDS _at_Resource
javax.mail.Session ejbmailSession
- Resource evaluates to JNDI name of resource
- java/com/env/com.codementor.devcon/ejbDS
- Alternatively it can be declared
- _at_Resource(name"jdbc/sqltestDB",typejavax.sql.Dat
aSource.class)
28Setter Injection
- Same example on the setter of the EJB
_at_Resource(name"jdbc/customerDB") public
void setDataSource(DataSource myDB) this.ds
myDB private DataSource myDB
29EJB References
- The following are examples of references to EJBs
in JEE 5.
_at_EJB LocalStatelessInterface sless
_at_EJB(name"ejb/stateless")
LocalStatelessInterface sless
_at_EJB(name"ejb/stateless") RemoteStatelessHome
slessHome
30Explicit Dependency Lookup
EJB 2.1 Initial Context icnew
InitialContext() Object oic.lookup("ejb/First
Stateless") statelessHome
(StatelessRemoteHome)PortableRemoteObject.narrow(o
bjref, StatelessRemoteHome.class) EJB
3.0 _at_Resource SessionContect sc
public void setup() FirstStatelessInterface
fi(FirstStatelessInterface)sc.lookup("ejb/
FirstStateless")
31Stateless Session Example
_at_Stateless public class ConferenceBean implements
Conference _at_Resource private
EntityManager em private ConfSession
session public ConfSession
findSessionByID(int sessionID) return
((ConfSession) em.find(ConfSession.class, sess
ionID)) public void addSession(long
sessionID, String title, Date date)
session new ConfSession()
session.setTitle(title) session.setDate(dat
e) session.setId(sessionID)
em.persist(session)
32Agenda
- EJB 3 Introduction
- Annotations
- EJB Annotation
- Persistence Model
33Persistence Framework
- Packaging
- Annotations / Mapping
- API EntityManager
34There's a new Archive in Town - PAR
- PAR Persistence Archive
- Contains persistence units
- Portability...
- Contains persistence.xml
- Details for the entity manager
- provider
- jta-data-source
- non-jta-data-source
- mapping-file
- jar-file
- class
- properties
35Persistence.xml
ltentity-managergt ltnamegtem1lt/namegt ltprovidergtcom.
acme.persistencelt/providergt ltjta-data-sourcegtjdbc
/MyDBlt/jta-data-sourcegt ltmapping-filegtormap.xmllt/
mapping-filegt ltjar-filegtMyApp.jarlt/jar-filegt ltcl
assgtcom.widgets.Orderlt/classgt ltclassgtcom.widgets.
Customerlt/classgt ltpropertiesgt ltproperty
name"sql-logging" value"on"/gt lt/propertiesgt lt/e
ntity-managergt
36With or Without You
- Any PAR without a persistence.xml will get the
container defaults.
37Persistence Framework
- Packaging
- Annotations / Mapping
- API EntityManager
38EJB 3 Persistence
- Good Bye Entity Beans!!!
- EJB 3 Provides an ORM framework similar to
Hibernate / JDO - No Descriptors necessary
- Annotated POJOs are the persistent classes
39Entity Annotations
- _at_Entity
- named used to refer to persistent class
- name property defaults to name of class
- access property or field access
- Callbacks
- _at_PrePersist
- _at_PostPersist
- _at_PreRemove
- _at_PostRemove
- _at_PreUpdate
- _at_PostUpdate
- _at_PostLoad
40Entity Annotations
- _at_PersistenceContext
- Provides EntityManager
- _at_PersistenceUnit
- Provides EntityManagerFactory
- _at_EntityTransaction
41Life-Cycle Method Example
_at_Entity _at_EntityListener(com.acme.AlertMonitor.clas
s) public class AccountBean implements Account
Long accountId Integer balance boolean
preferred public Long getAccountId() ...
public Integer getBalance() ...
_at_Transient // because status depends upon
non-persistent context public boolean
isPreferred() ... public void
deposit(Integer amount) ... public Integer
withdraw(Integer amount) throws NSFException ...
_at_PrePersist public void validateCreate()
if (getBalance() lt MIN_REQUIRED_BALANCE) th
row new AccountException("Insufficient balance to
open an account") _at_PostLoad public void
adjustPreferredStatus() preferred
(getBalance() gt AccountManager.getPreferredStatu
sLevel())
42Life-Cycle Example Complete
public class AlertMonitor _at_PostPersist public
void newAccountAlert(Account acct)
Alerts.sendMarketingInfo(acct.getAccountId(),
acct.getBalance())
43Persistence Framework
- Packaging
- Annotations / Mapping
- API EntityManager
44EJB3 Persistence Interfaces
- EntityManager
- Interface to interact with persistence context.
- EntityManagerFactory
- Creates an EntityManager
- Query
- Queries made through this interface
45EJB Bean Getting an Entity Manager
Example 1 _at_PersistenceContext public
EntityManager em
Example 2 _at_PersistenceContext(unitName"order") E
ntityManager em //here only one persistence unit
exists _at_PersistenceContext(typePersistenceContext
Type.EXTENDED) EntityManager orderEM
46Persisting with the Manager
public void addSession(long sessionID, String
title, Date date) session new
ConfSession() session.setTitle(title)
session.setDate(date)
session.setId(sessionID)
em.persist(session)
47Querying the Manager
_at_Stateless public class ConferenceEJB implements
Conference ... public ConfSession
findSessionByTitle(String title) String query
"SELECT OBJECT(session) FROM ConfSession
conSession WHERE conSession.title
sessionName" ConfSession session
(ConfSession)em.createQuery(query)
.setParameter("sessionName", title) .getSingle
Result() return session
48EJB Sample Code
_at_Stateless public class ShoppingCartImpl
implements ShoppingCart _at_PersistenceContext
EntityManager em public Order getOrder(Long id)
return em.find(Order.class, id) public
Product getProduct(String name) return
(Product) em.createQuery("select p from Product
p where p.name name") .setParameter("name
", name) .getSingleResult() public
LineItem createLineItem(Order order, Product
product, int quantity) LineItem li new
LineItem(order, product, quantity) order.getLin
eItems().add(li) em.persist(li) return
li
Notice whats missing!
49Agenda
- EJB 3 Introduction
- Annotations
- EJB Annotation
- Persistence Model
50EAR Configuration
- EAR
- contains an application.xml in the META-INF
- contains EJB, PAR and WAR modules
- PAR
- contains persistence.xml... maybe
- contains persistence classes
- EJB
- contains EJB classes
- WAR
- contains JSP, servlet classes
- web.xml
51Questions
52 Session 3110 JBoss Fundamentals with JBuilder
Thank You
Thank You
Thank You
Thank You
- 3110
- JBoss Fundamentals with JBuilder
- Please fill out the speaker evaluation
- You can contact me further at kensipe_at_codementor
.net