No Tier Enterprise Applications with CDI

68
No-Tier Enterprise Applications with CDI Lars Röwekamp | open knowledge GmbH @mobileLarson @_openknowledge

Transcript of No Tier Enterprise Applications with CDI

Page 1: No Tier Enterprise Applications with CDI

No-Tier EnterpriseApplications with CDI

Lars Röwekamp | open knowledge

GmbH @mobileLarson

@_openknowledge

Page 2: No Tier Enterprise Applications with CDI

> Cool! JavaEE bietet IoC/DI > Cool! JavaEE bietet IoC/DI > Cool! JavaEE bietet IoC/DI > Cool! JavaEE bietet IoC/DI

Page 3: No Tier Enterprise Applications with CDI

> Aber was ändert > Aber was ändert sich?sich?

Page 4: No Tier Enterprise Applications with CDI

> SCHICHT 3

> SCHICHT 2

> SCHICHT 1TX Grenze

Page 5: No Tier Enterprise Applications with CDI

> UseCase: ausgewählten Kunden bearbeiten

Einmal den Customer Controller, bitte ;-)

Und für mich den Customer Service, bitte!

Ok, dann brauch ich nen Customer Entity mit der internene ID 147122, aber flott!

Page 6: No Tier Enterprise Applications with CDI

AA KK TT UU EE LL LL

AA UU SS GG EE WW ÄÄ HH LL TT EE

KK UU NN DD EE

RR

> Was wäre > Was wäre wenn ...? wenn ...? > Was wäre > Was wäre wenn ...? wenn ...?

Page 7: No Tier Enterprise Applications with CDI

LL II SS TT EE

MM EE II NN EE RR

EE II NN KK ÄÄ UU

> Oder wenn ...? > Oder wenn ...? > Oder wenn ...? > Oder wenn ...?

FF EE

Page 8: No Tier Enterprise Applications with CDI

> „No-Tier“

TX Grenze

Page 9: No Tier Enterprise Applications with CDI

J2EE 1.3 (JSR 58):

September 2001 Servlet 2.3 JSP 1.2 EJB 2.0

J2EE 1.4 (JSR 151):

November 2003 Servlet 2.4 JSP 2.0 EJB 2.1

01 / 02 03 / 04 05 / 06 07 / 08 09 / 10

Spring V1

Juni 2003

„Spring“

Oktober 2002

Java EE v5 (JSR 244):

Mai 2006 Servlet 2.5 JSP 2.1 / JSF 1.2 EJB 3.0

Java EE v6 (JSR 316):

Dezember 2009 Servlet 3.0 JSP 2.1 MR / JSF 2.0 EJB 3.1 JPA 2.0 CDI 1.0

Spring V3

Dezember 2009

Spring V2

Oktober 2006

Page 10: No Tier Enterprise Applications with CDI

CDI Kickstart

> DI/IoC lite „Java EE without EJB“

> DI/IoC advanced LifeCyle Management und Scoping

> DI/IoC eXtreme Typensicherheit und lose Koppelung

> DI/IoC openExtension-Mechanismus

Page 11: No Tier Enterprise Applications with CDI

Teil 1: Base Features

Page 12: No Tier Enterprise Applications with CDI

Teil 1: Base Features

> @Inject> @Named> Sterotypes> Alternatives

Page 13: No Tier Enterprise Applications with CDI

Teil 1: Base Features

> Sope Singleton> Cleanup> Request> Conversation> Session> Application> Dependant*> Singleton**Pseudo-Scope

Page 14: No Tier Enterprise Applications with CDI

Teil 1: Base Features

Page 15: No Tier Enterprise Applications with CDI

Teil 1: Base Features

Page 16: No Tier Enterprise Applications with CDI

Teil 1: Base Features

> Events> Observer> Interceptors

Page 17: No Tier Enterprise Applications with CDI

Teil 1: Base Features

> Qualifier> Stereotypes> Alternatives

Page 18: No Tier Enterprise Applications with CDI

> Was haben wir > Was haben wir gewonnen?gewonnen?> Was haben wir > Was haben wir gewonnen?gewonnen?

Page 19: No Tier Enterprise Applications with CDI

> SCHICHT 3

> SCHICHT 2

> SCHICHT 1

Page 20: No Tier Enterprise Applications with CDI

> SCHICHT 3

> SCHICHT 2

> SCHICHT 1

Page 21: No Tier Enterprise Applications with CDI

import javax.inject.Named;import javax.inject.Inject;import javax.enterprise.context.RequestScoped;

