ECB: The Boundary Layer

download ECB: The Boundary Layer

If you can't read please download the document

Transcript of ECB: The Boundary Layer

  1. 1. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 1 Session: Pattern of the Boundary Layer Service Facade(s): Dual View SOA Facade Lightweight Asynchronous Facade Multichannel Facade
  2. 2. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 2 Objectives Learn about: the several types of facades
  3. 3. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 3 Some orientation Consumer Consumer Layer Integration Layer Business Process Layer Services Layer Component Layer OS Layer
  4. 4. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 4 ECB Pattern Entity Control Boundary Based upon Robustness Diagrams (http://www.agilemodeling.com/artifacts/robustnessDiagram.htm) Boundary: user interface Control: actual process or activity Entity: a concept from an enterprise context. Elements are generic enough to be mapped either to service- oriented or object-oriented architectures. Boundary Control Entity Adam Bien
  5. 5. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 5 Services, components and patterns Boundary Control Entity DAO & Domain Store Generic DAO Singleton Service Starter Dual View SOA Facade Lightweight asynchronous Facade Multichannel Facade TO & DTO Paginator Bean Locator Multichannel Facade Resource Binder Payload Extractor Aynchronous Resource Integrator Infrastructure
  6. 6. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 6 Module Service Facade
  7. 7. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 7 Service Facade What is it about: Easy to use business API, boundary between UI and backend Exposed methods should hardly / never change Coarse grained, remotely accessible Image: Bundesarchiv
  8. 8. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 8 Service facade The principle Web Container Invocation EJB Container Other Client SL
  9. 9. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 9 Service facade Benefits: Encapsulation: The client does not know too many things about the components behind Decoupling: fine-grained services are coordinated by the facade and remain independent and reusable Crosscutting: As being the single point of entry, the facade is the place to implement all cross-cutting concerns like monitoring or precondition checks Usability: The methods of a service facade should make sense to a business expert (not a bazillian of getter/setter invocations)
  10. 10. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 10 Service Facade How to implement: Usually a SLSB, in rare cases it might be a SFSB as well Remotely accessible, local interfaces should be there as well. Transaction handling might be left to the container (TransactionAttributeType.REQUIRES_NEW at class level) Implies a service-oriented way of thinking and promotes procedural programming. The methods are actually procedures, which expect parameters and return the result per value. The parameters and results can be either Transfer Objects (TOs) or detached JPA entities.
  11. 11. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 11 Big Picture App.-Server JNDI EJB Container Bean Remote Interface Service Endpoint Interface Local Interface
  12. 12. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 12 Implementation strategies There are several strategies to implement a service facade: CRUD facade (we will deal with this later using the GenericDAO) Dual View facade SOA facade (We will deal with this later using some integration patterns) Multichannel facade
  13. 13. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 13 CRUD Facade Nothing else but an exposed, transactional DAO Revisited later in the field of GenericDAO (stay tuned)
  14. 14. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 14 Dual View Facade In the vast majority of cases, the Service Facade is accessed from the same VM. Calling the remote interface: Call-By-Value semantics Calling the local interface: Call-By-Reference semantics Calling the remote/local interfaces has performance impact due to marshalling/unmarshalling of parameters in case of remote communication Remote interface is exposed to external clients, so it has to remain stable during the lifecycle. Not all methods need to be exposed remotely; some of them are only dedicated for internal use such as workfow engines, web applications, or message-driven beans.
  15. 15. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 15 How it might look like
  16. 16. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 16 Example The remote interface The local interface package de.brockhaus.userMgmt.boundary; import javax.ejb.Remote; @Remote public interface UserManagementService { public User findUserByCredentials(String user, String pwd); } package de.brockhaus.userMgmt.boundary; import javax.ejb.Local; @Local public interface UserManagementServiceLocal extends UserManagementService { public User createUser(User u); }
  17. 17. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 17 Example The Stateless Session Bean package de.brockhaus.userMgmt.boundary; @Stateless(mappedName = "ejb/facade/UserManagementService") @Local(UserManagementServiceLocal.class) @Remote(UserManagementService.class) @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public class UserManagementServiceBean implements UserManagementService, UserManagementServiceLocal { public User findUserByCredentials(String user, String pwd) { return new User("peterp", "neverland"); } public User createUser(User u) { return u; } }
  18. 18. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 18 The client For local clients it will be the injected local reference For remote clients, it will be the 'regular' JNDI lookup public static void init() { try { ctx = new InitialContext(); facade = (UserManagementService) ctx.lookup("ejb/facade/UserManagementService"); } catch (NamingException e) { e.printStackTrace(); } }
  19. 19. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 19 Best practices Facade resides in a component named after the domain-specific name, like ordermgmt, usermgmt, ... Implementation of facade to be in a package named facade or boundary Business interface named after the business concept like OrderService and OrderServiceBean
  20. 20. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 20 Lab Implement Dual View (not more than 15 min.)
  21. 21. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 21 SOA Facade Asynchronous, autonomous, independent of each other and technology agnostic Fire and forget style Usually implemented using a Message-driven Bean (MDB)
  22. 22. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 22 JMS Queue Architecture Each message has only one consumer. Receiver can fetch the message at any time. No timing dependency between senders and receivers. Receiver acknowledges after processing the message. Sender JMS Server Receiver sends consumes acknowledges Queue
  23. 23. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 23 JMS Topic Architecture Each message may have multiple consumers. Subscriber must be active to receive relevant subscription. Timing dependency between publishers and subscribers. Possibility of a durable subscription to remove timing dependency. Durable subscribers may be inactive. Publisher JMS Server Subscriber1 publishes delivers subscribes Subscriber2 delivers subscribes Topic
  24. 24. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 24 Message-Driven Beans A message-driven bean (MDB) is an EJB that listens to messages from a JMS server or any other messaging system through JCA. Features of MDBs Consumes messages from a destination (queue or topic). Not visible to clients (asynchronous). No remote or local business interfaces. Stateless, no conversational state. Managed by the container. Producer sends/publishes Server ContainerContainer Destination MDB Pool
  25. 25. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 25 The Message-driven Bean Annotations, interfaces and CDI @MessageDriven( activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty(propertyName="destination", propertyValue="queue/facade/UserMgmt") }) public class UserManagementQueueListenerBean implements MessageListener { @EJB private UserManagementServiceLocal local; ...
  26. 26. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 26 The Message-driven Bean onMessage() public void onMessage(Message msg) { log.info("invoked"); if(msg instanceof ObjectMessage) { try { Serializable s = ((ObjectMessage) msg).getObject(); if(s instanceof User) { local.createUser((User) s); } else { log.error("Wrong object type sent"); } } catch (JMSException e) { e.printStackTrace(); } } else { log.error("Wrong message type");} }
  27. 27. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 27 The client More needs to be done: ConnectionFactory and Destination (Queue/Topic) needs to be configured beforehand Get connected: public static void init() { try { InitialContext iniCtx = new InitialContext(); QueueConnectionFactory qcf = (QueueConnectionFactory) iniCtx.lookup("ConnectionFactory"); connection = qcf.createQueueConnection(); queue = (Queue) iniCtx.lookup("queue/facade/UserMgmt"); session = connection. createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); connection.start(); sender = session.createSender(queue); } ...
  28. 28. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 28 The client Send the things Better think twice, what you are sending (ObjectMessage, TextMessage, ByteMessage) @see ServiceActivator and PayloadExtractor (covered later) try { ObjectMessage msg = session. createObjectMessage(new User("peterp", "neverland")); sender.send(msg);
  29. 29. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 29 SOA Facade Further enhancements: Check message types by interceptor instead of using instanceof (PayloadExtractor) Send XML instead of Java objects and make use of JAXB Send JSON instead of Java Objects (and make use of JAXB, google's GSON or Jackson) See solutions section for examples, we will deal with these later
  30. 30. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 30 Lab Implement SOA Facade (not more than 15 min.)
  31. 31. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 31 Lightweight asynchronous facade In contrast to the SOA facade pattern, asynchronous invocation might also be done 'internally' (pure EJB 3.1, not using JMS) Request/response communication is much easier to handle (no Request and Response queue) No guaranteed delivery, no storing of messages in a dedicated message store (e.g. a database / journal)
  32. 32. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 32 Lightweight asynchronous facade Methods to be invoked asynchronously needs to be annotated with @Asynchronous (and will be executed using a background thread) Result of invocation available through a Future object or AsyncResult object (which implements the Future interface) @Asynchronous public class UserManagementServiceBean implements UserManagementService, UserManagementServiceLocal { ... public Future findUserByCredentials(String user, String pwd) { return new AsyncResult(new User("peterp", "neverland"));
  33. 33. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 33 The client Access the object through the Future object public void testFindUserByCredentials() { Future result = facade.findUserByCredentials("peterp", "neverland"); while(! result.isDone()) { //do something else ... } try { // what timeframe is accepted ('til you'll get an exception) User u = result.get(5, TimeUnit.SECONDS); ...
  34. 34. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 34 Lab Implement Lightweight Asynchronous Facade (not more than 15 min.)
  35. 35. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 35 Multichannel facade There might be the need to integrate non-Java clients JSR-181: WebService annotations allow customization of parameters names and methods A link explaining all annotations in the field of Web Services can be found below.
  36. 36. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 36 Multichannel facade Sample code: @Stateless(mappedName = "ejb/facade/UserManagementService") @Local(UserManagementServiceLocal.class) @Remote(UserManagementService.class) @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) @WebService(serviceName = "UserManagementService") public class UserManagementServiceBean implements UserManagementService, UserManagementServiceLocal { private Logger log = Logger.getLogger(this.getClass()); @WebMethod public User findUserByCredentials(String user, String pwd) { return new User("peterp", "neverland"); } @WebMethod(exclude=true) public User createUser(User u) { log.info("Creating user: " + u.getUser()); return u; } }
  37. 37. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 37 The client Sample code: public void testFindUserByCredentials() { try { // String wsdl = "http://localhost:8080/BRO130_3_4_MultiChannel_WS-solution/+ UserManagementService/UserManagementServiceBean?wsdl"; //xmlns:tns="http://control.userMgmt.brockhaus.de/" String nameSpaceURI = "http://control.userMgmt.brockhaus.de/"; // String serviceName = "UserManagementService"; URL wsdlLocation = new URL(wsdl); QName qName = new QName(nameSpaceURI, serviceName); Service service = Service.create(wsdlLocation, qName); UserManagementService mgmtService = service.getPort(UserManagementService.class); User user = mgmtService.findUserByCredentials("peterp", "neverland");
  38. 38. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 38 Lab Implement Multichannel Facade (not more than 15 min.)
  39. 39. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 39 Conclusion All types of facades mapping to one (or even more) SLSB, several 'inbound' channels like: RMI or POJI (as used in Dual View Pattern) JMS plus Java Object or XML / JAXB or JSON / Jackson (as used in SOA Facade Pattern) WebService and RESTful service (as used in MultiChannel Pattern)
  40. 40. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 40 Review Session Review: Can you name the differences between @Local and @Remote? Why do we need to deal asynchronously? What if we are leaving the Java world? How to combine all of it?
  41. 41. Copyright by Brockhaus GmbH, alle Rechte reserviert, unautorisierte Vervielfltigung untersagt 41 Recommeded reading http://java.sun.com/blueprints/corej2eepatterns/ http://www.corej2eepatterns.com/Patterns2ndEd/ Adam Bien, J2EE Patterns, Addison Wesley 2002, ISBN: 3-8273-1903-X Floyd Marinescu, Ed Roman: Ejb Design Patterns: Advanced Patterns, Processes, and Idioms; Wiley & Sons, ISBN-10: 0471208310 And other ... Photo: Bundesarchiv