JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

52
JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005

Transcript of JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

Page 1: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

JBoss Fundamentals with JBuilder: Session #3110Ken Sipe

Code Mentor, Inc.Nov. 5, 2005

Page 2: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

2 October 16, 2005

Agenda

JBoss Introduction JBuilder JBoss Server Configuration JBoss Services EJB 3 Introduction

Annotations EJB Annotation Persistence Model

Page 3: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

3 October 16, 2005

What 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

Page 4: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

4 October 16, 2005

JBoss Installation

Version < 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

Page 5: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

5 October 16, 2005

JBoss 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

Page 6: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

6 October 16, 2005

Configuration of JBuilder

Enterprise --> Configure Servers Select Jboss 4.0.1+ Configure home directory

Page 7: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

7 October 16, 2005

Starting a Project

Copy the preferred server configuration Create JB project select this new server configuration for

the deployment. Create EJBs... etc.

Page 8: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

8 October 16, 2005

Managing JBoss

Jboss is running and ready when you see 14:59:11,304 INFO [Server] JBoss (MX MicroKernel) [4.0.3 (build:

CVSTag=JBoss_4_0_3 date=200510042341)] Started in 34s:106ms

Web administration http://localhost:8080/

Tomcat status JMX Console Jboss Web Console

Page 9: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

DEMO – Stateless Session

Page 10: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

10 October 16, 2005

Tricks and Tips

Multi-Server List deployments Disable services

Page 11: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

11 October 16, 2005

Multi-Server

Configure JBoss for EJBs Configure Tomcat for WAR.

DEMO

Page 12: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

12 October 16, 2005

Agenda

JBoss Introduction JBuilder JBoss Server Configuration JBoss Services EJB 3 Introduction

Annotations EJB Annotation Persistence Model

Page 13: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

13 October 16, 2005

SAR – Service ARchive

JAR with a SAR extension JBoss specific JMX components

Page 14: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

14 October 16, 2005

Creating SAR

Create JMX MBean Create SAR

New Object Gallery Select Enterprise --> Jboss Service Module

Assign Mbean class Assign Name

JBuilder is misleading the name must have a domain!!!

Page 15: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

15 October 16, 2005

Agenda

JBoss Introduction JBuilder JBoss Server Configuration JBoss Services EJB 3 Introduction

Annotations EJB Annotation Persistence Model

Page 16: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

16 October 16, 2005

Because 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?

Page 17: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

17 October 16, 2005

EJB 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

Page 18: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

18 October 16, 2005

EJB 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

Page 19: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

19 October 16, 2005

EJB 3 Goals

The EJB 3.0 release is focused on a simplification of the Enterprise JavaBeans architecture from the developer’s 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.

Page 20: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

20 October 16, 2005

Agenda

EJB 3 Introduction Annotations EJB Annotation Persistence Model

Page 21: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

21 October 16, 2005

MetaData is Required in EJB 3

Annotations (JSR-175) were incorporated in JDK 1.5 Annotations seem like JavaDoc but are quite different.

JavaDoc/** *@deprecated */public void doThis(){ ... }

Annotations@deprecated ???public void doThis(){ ... }

Page 22: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

22 October 16, 2005

Annotation Types

Marker Annotations @MarkerAnnotation

Single-value Annotations @SingleValueAnnotation(“value”)

Full Annotations @FullAnnotation( value1=”x”, value2=”y”)

Page 23: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

23 October 16, 2005

JDK Built-in Annotations

Override Marker to indicate the intention to override

Deprecated Marker (which is a duplication of the @deprecated javadoc)

SuppressWarnings Suppresses warnings

public class DeprecatedUser2 { @SuppressWarnings({"deprecation"}) public static void main(String[] args) { DeprecatedExample2.foo(); }}

Page 24: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

24 October 16, 2005

Agenda

EJB 3 Introduction Annotations EJB Annotation Persistence Model

Page 25: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

25 October 16, 2005

EJB 3 Annotations

@EJB @Stateless @Stateful @Remote @NamedQuery @MessageDriven @ActivationConfigProperty @TransactionManagemen

t @TransactionAttribute

@RemoteHome @Local @LocalHome @Interceptors @AroundInvoke @Inject @Resource @EJBs

Page 26: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

26 October 16, 2005

EJB 3 Annotations

@CallbackListener @PostConstruct @Timeout @ApplicationException @RolesReferenced @RolesAllowed @PermitAll @DenyAll @RunAs @SecurityRoles

@Init @PreDestroy @PrePassivate @PostConstruct @PostActivate @Remove

Page 27: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

27 October 16, 2005

Field Level Injection

Resource evaluates to JNDI name of resource java:/com/env/com.codementor.devcon/ejbDS