@Named(“hello“)@RequestScopedpublic class HelloBean {

@Inject Greeter greeter;

private String name;

public String getGreeting() {return greeter.greet(name);

}

// getter and setter for name attribute...}

CDI Managed Bean

„hello“ für EL„hello“ für EL

gültig für Requestgültig für Request

Injection PointInjection Point

Page 22: No Tier Enterprise Applications with CDI

Cool, aber geht da

nicht mehr?

CDI Mehrwert

Page 23: No Tier Enterprise Applications with CDI

FACHLICHE Injektion statt „nur“

Infrastruture Injection

Page 24: No Tier Enterprise Applications with CDI

eCustomer - Beispiel

Page 25: No Tier Enterprise Applications with CDI
Page 26: No Tier Enterprise Applications with CDI

CDI Refactoring

> CDI Wiring

> CDI Current User

> CDI Beans only

> CDI Style Views

> CDI Events

Page 27: No Tier Enterprise Applications with CDI

CDI Refactoring

> CDI Wiring

> CDI Current User

> CDI Beans only

> CDI Style Views

> CDI Events

Page 28: No Tier Enterprise Applications with CDI

CDI Refactoring

> CDI Wiring

> CDI Current User

> CDI Beans only

> CDI Style Views

> CDI Events

Page 29: No Tier Enterprise Applications with CDI

eCustomer - CDI Current User

> Producer Methods & Fields

> „Factory Method“ Pattern für Objekte

> @Produces als Mittel zum Zweck

> @Qualifier als Mittel zur Qualifizierung

> Ermöglicht nicht nur Infrastruktur

sondern vor allem auch fachliche

Injection.

Page 30: No Tier Enterprise Applications with CDI

eCustomer - CDI Current User

@Named@SessionScopedpublic class AuthenticationController implements Serializable {

private User authenticatedUser; public String authenticate() {...}

public User getAuthenticatedUser() {...}

...}

AuthenticationControllerAuthenticationController

@Produces @Current

Page 31: No Tier Enterprise Applications with CDI

eCustomer - CDI Current User

@Named@SessionScopedpublic class AuthenticationController implements Serializable {

private User authenticatedUser; public String authenticate() {...}

@Produces @Current public User getAuthenticatedUser() {...}

...} @Inject @Current User user; @Inject @Current User user;

@RequestScoped

Page 32: No Tier Enterprise Applications with CDI

eCustomer - CDI Current User

package de.openknowledge.qualifier;

import ...

@Qualifier@Target({TYPE, METHOD, PARAMETER, FIELD})@Retention(RUNTIME)public @interface Current{}

Self-made QualifierSelf-made Qualifier

Page 33: No Tier Enterprise Applications with CDI

Use Case „Kunde bearbeiten“

@Statelesspublic class CustomerServiceEJB

implements CustomerService {

@Inject @Current private User currentUser;

@PersistenceContext private EntityManager em;

// updates the customer - needs a transaction public Customer updateCustomer(Customer customer) { customer.updateAuditInformation(currentUser); return em.merge(customer); }}

CustomerService EJBCustomerService EJB

Page 34: No Tier Enterprise Applications with CDI

CDI Refactoring

> CDI Wiring

> CDI Current User

> CDI Beans only

> CDI Style Views

> CDI Events

Page 35: No Tier Enterprise Applications with CDI

eCustomer - CDI Beans only

> Injizierbare CDI Ressourcen

> „normale“ Java Klassen, optional mit

@Named oder @Qualifier markiert

> EJBs, wie Stateless, Stateful, Singleton

> Sonstige Java EE Ressourcen, wie

PersistenceContext, UserTransaction

Page 36: No Tier Enterprise Applications with CDI

eCustomer - CDI Beans only

@Stateless// no annotation required!public class CustomerServiceBeanEJB

implements CustomerService {

@Inject @Current private User currentUser;

@PersistenceContext private EntityManager em;

// updates the customer - needs a transaction public Customer updateCustomer(Customer customer) { customer.updateAuditInformation(currentUser); return em.merge(customer); }

...

}

CustomerService CDICustomerService CDI

Page 37: No Tier Enterprise Applications with CDI

eCustomer - CDI Beans only

@Stateless// no annotation required!public class CustomerServiceBeanEJB

implements CustomerService {

@Inject @Current private User currentUser;

@PersistenceContext private EntityManager em;

// updates the customer - needs a transaction public Customer updateCustomer(Customer customer) { customer.updateAuditInformation(currentUser); return em.merge(customer); }

...

}

CustomerService CDICustomerService CDI

Und Transaktionen?!Und Transaktionen?!

Bauen wir uns selbst ...Bauen wir uns selbst ...

Page 38: No Tier Enterprise Applications with CDI

eCustomer - CDI Beans only

// simple pojo // no annotation required!public class CustomerServicBean

implements CustomerService {

@Inject @Current private User currentUser;

@PersistenceContext private EntityManager em;

@Transactional public Customer updateCustomer(Customer customer) { customer.updateAuditInformation(currentUser); return em.merge(customer); }

...

}

CustomerService CDICustomerService CDI

Page 39: No Tier Enterprise Applications with CDI

CDI Interceptor

s

Page 40: No Tier Enterprise Applications with CDI

eCustomer - CDI Beans only

@InterceptorBinding@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD, ElementType.TYPE})

