Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Avec JavaEE, 
fais ce que tu veux… 
Alexis Hassler
Alexis Hassler 
• Développeur, formateur Java 
• Indépendant 
• Co-leader du 
• @AlexisHassler
Java EE 1° partie 
http://www.public-domain-image.com/
Java EE 
CDI 
JSF 
EJB 3 
JPA 
JAX-RS 
WebSocket
CDI 
JCA
MQTT 
Broker 
Consumer 
Consumer 
Consumer 
Producer 
Producer 
Producer 
Topic 
Topic 
Topic 
Topic 
Publish Subscribe
MQTT 
http://www.galuzzi.it/
MQTT 
http://commons.wikimedia.org/wiki/File:St_Jude_Medical_pacemaker_with_ruler.jpg
MQTT
JCA2° partie 
http://www.public-domain-image.com/
Java 
Connector 
Architecture
Connector Java EE 
Java EE 
App EIS
Outbound
Java EE 
Connector 
App EIS
Java EE 
App 
DataSource
Resource 
public class SomeNiceBean { 
@Resource(name="jdbc/SomeDS") 
DataSource dataSource; 
public void doTheJob() { 
Co...
MQTT Connection Factory 
public class SomeNiceBean { 
@Resource(name="mqtt/QuestionCF") 
ConnectionFactory connectionFacto...
Resource Adapter 
Connection ManagedConnection 
API Implementation 
ManagedConnection 
Factory 
ConnectionFactory
ManagedConnection 
Factory 
ConnectionFactory 
new 
ManagedConnection 
new 
ConnectionManager 
Connection 
ResourceAdapter...
Inbound
Java EE 
App 
Connector 
EIS
Message Driven Bean 
Java EE 
MDB 
JMS 
Destination 
Connector
JMS Message Driven Bean 
@MessageDriven(activationConfig = { 
@ActivationConfigProperty(propertyName="destinationLookup", ...
MQTT Message Driven Bean 
@MessageDriven( 
activationConfig = { 
@ActivationConfigProperty(propertyName = "topicName", 
pr...
ActivationSpec 
API Implementation 
Message 
Listener ResourceAdapter 
BootstrapContext 
WorkerManager 
XAResource
Connector API 
public interface MqttListener { 
void onMessage(Message message); 
} 
public class Message { 
private byte[...
Message Driven Bean ++ 
@MessageDriven 
public class MyMqttBean implements MqttListener { 
@TopicName("swt/Question") 
pub...
Connector 
mqtt-ra.rar 
Application 
(w. MDB) 
mqtt-ra-example.war 
WildFly 
MQTT 
Broker 
(mosquitto) 
MQTT Client 
mosqu...
C D I 3° partie 
http://www.picturesdepot.com/
Context & Dependency Injection 
• IoC pour Java EE et Java SE 
• Gestion des contextes 
• Liant pour la plupart des spec J...
http://commons.wikimedia.org/ 
Bean
@Inject
Dependency Injection 
public class MessageService { 
@Override 
public String message() { 
return "Bonjour le monde !"; 
}...
Dependency Injection 
public class HelloBean { 
@Inject MessageService service; 
public void displayHello() { 
display( fr...
Highlander Rule 
there can be 
only one 
http://pixabay.com/
Qualifiers 
public class FrenchMessageService 
implements MessageService { 
public String message() { 
return "Bonjour le ...
Qualifiers 
@Qualifier 
@Retention(RUNTIME) 
@Target({FIELD, TYPE, METHOD, PARAMETER}) 
public @interface French {} 
@Qual...
Qualifiers 
@French 
public class FrenchMessageService 
implements MessageService { 
public String message() { 
return "Bo...
Qualifiers 
public class HelloBean { 
@Inject @French MessageService service; 
public void displayHello() { 
display(servi...
Programmatic 
Lookup 
http://pixabay.com/
Programmatic lookup 
public class HelloBean { 
@Inject Instance<MessageService> service; 
public void displayHello() { 
if...
Programmatic lookup 
public class HelloBean { 
@Inject @Any 
Instance<MessageService> services; 
public void displayHello(...
Contexts 
@Dependent 
@RequestScoped 
@ConversationScoped 
@SessionScoped 
@ApplicationScoped 
public class MessageService...
Decorators 
@Decorator 
@Priority(Interceptor.Priority.APPLICATION) 
public class MessageDecorator 
implements MessageServ...
Interceptors 
http://commons.wikimedia.org/
Interceptors 
@InterceptorBinding 
@Target({METHOD, TYPE}) 
@Retention(RUNTIME) 
public @interface Loggable {}
@Interceptor @ILnoggtabeler ceptors 
public class LogInterceptor { 
@AroundInvoke 
public Object log(InvocationContext ic)...
Interceptors 
@Loggable 
public class HelloBean { 
@Inject MessageService service; 
public void displayHello() { 
display(...
Producers 
http://commons.wikimedia.org/
Producers 
public class FacesProducer { 
@Produces @RequestScoped 
public FacesContext produceFacesContext() { 
return Fac...
Producers 
@Produces 
public Logger produceLogger( 
InjectionPoint injectionPoint) { 
String loggerName = injectionPoint 
...
public class HelloBean { 
@Inject MessageService service; 
@Inject Event<Message> messageEvent; 
public void displayHello(...
Events 
public class MessageReceiver { 
public void receive(@Observes Message message) { 
System.out.println("Received: " ...
Synthèse 
Decoration Interception Event JavaEE 
Injection Context 
Integration 
Portable Extension
CDI 
Portable Extensions
Bean Manager 
https://www.kingbean.com/
Bean Manager 
• Interagir avec le conteneur CDI 
• Lecture seule 
• Injectable 
• JNDI 
• java:comp/BeanManager 
public cl...
Extension 
• Manipuler les métadonnées du container 
• AnnotatedType 
• InjectionPoint / InjectionTarget 
• BeanAttributes...
Comment ? 
En observant les 
événements 
déclenchés par le 
conteneur CDI
Exemple (-) 
• Exclure les entités JPA 
public class VetoEntityExtension 
implements Extension { 
public void vetoEntity( ...
Exemple (+) 
• Ajouter un bean 
public class HashMapAsBeanExtension 
implements Extension { 
public void addHashMapAsAnnot...
CDI 1.1 Lifecycle 
Before Bean 
Discovery 
Process Bean 
Scan 
Archive 
Process 
Annotated 
Type 
After 
Deployment 
Valid...
Application 
mqtt-cdi-example.war 
WildFly 
MQTT 
Broker 
(mosquitto) 
MQTT Client 
mosquitto 
pub/sub 
http://pixabay.com...
4° partie 
JCA // CDI
CDI 
JCA
Managed Connection Factory 
@ConnectionDefinition 
(connectionFactory = MqttConnectionFactoryImpl.class, 
connectionFactor...
JCA 
CDI
public class SomeNiceBean { 
@Inject 
MqttConnectionFactory connectionFactory; 
public void doTheJob() { 
javax.enterprise...
Java EE 8 
• Implicit Producers ? 
• Ambiguities ? 
• Qualifiers ?
JCA // CDI
JCA 
ManagedConnection 
Factory 
ConnectionFactory 
new 
ManagedConnection 
new 
ConnectionManager 
Connection 
ResourceAd...
CDI 
UserTransaction 
ManagedExecutor 
Service 
ConnectionFactory 
Producer 
ConnectionFactory 
new 
Connection 
LocalTran...
Outbound Connector 
JCA < CDI
Message Driven Bean 
@MqttDriven 
public class MyMqttBean { 
@TopicName("swt/Question") 
public void onQuestion(Message me...
Message Observer 
@ApplicationScoped 
public class MyMqttBean { 
public void onQuestion( 
@Observes @TopicName(value = "sw...
Threads 
https://www.flickr.com/photos/mckaysavage/6491930649/
Inbound Connector 
JCA > CDI
Java EE Application Server 
App 
CDI 
App 
CDI 
App 
CDI 
JCA
? 
http://pixabay.com/fr/point-d-interrogation-boule-demande-65833/
Exemples 
• https://github.com/antoinesd/cdi-demo 
• https://github.com/sewatech/mqtt-ra/ 
• https://github.com/hasalex/mq...
Version longue 
• Part 1 : Intro + CDI 
• https://parleys.com/play/536749d1e4b04bb59f502706 
• Part 2 : JCA + Synthèse 
• ...
Prochain SlideShare
Chargement dans…5
×

softshake 2014 - Java EE

1 515 vues

Publié le

Le temps est révolu où Java EE ne serait qu’à développer des applications de mise à jour de données, avec JSF / EJB / JPA. Aujourd’hui Java EE s’est assoupli et s’est ouvert sur le monde, avec CDI comme clé de voûte et a repoussé nos limites grâce à des capacités d’extension puissantes et faciles d’utilisation comme JCA.

Dans un premier temps, nous reviendrons rapidement sur la place de CDI dans JavaEE 7 et sur ses mécanismes d’extension. Dans un deuxième temps, nous verrons les techniques de connecteurs JCA et comment ils peuvent aussi constituer une possibilité d’ouverture simple à mettre en œuvre. JCA fournit des techniques pour gérer des connexions sortantes ou entrantes, sur des formats ou protocoles variés.

Publié dans : Logiciels
  • Soyez le premier à commenter

softshake 2014 - Java EE

  1. 1. Avec JavaEE, fais ce que tu veux… Alexis Hassler
  2. 2. Alexis Hassler • Développeur, formateur Java • Indépendant • Co-leader du • @AlexisHassler
  3. 3. Java EE 1° partie http://www.public-domain-image.com/
  4. 4. Java EE CDI JSF EJB 3 JPA JAX-RS WebSocket
  5. 5. CDI JCA
  6. 6. MQTT Broker Consumer Consumer Consumer Producer Producer Producer Topic Topic Topic Topic Publish Subscribe
  7. 7. MQTT http://www.galuzzi.it/
  8. 8. MQTT http://commons.wikimedia.org/wiki/File:St_Jude_Medical_pacemaker_with_ruler.jpg
  9. 9. MQTT
  10. 10. JCA2° partie http://www.public-domain-image.com/
  11. 11. Java Connector Architecture
  12. 12. Connector Java EE Java EE App EIS
  13. 13. Outbound
  14. 14. Java EE Connector App EIS
  15. 15. Java EE App DataSource
  16. 16. Resource public class SomeNiceBean { @Resource(name="jdbc/SomeDS") DataSource dataSource; public void doTheJob() { Connection connection = dataSource.getConnection(); ... } }
  17. 17. MQTT Connection Factory public class SomeNiceBean { @Resource(name="mqtt/QuestionCF") ConnectionFactory connectionFactory; public void doTheJob() { connectionFactory.getConnection() .publish(message); } }
  18. 18. Resource Adapter Connection ManagedConnection API Implementation ManagedConnection Factory ConnectionFactory
  19. 19. ManagedConnection Factory ConnectionFactory new ManagedConnection new ConnectionManager Connection ResourceAdapter new ConnectionEvent Listener XAResource LocalTransaction ManagedConnection MetaData
  20. 20. Inbound
  21. 21. Java EE App Connector EIS
  22. 22. Message Driven Bean Java EE MDB JMS Destination Connector
  23. 23. JMS Message Driven Bean @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName="destinationLookup", propertyValue="swt/Question"), @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"), }) public class MyJmsBean implements MessageListener { @Override public void onMessage(Message message) { ... } }
  24. 24. MQTT Message Driven Bean @MessageDriven( activationConfig = { @ActivationConfigProperty(propertyName = "topicName", propertyValue = "swt/Question") } ) public class MyMqttBean implements MqttListener { @Override public void onMessage(Message message) { ... } }
  25. 25. ActivationSpec API Implementation Message Listener ResourceAdapter BootstrapContext WorkerManager XAResource
  26. 26. Connector API public interface MqttListener { void onMessage(Message message); } public class Message { private byte[] payload; public Message(byte[] payload) { this.payload = payload; } // + Getter }
  27. 27. Message Driven Bean ++ @MessageDriven public class MyMqttBean implements MqttListener { @TopicName("swt/Question") public void onQuestion(Message message) { ... } @TopicName("swt/Answer") public void onAnswer(Message message) { ... } }
  28. 28. Connector mqtt-ra.rar Application (w. MDB) mqtt-ra-example.war WildFly MQTT Broker (mosquitto) MQTT Client mosquitto pub/sub
  29. 29. C D I 3° partie http://www.picturesdepot.com/
  30. 30. Context & Dependency Injection • IoC pour Java EE et Java SE • Gestion des contextes • Liant pour la plupart des spec Java EE • Moteur d’extensions pour Java EE
  31. 31. http://commons.wikimedia.org/ Bean
  32. 32. @Inject
  33. 33. Dependency Injection public class MessageService { @Override public String message() { return "Bonjour le monde !"; } }
  34. 34. Dependency Injection public class HelloBean { @Inject MessageService service; public void displayHello() { display( frService.message() ); } }
  35. 35. Highlander Rule there can be only one http://pixabay.com/
  36. 36. Qualifiers public class FrenchMessageService implements MessageService { public String message() { return "Bonjour le monde !"; } } public class EnglishMessageService implements MessageService { public String message() { return "Hello World!"; } }
  37. 37. Qualifiers @Qualifier @Retention(RUNTIME) @Target({FIELD, TYPE, METHOD, PARAMETER}) public @interface French {} @Qualifier @Retention(RUNTIME) @Target({FIELD, TYPE, METHOD, PARAMETER}) public @interface English {}
  38. 38. Qualifiers @French public class FrenchMessageService implements MessageService { public String message() { return "Bonjour le monde !"; } } @English public class EnglishMessageService implements MessageService { public String message() { return "Hello World!"; } }
  39. 39. Qualifiers public class HelloBean { @Inject @French MessageService service; public void displayHello() { display(service.message()); } }
  40. 40. Programmatic Lookup http://pixabay.com/
  41. 41. Programmatic lookup public class HelloBean { @Inject Instance<MessageService> service; public void displayHello() { if (! service.isUnsatisfied()) { display(service.get().message()); } } }
  42. 42. Programmatic lookup public class HelloBean { @Inject @Any Instance<MessageService> services; public void displayHello() { for (MessageService service : services) { display(service.message()); } } }
  43. 43. Contexts @Dependent @RequestScoped @ConversationScoped @SessionScoped @ApplicationScoped public class MessageService { ... }
  44. 44. Decorators @Decorator @Priority(Interceptor.Priority.APPLICATION) public class MessageDecorator implements MessageService { @Inject @Delegate @Any MessageService service; public String message() { return service.message() + " -decorated"; } } http://pixabay.com/
  45. 45. Interceptors http://commons.wikimedia.org/
  46. 46. Interceptors @InterceptorBinding @Target({METHOD, TYPE}) @Retention(RUNTIME) public @interface Loggable {}
  47. 47. @Interceptor @ILnoggtabeler ceptors public class LogInterceptor { @AroundInvoke public Object log(InvocationContext ic) throws Exception { System.out.println("Entering " + ic.getMethod().getName()); try { return ic.proceed(); } finally { System.out.println("Exiting " + ic.getMethod().getName()); } } }
  48. 48. Interceptors @Loggable public class HelloBean { @Inject MessageService service; public void displayHello() { display(service.message()); } }
  49. 49. Producers http://commons.wikimedia.org/
  50. 50. Producers public class FacesProducer { @Produces @RequestScoped public FacesContext produceFacesContext() { return FacesContext.getCurrentInstance(); } }
  51. 51. Producers @Produces public Logger produceLogger( InjectionPoint injectionPoint) { String loggerName = injectionPoint .getMember() .getDeclaringClass() .getName(); return Logger.getLogger(loggerName); }
  52. 52. public class HelloBean { @Inject MessageService service; @Inject Event<Message> messageEvent; public void displayHello() { display(service.message()); messageEvent.fire( new Message(frService.message())); } } Events
  53. 53. Events public class MessageReceiver { public void receive(@Observes Message message) { System.out.println("Received: " + message.text); } }
  54. 54. Synthèse Decoration Interception Event JavaEE Injection Context Integration Portable Extension
  55. 55. CDI Portable Extensions
  56. 56. Bean Manager https://www.kingbean.com/
  57. 57. Bean Manager • Interagir avec le conteneur CDI • Lecture seule • Injectable • JNDI • java:comp/BeanManager public class HelloBean { @Inject BeanManager beanManager; ... }
  58. 58. Extension • Manipuler les métadonnées du container • AnnotatedType • InjectionPoint / InjectionTarget • BeanAttributes / Beans • Producer, Observer • Pendant le démarrage
  59. 59. Comment ? En observant les événements déclenchés par le conteneur CDI
  60. 60. Exemple (-) • Exclure les entités JPA public class VetoEntityExtension implements Extension { public void vetoEntity( @Observes @WithAnnotations({Entity.class}) ProcessAnnotatedType<?> pat) { pat.veto(); } }
  61. 61. Exemple (+) • Ajouter un bean public class HashMapAsBeanExtension implements Extension { public void addHashMapAsAnnotatedType( @Observes BeforeBeanDiscovery bbd, BeanManager beanManager) { bbd.addAnnotatedType( beanManager.createAnnotatedType(HashMap.class) ); } }
  62. 62. CDI 1.1 Lifecycle Before Bean Discovery Process Bean Scan Archive Process Annotated Type After Deployment Validation Application Running Before Shutdown Undeploy Application After Bean Discovery Process Producer Process Injection Target Process Observer Method Process Injection Point Process Bean Attributes After Type Discovery événement unique événements multiples étape interne Deployment starts Bean eligibility check
  63. 63. Application mqtt-cdi-example.war WildFly MQTT Broker (mosquitto) MQTT Client mosquitto pub/sub http://pixabay.com/
  64. 64. 4° partie JCA // CDI
  65. 65. CDI JCA
  66. 66. Managed Connection Factory @ConnectionDefinition (connectionFactory = MqttConnectionFactoryImpl.class, connectionFactoryImpl = MqttConnectionFactoryImpl.class, connection = BlockingConnection.class, connectionImpl = BlockingConnection.class) public class MqttManagedConnectionFactory implements ManagedConnectionFactory, ResourceAdapterAssociation, Serializable { @Inject ResourceAdapter ra; }
  67. 67. JCA CDI
  68. 68. public class SomeNiceBean { @Inject MqttConnectionFactory connectionFactory; public void doTheJob() { javax.enterprise.inject. Connection connection = connectionFactory.getConnection(); ... } UnsatisfiedResolutionException }
  69. 69. Java EE 8 • Implicit Producers ? • Ambiguities ? • Qualifiers ?
  70. 70. JCA // CDI
  71. 71. JCA ManagedConnection Factory ConnectionFactory new ManagedConnection new ConnectionManager Connection ResourceAdapter new ConnectionEvent Listener XAResource LocalTransaction ManagedConnection MetaData WorkerManager
  72. 72. CDI UserTransaction ManagedExecutor Service ConnectionFactory Producer ConnectionFactory new Connection LocalTransaction
  73. 73. Outbound Connector JCA < CDI
  74. 74. Message Driven Bean @MqttDriven public class MyMqttBean { @TopicName("swt/Question") public void onQuestion(Message message) { ... } @TopicName("swt/Answer") public void onAnswer(Message message) { ... } }
  75. 75. Message Observer @ApplicationScoped public class MyMqttBean { public void onQuestion( @Observes @TopicName(value = "swt/Question") Message message) { ... } public void onAnswer( @Observes @TopicName("swt/QuestionBis") Message message) { ... } }
  76. 76. Threads https://www.flickr.com/photos/mckaysavage/6491930649/
  77. 77. Inbound Connector JCA > CDI
  78. 78. Java EE Application Server App CDI App CDI App CDI JCA
  79. 79. ? http://pixabay.com/fr/point-d-interrogation-boule-demande-65833/
  80. 80. Exemples • https://github.com/antoinesd/cdi-demo • https://github.com/sewatech/mqtt-ra/ • https://github.com/hasalex/mqtt-cdi • http://fr.slideshare.net/sewatech/
  81. 81. Version longue • Part 1 : Intro + CDI • https://parleys.com/play/536749d1e4b04bb59f502706 • Part 2 : JCA + Synthèse • https://parleys.com/play/5369f1f9e4b04bb59f502725

×