jboss fundamentals with jbuilder: session #3110 ken sipe code mentor, inc. nov. 5, 2005

Post on 13-Dec-2015

218 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

JBoss Fundamentals with JBuilder: Session #3110Ken 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

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

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

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

6 October 16, 2005

Configuration of JBuilder

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

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.

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

DEMO – Stateless Session

10 October 16, 2005

Tricks and Tips

Multi-Server List deployments Disable services

11 October 16, 2005

Multi-Server

Configure JBoss for EJBs Configure Tomcat for WAR.

DEMO

12 October 16, 2005

Agenda

JBoss Introduction JBuilder JBoss Server Configuration JBoss Services EJB 3 Introduction

Annotations EJB Annotation Persistence Model

13 October 16, 2005

SAR – Service ARchive

JAR with a SAR extension JBoss specific JMX components

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

15 October 16, 2005

Agenda

JBoss Introduction JBuilder JBoss Server Configuration JBoss Services EJB 3 Introduction

Annotations EJB Annotation Persistence Model

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?

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

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

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.

20 October 16, 2005

Agenda

EJB 3 Introduction Annotations EJB Annotation Persistence Model

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(){ ... }

22 October 16, 2005

Annotation Types

Marker Annotations @MarkerAnnotation

Single-value Annotations @SingleValueAnnotation(“value”)

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

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

24 October 16, 2005

Agenda

EJB 3 Introduction Annotations EJB Annotation Persistence Model

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

26 October 16, 2005

EJB 3 Annotations

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

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

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;

}

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;

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;

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

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

32 October 16, 2005

Agenda

EJB 3 Introduction Annotations EJB Annotation Persistence Model

33 October 16, 2005

Persistence Framework

Packaging Annotations / Mapping API – EntityManager

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

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>

36 October 16, 2005

With or Without You

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

37 October 16, 2005

Persistence Framework

Packaging Annotations / Mapping API – EntityManager

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

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

40 October 16, 2005

Entity Annotations

@PersistenceContext Provides EntityManager

@PersistenceUnit Provides EntityManagerFactory

@EntityTransaction

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

}

42 October 16, 2005

Life-Cycle Example Complete

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

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

43 October 16, 2005

Persistence Framework

Packaging Annotations / Mapping API – EntityManager

44 October 16, 2005

EJB3 Persistence Interfaces

EntityManager Interface to interact with persistence context.

EntityManagerFactory Creates an EntityManager

Query Queries made through this interface

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;

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

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

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!

49 October 16, 2005

Agenda

EJB 3 Introduction Annotations EJB Annotation Persistence Model

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

51 October 16, 2005

Questions

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 …kensipe@codementor.net

Thank YouThank YouThank You

top related