"Web Age Speaks!" Webinar Series Java EE Patterns Revisited WebAgeSolutions.com 1
Introduction Bibhas Bhattacharya CTO bibhas@webagesolutions.com Web Age Solutions Premier provider of Java & Java EE training and consulting One of the first to offer training on Java EE 5 topics like EJB 3.0 WebAgeSolutions.com 2
Overview of Talk Rethinking patterns Why is it important? Where do patterns go to retire? Changes to Business tier patterns Changes to Integration tier patterns Changes to Presentation tier patterns Our offerings WebAgeSolutions.com 3
Java EE Patterns Revisited Rethinking Patterns WebAgeSolutions.com 4
J2EE Patterns History The Core J2EE Patterns book and other similar resources were released in the 2001-2004 timeframe This was certainly needed as the J2EE platform of that timeframe was significantly harder to work with than today The Java EE standards have not stopped moving though New versions continued to come out with a chance to address the problems with prior versions With the release of Java EE 6 it could be argued that the rate of change and improvement is accelerating, not slowing down WebAgeSolutions.com 5
This is Where We Start From From corej2eepatterns.com WebAgeSolutions.com 6
Rethinking Patterns The J2EE patterns were developed to address specific weaknesses and provide best practice solutions to prior versions of the platform Later versions of the platform, especially Java EE 5, changed the technologies and solutions available to projects Continuing to apply old patterns to the new technologies may have an undesired effect The effect of not rethinking patterns can be: Creating unneeded code to maintain Not applying the best solution with new technologies We are assuming a familiarity with the patterns and will focus on the changes to the patterns WebAgeSolutions.com 7
Where Do Patterns Go To Retire? A pattern is a combination of: Common business problem. Eg. Need for optimistic locking. A solution approach or idea that is generally accepted as sound. Eg. Use of version number column. Implementation technique using a specific platform and programming language, such as Java EE or C#/.NET. The problems themselves and the sound solution approaches do not go away with Java EE 5. Eg. Need for optimistic locking and use of version number column are still there. However, Java EE 5 changes the implementation technique significantly for many of the patterns. For several patterns Java EE 5 provides a built-in solution and you have to write little or no code to implement the solution. Eg. Optimistic locking support in JPA. Retirement refers to this built-in nature of the solution implementation. WebAgeSolutions.com 8
Describing Pattern Changes We will use the following to describe the changes to patterns Status Active Still applies with perhaps new solutions Semi-retired Active in some situations even with new technologies Retired Does not apply or built-in when using new technologies Technologies with largest impact on the pattern Why the rethinking? Perhaps when the pattern still applies Eye to Java EE 6 How this pattern may change again in a Java EE 6 environment WebAgeSolutions.com 9
Java EE Patterns Revisited Changes to Business Tier Patterns WebAgeSolutions.com 10
Façade / Application Service Status Active Technologies EJB 3.0 The purpose of both patterns is to abstract and combine functionality Façade provides a use-case view of business logic exposed to external clients Application Service provides reusability of business logic across façade implementations Both are about layering of business logic Although the purpose doesn t change, the simplified programming of EJB 3.0 makes it easier to implement this with EJBs EJB 3.0 implementations probably become more simplified compared to POJO due to robust security, transaction, etc without need for third party framework WebAgeSolutions.com 11
Business Object Status Active Technologies JPA The restriction of the managed persistence of Entity EJBs to the EJB container and the lack of EJB inheritance and polymorphism created an anemic domain model and a procedural approach to adding behavior to that model With these restrictions lifted using JPA we can create a truly object-oriented domain model that combines state and behavior Any business logic that acts solely on one instance is implemented within the business object directly Although there are still some challenges related to detached JPA entities to deal with we clearly need to rethink the best practice of J2EE applications which completely separated state from behavior WebAgeSolutions.com 12
Value List Handler Status Alive Technologies JPA The JPA EntityManager provides a built-in way to provide a subset of the results of persistent data queries Now we can simply provide an implementation of the standard Iterator pattern and use state to remember the position in the query list We don t need to cache the whole query results, just the position in the list WebAgeSolutions.com 13
Version Number Status Alive Technologies JPA JPA now has this built-in with the @Version annotation The implementation changes drastically, and is now almost trivial, as we do not need to implement the comparison of the version attribute of the detached and managed data sets WebAgeSolutions.com 14
Transfer Object (DTO, Value Object) Status Semi-retired Technologies JPA Business Objects can be directly used as DTO. This pattern evolved because Entity EJB data could not be passed into the web tier With JPA we have non-ejb Java objects that can be passed in and out of the EJB tier With JPA you have to be concerned with the management of detached entities Proper use of an extended persistence context, perhaps with a framework like Seam, can address this The pattern can still be used if the data model differs among layers of the application WebAgeSolutions.com 15
Service Locator Status Semi-retired Technologies Managed components like Servlets, EJBs, and JSF managed beans The limited injection of Java EE 5 lets most needs for this pattern be addressed with injections Configuration of remote EJB lookup, web service client, and components not injectable could still benefit from the pattern Java EE 6 A robust dependency injection model will be expanded with the introduction of JSR 299, Contexts and Dependency Injection WebAgeSolutions.com 16
Transfer Object Assembler / Composite Entity Status Semi-retired Technologies JPA JPA can model complex and nested relationships so no need to define a separate data model for relationships WebAgeSolutions.com 17
Business Delegate Status Retired Technologies EJB 3.0 In EJB 3.0 checked exceptions are optional One Business Delegate function was to catch and hide exceptions from clients Business Delegate also hid the use of a Service Locator which is reduced also The Business Delegate and Façade patterns basically merge into just Façade pattern Java EE 6 The robust dependency injection in Java EE 6 reduces the situations for bringing this pattern out of retirement WebAgeSolutions.com 18
Java EE Patterns Revisited Changes to Integration Tier Patterns WebAgeSolutions.com 19
Service Activator Status Active Technologies EJB 3.0 Although the purpose of the pattern does not change (asynchronous invocation of services) implementing with MDB becomes easier with EJB 3.0 Java EE 6 The implementation of this pattern will change as EJB 3.1 adds asynchronous invocation of session EJBs You will no longer need to use messaging to achieve this if not needed WebAgeSolutions.com 20
Data Access Object / Domain Store Status Semi-retired for JDBC data access. Technologies JPA The JPA EntityManager is already an abstraction of the data access and the JPA persistence provider implements the interaction with different databases In JPA 1.0 queries are defined by JPQL Strings so the need for abstraction of the data access layer could be required for complex queries Java EE 6 JPA 2.0 will introduce Criteria queries which will allow the creation of type-safe queries checked by the compiler instead of String JPQL queries This will further reduce the need for data access abstraction on top of the JPA EntityManager WebAgeSolutions.com 21
Java EE Patterns Revisited Changes to Presentation Tier Patterns WebAgeSolutions.com 22
Lack of Standardized J2EE Web Framework Most of the J2EE presentation patterns arise from the fact that there is not a standardized web framework required by J2EE 1.4 Struts was a de-facto standard but not part of J2EE platform JSF was released but support not required JSF 1.2 support is required as part of Java EE 5 compatibility Most of the goals of the J2EE presentation patterns are implemented with these frameworks so little custom pattern implementation is required WebAgeSolutions.com 23
Framework Promoted Patterns Struts and JSF promote implementation of several patterns Front Controller Both provide servlets that process all incoming requests Model/View/Controller Both promote the separation of concerns within the application View Helper Both provide dynamic data to the view as helper objects Application Controller Both provide modular request processing logic Both provide an extension to promote the Composite View pattern Struts Tiles JSF Facelets (This will be part of the JSF 2.0 standard of Java EE 6) WebAgeSolutions.com 24
Java EE Patterns Revisited Web Service Patterns WebAgeSolutions.com 25
Façade / Application Service Status Active Technologies JAX-WS These patterns can also be implemented by JAX-WS web services The JAX-WS programming is simplified and relies less on vendor-specific generated code This is not to say that web services should be used in all places these patterns may be applied Web services should only be used for functionality that is used by applications deployed to different environments and/or developed by different teams Instances where these patterns are applied internally to an application should still use local technologies WebAgeSolutions.com 26
Adapter Services / Wrapper Services Legacy applications that need to be integrated with other applications/systems may be difficult to do Using web services to implement the integration with these systems will be based more on standards and provide more future flexibility An Adapter service may be a service that is provided by the legacy application or web service environment A Wrapper service may be custom developed specifically for that application Don t look to expose all of the function of the legacy application, only expose what is needed to simplify management of the service WebAgeSolutions.com 27
Reliable Messaging Previously the only way to get reliable messaging was to use a messaging platform which required both sides to have access to that platform This also tied both sides to being able to send messages in perhaps a proprietary way With the development of the WS-RM (Web Services Reliable Messaging) standard we can use web services to accomplish the same pattern This will provide more flexibility to how the producer and consumer of the messages is implemented This is a rather new standard so both sides would have to support it WebAgeSolutions.com 28
Java EE Patterns Revisited AJAX Patterns WebAgeSolutions.com 29
What is AJAX? AJAX stands for Asynchronous JavaScript and XML AJAX is an approach to developing web application that differs from the traditional way of developing applications In a traditional application, user submits a form or clicks on a link, the browser sends a HTTP request to the server, the server replies with a fresh new HTML document that the browser renders as a new page AJAX differs in two main ways: The browser makes a HTTP request that may or may not be due to a user action. For example, a clock application may automatically make a HTTP request every second The reply from such a request does not contain a full new page. It contains information that is used to update portions of the existing page WebAgeSolutions.com 30
Partial Page Update Problem When user submits a form or clicks a link to view information, the entire page has to be updated. The process is time consuming and lowers productivity Example scenarios: User adds a product to shopping cart, and a summary information about the cart is shown on page without reloading the page User deletes an item from the cart, the row is removed from the table without refreshing the page Solution: Use AJAX to fetch updated data from the server. Use DOM API to display the data on the page WebAgeSolutions.com 31
Realtime Form Validation Problem In a conventional web application, only very basic validation is performed in the client side. Server side validation requires the form to be submitted and the entire page updated The process can be time consuming in a busy web site with complex pages Solution Use AJAX to validate user input and display error message before the form is submitted. You can do that: As the user is typing (from the onkeyup event handler) As user leaves a field (from onchange or onblur event handler) When the user clicks the submit button, you can submit the form using AJAX WebAgeSolutions.com 32
Automatic Form Completion Problem: To search for information, user has to enter input, submit the form and then view the result in a new page User has to manually enter a lot of data, when some of the data could be retrieved and filled in from the server User has to perform separate lookups and then copy and paste data in the form Example scenarios: User selects a country from a drop down list, the entire page is updated to populate the list of states or provinces User is looking at a long list of employees. To filter out the list, user enters a last name and clicks submit. The whole page is refreshed to show the filtered list To edit an address, user clicks on the contact's name. In the next page, the form is pre-filled with the contact's address Solution: As the user enters data, use AJAX to retrieve information and display them on the same page. You can also use the information to pre-populate form fields This pattern is quite popular in search fields. As the user enters data, possible search hit terms are displayed in a drop down menu below the text box WebAgeSolutions.com 33
Progress or Status Indicator Problem: After an AJAX request is sent, the browser does not use spinning logo or progress indicator to keep the user informed Also, after the reply comes back, user has no visual cue if the action was successful or not Solution: Prior to sending the AJAX request, show an animated GIF or plain text to inform the user of pending action When the response comes back, show a success or error message. Avoid using alert() for the message, use DOM WebAgeSolutions.com 34
Java EE Patterns Revisited Our Offerings WebAgeSolutions.com 35
Patterns and Best Practices Courses We have updated versions of patterns courses that reflect the information in this presentation Courses go into much more detail with sample code and hands-on labs Other topics like various testing tools and techniques are covered also Using Eclipse/JBoss 5.0 WA1713 Using RAD 7.5/WebSphere 7.0 WA1778 WebAgeSolutions.com 36
WA1713: Java EE Design Patterns Live Virtual Training: Date: 05/10/2010 WA1713 Java EE Design Patterns Discount: $2,395/student for the 5 day class. Register a second student for the same class and same date and receive 50% off the fee for the second student. Fee is due at least 10 business days before 1st day of class Price: $2,395 Registration link: http://www.webagesolutions.com/training/register/public/register.html?wa1713 Samples: http://www.webagesolutionsfiles.com/samples/wa1713-sample_chapter.pdf http://www.webagesolutionsfiles.com/samples/wa1713-sample_lab.pdf WebAgeSolutions.com 37
Java EE 5 Courses Many new courses on Java EE 5 www.webagesolutions.com/training/java/ J2EE.html Java EE 5 Coursemap http://www.webagesolutions.com/ coursemaps/j2ee/wajavaeecoursemap.pdf Paths for experienced J2EE developers and EJB/non-EJB options Courses use standardized Eclipse and JBoss 5 setup Easy to customize or mix content if desired WebAgeSolutions.com 38
Other Platforms We also have many courses on Java EE 5 for other platforms WebSphere 7.0/RAD 7.5 www.webagesolutions.com/training/ websphere/programmingv75.html Oracle WebLogic www.webagesolutions.com/training/ weblogic/weblogic10/programming.html WebAgeSolutions.com 39
Upcoming Webinars What s new in WebSphere Process Server 7.0 Programming Wednesday, April 7, 2010 Noon ET https://www2.gotomeeting.com/register/3 68195802 WebAgeSolutions.com 40
Summary Patterns are meant to solve problems with standard solutions If technologies have changed since the patterns were developed the solutions available may be different Failure to adjust to the new technology landscape may create unneeded code and poor solutions WebAgeSolutions.com 41
Contact and Resources Stuart Smith stuart.smith@webagesolutions.com Bibhas Bhattacharya bibhas.bhattacharya@webagesolutions.com Web Age Solutions www.webagesolutions.com/contactus/ US - 215-517-6540 Canada - 416-406-3994 Java EE information http://java.sun.com/javaee/ WebAgeSolutions.com 42