public @interface Transactional { @Nonbinding public TransactionalType value()

default TransactionalType.REQUIRED; }

Transactional AnnotationTransactional Annotation

??

Page 41: No Tier Enterprise Applications with CDI

eCustomer - CDI Beans only

@Transactional @Interceptorpublic class TransactionAdvice {

@Inject private UserTransaction utx;

@AroundInvoke public Object applyTransaction( InvocationContext ic) throws Throwable {

... // 1. access @Transactional, // if needed ... // 2. implement utx.begin() ic.proceed(); // 3. call original method ... // 4. implement utx.commit()

} }

*XML registration omitted

Transactional Interceptor*Transactional Interceptor*

Page 42: No Tier Enterprise Applications with CDI

eCustomer - CDI Beans only

@Transactional @Interceptorpublic class TransactionAdvice {

@Inject private UserTransaction utx;

@AroundInvoke public Object applyTransaction( InvocationContext ic) throws Throwable {

Transactional tx = ic.getMethod(). getAnnotation(Transactional.class); ... // 2. implement utx.begin() ic.proceed(); // 3. call original method ... // 4. implement utx.commit()

} }

Transactional Interceptor*Transactional Interceptor*

*XML registration omitted

Page 43: No Tier Enterprise Applications with CDI

eCustomer - CDI Beans only

// no annotation required!public class CustomerServiceBean

implements CustomerService {

@Inject @Current private User currentUser;

@PersistenceContext private EntityManager em;

@de.openknowledge.qualifier.Transactional public Customer updateCustomer(Customer customer) { customer.updateAuditInformation(currentUser); return em.merge(customer); } ...}

CustomerService CDICustomerService CDI

inkl. Transaktioninkl. Transaktion

Page 44: No Tier Enterprise Applications with CDI

> SCHICHT 3

> SCHICHT 2

> SCHICHT 1TX Grenze

Page 45: No Tier Enterprise Applications with CDI

> SCHICHT 3

> SCHICHT 2

> SCHICHT 1

TX Grenze

Page 46: No Tier Enterprise Applications with CDI

eCustomer - CDI Beans only

