Intro in JavaEE world (TU Olomouc)

download Intro in JavaEE world (TU Olomouc)

If you can't read please download the document

description

Presentation that was done on Technical University in Olomouc (Apr-2008)

Transcript of Intro in JavaEE world (TU Olomouc)

  • 1. JavaEE TourPetr Blaha Sun Microsystems, Inc 1

2. Agenda Java World introduction JavaEE (Why to use this technology, APIs) Dive in Enterprise Java Beans Demo Q&A 2 3. Java First implementation released in 1995 (1.1) Java compiled to bytecode -> JVM Basic Philosophy > Object-oriented > Platform independent > Automatic memory management Current version 63 4. The Java Platform multiple configurations built for different types Java Technology Java Technology Workgroup High-EndEnabled Devices Enabled DesktopServerServer 4 5. The JavaTMPlatformJava 2 Platform Micro Edition(JavaMETM)OptionalPackagesOptional Packages Personal PersonalJava 2 Java 2 Basis ProfileProfile Enterprise StandardEdition Edition Foundation Profile MIDP (JavaEE) (JavaSE)JavaCDC CLDCCardAPIsJVM KVMCardVM* Under development in JCP5 6. What Is the JavaEE? Open and standard based platform for developing, deploying and managing n-tier, Web-enabled, server-centric, and component-based enterprise applications6 7. Evolution of Enterprise Application Development Frameworks7 8. Single Tier (Mainframe-based) Dumb terminals are directly connected to mainframe Centralized model (as opposed distributed model) Presentation, business logic, and data access are intertwined in one monolithic mainframe application8 9. Single-Tier: Pros & Cons Pros: > No client side management is required > Data consistency is easy to achieve Cons: > Functionality (presentation, data model, business logic) intertwined, difficult for updates and maintenance and code reuse 9 10. Two-Tier SQL request Database SQL response Fat clients talking to back end database > SQL queries sent, raw data returned Presentation,Business logic and Data Model processing logic in client application 10 11. Two-Tier Pro: > DB product independence (compared to single-tier model) Cons: > Presentation, data model, business logic are intertwined (at client side), difficult for updates and maintenance > Data Model is tightly coupled to every client: If DB Schema changes, all clients break > Updates have to be deployed to all clients making System maintenance nightmare > DB connection for every client, thus difficult to scale > Raw data transferred to client for processing causes high network traffic 11 12. Three-Tier (RPC based)SQLRPC request request DatabaseRPC response SQL response Thinner client: business & data model separated from presentation > Business logic and data access logic reside in middle tier serverwhile client handles presentation Middle tier server is now required to handle system services > Concurrency control, threading, transaction, security, persistence,multiplexing, performance, etc. 12 13. Three-tier (RPC based): Pros & Cons Pro:> Business logic can change more flexibly than 2-tiermodel > Most business logic reside in the middle-tier server Cons:> Complexity is introduced in the middle-tier server> Client and middle-tier server is more tightly-coupled (than the three-tier object based model)> Code is not really reusable (compared to objectmodel based)13 14. Three-Tier (Web Server) SQL HTML requestWEBrequest ServerDatabase HTML responseSQLresponse Browser handles presentation logic Browser talks Web server via HTTP protocol Business logic and data model are handled by dynamic contents generation technologies14 15. Three-tier (Web Server based): Pros & Cons Pro: > Ubiquitous client types > Zero client management > Support various client devices> J2ME-enabled cell-phones Cons: > Complexity in the middle-tier still need to be addressed15 16. Trends Moving from single-tier or two-tier to multi- tier architecture Moving from monolithic model to object- based application model Moving from application-based client to HTML-based client16 17. Outstanding Issues & Solution Complexity at the middle tier server still remains Duplicate system services still need to be provided for the majority of enterprise applications > Concurrency control, Transactions > Load-balancing, Security > Resource management, Connection pooling How to solve this problem? > Commonly shared container that handles the above system services > Proprietary versus Open-standard based 17 18. Proprietary Solution Use "component and container" model > Components captures business logic > Container provides system services The contract between components and container is defined in a well-defined but with proprietary manner Problem of proprietary solution: Vendor lock-in Example: Tuxedo, .NET18 19. Open and Standard Solution J2EE Use "component and container" model in which container provides system services in a well- defined and as industry standard JavaEE is that standard that also provides portability of code because it is based on Java technology and standard-based Java programming APIs19 20. J2EE is End-to-End SolutionFirewallJ2EE ApplicationClient ServerEnterprise Enterprise InformationJavaBeansClient Systems (EIS): Client RelationalDatabase, Web EnterpriseLegacy Client Server JavaBeans Applications,JSP, ERP Systems Servlets ClientHTML/XML Other Services: JNDI, JMS, EnterpriseClient MiddleJavaMail Information Tier TierTier 20 21. J2EE Application DEMO 21 22. J2EE 1.4 APIs and Technologies J2SE 1.4 Servlet 2.4 JAX-RPC JSP 2.0 Web Service for J2EE EJB 2.1 J2EE Management JAXR J2EE Deployment Connector 1.5 JMX 1.1 JACC JMS 1.1 JAXP 1.2 JTA 1.0 JavaMail 1.3 JAF 1.022 23. The J2EE ChallengeTM J2EE is enormously powerful > The industry standard for robust enterprise apps But that power sometimes gets in the way > Too difficult to get started > Even simple apps need boring boilerplate Can we keep the power, but make typical development tasks simpler? YES: and that is the focus of Java EE 5!23 24. EoD Improvements in Java EE 5TM POJO-based programming > More freedom, fewer requirements Extensive use of annotations > Reduced need for deployment descriptors Resource Injection > Inversion of control New APIs and frameworks24 25. Annotations in Java EE 5 TM Making extensive use of annotations > For defining and using web services > To map Java classes to XML > To greatly simplify EJB development > To map Java classes to databases > To specify external dependencies > To reduce need for deployment descriptors25 26. Java EE 5 Major Features TM Simplified web services support More web service standards support Greatly simplified EJBTM development New persistence API Easy web applications with JavaServerTM Faces 26 27. Java EE 5 Platform JSR 220 (EJB 3.0) JSR 224 (JAX-WS 2.0) JSR 222 (JAXB 2.0) JSR 252 (JavaServer Faces 1.2) JSR 52 (JSTL 1.1) JSR 181 (WS Annotations) JSR 245 (JSP 2.1) 27 28. EJB (Enterprise Java Beans) 28 29. What is EJB Technology? A server-side component technology Easy development and deployment of Java technology-based application that are: > Transactional, distributed, multi-tier, portable, scalable, secure, 29 30. Why EJB Technology? Leverages the benefits of component-model on the server side Separates business logic from system code > Container provides system services Provides framework for portable components > Over different J2EE-compliant servers > Over different operational environments Enables deployment-time configuration > Deployment descriptor 30 31. EJB Architecture31 32. Enterprise JavaBeans Enterprise JavaBeans Synchronous communicationAsynchronous communicationSession BeanEntity BeanMessage-Driven Bean Stateless Stateful32 33. EJB 2.1 Example // EJB 2.1public class PayrollBean implements javax.ejb.SessionBean {SessionContext ctx; DataSource empDB;public void setSessionContext(SessionContext ctx) {this.ctx = ctx; }public void ejbCreate() { Context initialContext = new InitialContext(); empDB = (DataSource)initialContext.lookup( java:comp/env/jdbc/empDB); } 33 34. EJB 2.1 Example // EJB 2.1 (cont.)public void ejbActivate() {} public void ejbPassivate() {} public void ejbRemove() {}public void setBenefitsDeduction (int empId, double deduction) { ... Connection conn = empDB.getConnection(); ... } ... }// NOTE deployment descriptor needed34 35. EJB 2.1 Example PayrollBeanPayrollHomePayrollcom.example.PayrollBeanStatelessContainerjdbc/empDBjavax.sql.DataSourceContainer ... ... 35 36. Bean Classes In EJB 3.0, session beans, message-driven beansare ordinary Java classes> Container interface requirements removed> Bean type specified by annotation or XML Annotations> @Stateless, @Stateful, @MessageDriven> Specified on bean class 36 37. EJB 3.0 Example // Same example, EJB 3.0@Stateless public class PayrollBeanimplements Payroll {@Resource DataSource empDB;public void setBenefitsDeduction (int empId, double deduction) { ... Connection conn = empDB.getConnection(); ... }... }37 38. Dependency Injection Annotations > @EJB> References to EJB business interfaces> References to Home interfaces (when accessingEJB 2.1 components) > @Resource> Almost everything else > Number of annotations is simplified from EJB 3 specification early draft Injection can also be specified using deployment descriptor elements38 39. EJB 3.0 Client Example // EJB 3.0 client view@EJB ShoppingCart myCart; ... Collection widgets = myCart.startToShop(widgets); ... 39 40. EJB 3.0 & Persistence 40 41. Persistence Model in EJB 3.0 Entities are simple Java classes > Concrete classessupport use of new > Getter/setter property methods or persistent instance variables > No required bean interfaces > No required callback interfaces Usable as detached objects in other applicationtiers > No more need for DTOs Single API in JavaEE, Java SE41 42. EntityManager EntityManager serves as untyped home for entity operations> Methods for lifecycle operations> Persist, remove, merge, flush, refresh, etc. > Similar in functionality to Hibernate Session, JDO PersistenceManager, etc. > Manages persistence context> Both transaction-scoped and extended persistence contexts42 43. Persistence Focus: O/R Mapping Ease-of-use facility for Java developers mapping domain object model to a relational database Developer is aware of mapping between DB schema and domain object model > Developer is in control of mapping > Developer can rely on mapping and its semantics Mappings may be expressed using metadata annotations or XML > Default mappings provided43 44. JPA Entity Classimport javax.persistence.*; @Entitypublic class Person { @Id private String name; private int visits; public Person() { } public Person(String name) { this.name = name;} public int incrementVisit() { return visits++;}}44 45. Persistence Entity Example (Contd.)...// Relationship between Customer and Orders@OneToManypublic Collection getOrders() {return orders;}public void setOrders(Collection orders) {this.orders = orders;} 45 46. Persist Operation public Order createNewOrder(Customer customer) { // Create new object instance transient state Order order = new Order(customer);// Transitions new instances to managed. On the // next flush or commit, the newly persisted // instances will be inserted into the database table. entityManager.persist(order); return order; 46 47. Find and Remove Operations public void removeOrder(Long orderId) { Order order = entityManager.find(Order.class, orderId);// The instances will be deleted from the table // on the next flush or commit. Accessing a // removed entity has undefined results. entityManager.remove(order); } 47 48. Persistence DEMO48 49. JavaEE TourPetr Blaha Sun Microsystems, Inc 49