Alternatively it can be declared @Resource(name="jdbc/

sqltestDB",type=javax.sql.DataSource.class)

package com.codementor.devcon;import javax.ejb.*; import javax.annotation.*;@Statefulpublic class StatefulBean implements RemoteStatefulInterface{

@Resource javax.sql.DataSource ejbDS;@Resource javax.mail.Session ejbmailSession;

}

Page 28: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

28 October 16, 2005

Setter Injection

Same example on the setter of the EJB

@Resource(name="jdbc/customerDB") public void setDataSource(DataSource myDB) {

this.ds = myDB; } private DataSource myDB;

Page 29: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

29 October 16, 2005

EJB References

The following are examples of references to EJBs in JEE 5.

@EJB LocalStatelessInterface sless;

@EJB(name="ejb/stateless") LocalStatelessInterface sless;

@EJB(name="ejb/stateless") RemoteStatelessHome slessHome;

Page 30: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

30 October 16, 2005

Explicit Dependency Lookup

EJB 2.1: Initial Context ic=new InitialContext(); Object o=ic.lookup("ejb/FirstStateless"); statelessHome= (StatelessRemoteHome)PortableRemoteObject.narrow(objref, StatelessRemoteHome.class);

EJB 3.0: @Resource SessionContect sc public void setup(){ FirstStatelessInterface fi=(FirstStatelessInterface)sc.lookup("ejb/FirstStateless"); }

Page 31: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

31 October 16, 2005

Stateless Session Example@Statelesspublic class ConferenceBean implements Conference { @Resource private EntityManager em; private ConfSession session;

public ConfSession findSessionByID(int sessionID) { return ((ConfSession) em.find(ConfSession.class,

sessionID)); }

public void addSession(long sessionID, String title, Date date) {

session = new ConfSession(); session.setTitle(title); session.setDate(date); session.setId(sessionID); em.persist(session); }}

Page 32: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

32 October 16, 2005

Agenda

EJB 3 Introduction Annotations EJB Annotation Persistence Model

Page 33: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

33 October 16, 2005

Persistence Framework

Packaging Annotations / Mapping API – EntityManager

Page 34: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

34 October 16, 2005

There'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

Page 35: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

35 October 16, 2005

Persistence.xml

<entity-manager><name>em1</name><provider>com.acme.persistence</provider><jta-data-source>jdbc/MyDB</jta-data-source><mapping-file>ormap.xml</mapping-file><jar-file>MyApp.jar</jar-file><class>com.widgets.Order</class><class>com.widgets.Customer</class><properties>

<property name="sql-logging" value="on"/></properties>

</entity-manager>

Page 36: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

36 October 16, 2005

With or Without You

Any PAR without a persistence.xml will get the container defaults.

Page 37: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

37 October 16, 2005

Persistence Framework

Packaging Annotations / Mapping API – EntityManager

Page 38: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

38 October 16, 2005

EJB 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

Page 39: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

39 October 16, 2005

Entity Annotations

@Entity named used to refer to persistent class name – property defaults to name of class access – property or field access

Callbacks @PrePersist @PostPersist @PreRemove @PostRemove @PreUpdate @PostUpdate @PostLoad

Page 40: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

40 October 16, 2005

Entity Annotations

@PersistenceContext Provides EntityManager

@PersistenceUnit Provides EntityManagerFactory

@EntityTransaction

Page 41: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

41 October 16, 2005

Life-Cycle Method Example@Entity@EntityListener(com.acme.AlertMonitor.class)public class AccountBean implements Account {

Long accountId; Integer balance; boolean preferred;public Long getAccountId() { ... }public Integer getBalance() { ... }@Transient // because status depends upon non-persistent contextpublic boolean isPreferred() { ... }public void deposit(Integer amount) { ... }public Integer withdraw(Integer amount) throws NSFException {... }@PrePersistpublic void validateCreate() {

if (getBalance() < MIN_REQUIRED_BALANCE)throw new AccountException("Insufficient balance to open an account");

}@PostLoadpublic void adjustPreferredStatus() {

preferred = (getBalance() >= AccountManager.getPreferredStatusLevel());}

}

Page 42: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

42 October 16, 2005

Life-Cycle Example Complete

public class AlertMonitor {@PostPersistpublic void newAccountAlert(Account acct) {

Alerts.sendMarketingInfo(acct.getAccountId(), acct.getBalance());}

Page 43: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

43 October 16, 2005

Persistence Framework

Packaging Annotations / Mapping API – EntityManager

Page 44: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

44 October 16, 2005

EJB3 Persistence Interfaces

EntityManager Interface to interact with persistence context.

EntityManagerFactory Creates an EntityManager

Query Queries made through this interface

Page 45: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

45 October 16, 2005

EJB Bean – Getting an Entity Manager

Example 1:@PersistenceContextpublic EntityManager em;

Example 2:@PersistenceContext(unitName="order")EntityManager em;//here only one persistence unit exists@PersistenceContext(type=PersistenceContextType.EXTENDED)EntityManager orderEM;

Page 46: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

46 October 16, 2005

Persisting 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); }

Page 47: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

47 October 16, 2005

Querying the Manager

@Statelesspublic 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)

.getSingleResult(); return session; }

Page 48: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

48 October 16, 2005

EJB Sample Code

@Statelesspublic class ShoppingCartImpl implements ShoppingCart {

@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 pwhere p.name = :name").setParameter("name", name).getSingleResult();

}public LineItem createLineItem(Order order, Product product, int quantity) {

LineItem li = new LineItem(order, product, quantity);order.getLineItems().add(li);em.persist(li);return li;

}}

Notice whats missing!

Page 49: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

49 October 16, 2005

Agenda

EJB 3 Introduction Annotations EJB Annotation Persistence Model

Page 50: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

50 October 16, 2005

EAR 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

Page 51: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

51 October 16, 2005

Questions

Page 52: JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

52 October 16, 2005

Session 3110 – JBoss Fundamentals with JBuilder

Thank You

3110JBoss Fundamentals with JBuilder

Please fill out the speaker evaluation

You can contact me further at …[email protected]

Thank YouThank YouThank You