@Named@SessionScopedpublic class CustomerBean implements Serializable {

@Inject private CustomerService customerService;

@de.openknowledge.qualifier.Transactional public String update() { customerService.updateCustomer(currentCustomer); ... // some additional use case “tx“ related work return Outcome.SUCCESS; } ...}

CustomerBean CDICustomerBean CDI

inkl. „fachliche“ Transaktioninkl. „fachliche“ Transaktion

Page 47: No Tier Enterprise Applications with CDI

CDI Refactoring

> CDI Wiring

> CDI Current User

> CDI Beans only

> CDI Style Views

> CDI Events

Page 48: No Tier Enterprise Applications with CDI

eCustomer - CDI Style Views

> CDI Conversations

> @ConversationScoped

> conversation.begin() / .end()

> CDI „Backing Beans“

> @Produces in Kombination mit @Named

> „self-made“ Qualifier

Page 49: No Tier Enterprise Applications with CDI

eCustomer - CDI Style Views

@javax.inject.Named@javax.context.enterpise.ConversationScopedpublic class CustomerBean implements Serializable {

private Customer customer;

@javax.inject.Inject private Conversation conversation;

public String selectCustomer(Customer customer) { conversation.begin(); this.customer = customer; return ... ; } ... }

CustomerBean (1/2)CustomerBean (1/2)

Page 50: No Tier Enterprise Applications with CDI

eCustomer - CDI Style Views

@javax.inject.Named@javax.context.enterpise.ConversationScopedpublic class CustomerBean implements Serializable {

private Customer customer;

@javax.inject.Inject private Conversation conversation;

public String updateCustomer() { ... conversation.end(); return ... ; } ... }

CustomerBean (2/2)CustomerBean (2/2)

Conversation endet mit RequestConversation endet mit Request

Page 51: No Tier Enterprise Applications with CDI

eCustomer - CDI Style Views

<html ...> <h:body> <h:form>

Vorname: <h:inputText value=“#{customerBean.customer.firstname}"/> Name: <h:inputText value=“#{customerBean.customer.lastname}"/>

</h:form> </h:body></html>

Infrastructure in der View!Infrastructure in der View!

Page 52: No Tier Enterprise Applications with CDI

eCustomer - CDI Style Views

<html ...> <h:body> <h:form>

Vorname: <h:inputText value=“#{selectedCustomer.firstname}"/> Name: <h:inputText value=“#{selectedCustomer.lastname}"/>

</h:form> </h:body></html>

Fachlichkeit in der View!Fachlichkeit in der View!

Page 53: No Tier Enterprise Applications with CDI

eCustomer - CDI Style Views

@javax.inject.Named@javax.context.enterpise.ConversationScopedpublic class CustomerBean implements Serializable {

private Customer customer;

@javax.inject.Produces @javax.inject.Named(“selectedCustomer“) @de.openknowledge.qualifier.Selected public Customer getSelected { return customer; } ... }

CustomerBean CustomerBean

Page 54: No Tier Enterprise Applications with CDI

CDI Refactoring

> CDI Wiring

> CDI Current User

> CDI Beans only

> CDI Style Views

> CDI Events

Page 55: No Tier Enterprise Applications with CDI

eCustomer - CDI Events

> Java EE Observer Pattern, inkl. ...

> Event Object & Event Producer

> Observer Method

> „besondere“ Eigenschaften

> schichtenneutral auch für POJOs

> (ggf.) transaktionsgebunden

> synchrone Interaktion

Page 56: No Tier Enterprise Applications with CDI

eCustomer - CDI Events

„User created!“„User created!“

„Und ich auch!“„Und ich auch!“„Ah, interessant.“„Ah, interessant.“

„Finde ich!“„Finde ich!“

Page 57: No Tier Enterprise Applications with CDI

eCustomer - CDI Events

> Ok, aber ...

> Wie sieht ein solches Event aus?

> Und wie fange ich es?

> Und vor allem: wie löse ich es aus?

Page 58: No Tier Enterprise Applications with CDI

eCustomer - CDI Events

public class CustomerCreatedEvent {

private Customer customer;

public CustomerCreatedEvent(Customer customer) { this.customer = customer; }

public Customer getCustomer() { ... }}

Customer Created Event (1/3)Customer Created Event (1/3)

„Wie sieht ein solches Event aus?“

Page 59: No Tier Enterprise Applications with CDI

eCustomer - CDI Events

public void sendWelcomeMail( @Observes CustomerCreatedEvent event) { Customer customer = event.getCustomer(); ...}

Customer Created Event (2/3)Customer Created Event (2/3)

„Und wie fange ich es?“

Page 60: No Tier Enterprise Applications with CDI

eCustomer - CDI Events

@Injectprivate Event<CustomerCreatedEvent> eventSource;

public String createCustomer() {

Customer customer = ...; eventSource.fire( new CustomerCreatedEvent(customer) ); ... }

Customer Created Event (3/3)Customer Created Event (3/3)

„Und vor allem: Wie löse ich es aus?“

Page 61: No Tier Enterprise Applications with CDI

eCustomer - CDI Events

public void sendWelcomeMail( @Observes CustomerCreatedEvent event @Created Customer customer) { Customer customer = event.getCustomer(); ...}

Qualified CDI Event - Alternative (2/3)Qualified CDI Event - Alternative (2/3)

„Und wie fange ich es?“

Page 62: No Tier Enterprise Applications with CDI

eCustomer - CDI Events

@Inject @Created private Event<Customer> eventSource;

public String createCustomer() {

Customer customer = ...; eventSource.fire(customer); ... }

Qualified CDI Event - Alternative (3/3)Qualified CDI Event - Alternative (3/3)

„Und vor allem: Wie löse ich es aus?“

Page 63: No Tier Enterprise Applications with CDI

eCustomer - CDI Events

„Wow! Observer mit Qulifiern - das muss ich erst einmal sacken lassen!“

„Und das ist erst die Spitze des Eisberges - schau dir

mal die CONDITIONAL OBSERVER METHODS hier

an!“

Page 64: No Tier Enterprise Applications with CDI

eCustomer - CDI Events

// Conditional Observer Method that takes // - Transaction status, e.g. AFTER_SUCCESS, AFTER_FAILURE// - Bean instance status, e.g. ALLWAYS// into accountpublic void sendWelcomeMail( @Observes( receive=ALLWAYS, // bean during=AFTER_SUCCESS // tx ) @Created Customer customer) { ... }

Conditional Observer MethodsConditional Observer Methods

Page 65: No Tier Enterprise Applications with CDI

> „No-Tier“

TX Grenze

Page 66: No Tier Enterprise Applications with CDI

XFazit:

Page 67: No Tier Enterprise Applications with CDI

CDI Fazit

CDI ermöglicht ...

“Java Enterprise Development without EJB - and without Spring“

CDI bietet dafür ...

„typesafe und schichtenneutrales Injection Framework für fachliche und technische Injection sowie ein Rahmenwerk für eventgetriebene Entwicklung.“

Page 68: No Tier Enterprise Applications with CDI

Gibt es noch Fragen?

Dann los ...

@mobileLarson@_openknowledg

efacebook.com/openknowledge