Presentation is loading. Please wait.

Presentation is loading. Please wait.

JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005.

Similar presentations


Presentation on theme: "JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005."— Presentation transcript:

1 JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005

2 2October 16, 2005 Agenda  JBoss Introduction  JBuilder JBoss Server Configuration  JBoss Services  EJB 3 Introduction  Annotations  EJB Annotation  Persistence Model

3 3October 16, 2005 What is JBoss?  Open Source J2EE Application Server ++  Versions  2.4.x  J2EE 1.3 Spec  4.0.1  J2EE 1.4 Spec  Refactor Server  4.0.3 (current)  EJB 3 Capable

4 4October 16, 2005 JBoss Installation  Version < 4.0.3  unzip or extract tar to a directory  /usr/local$ sudo tar xf jboss-4.0.3.tar  Version 4.0.3 +  New Java-based installer  From Web Start or  ~/downloads$ jar./jboss-4.0.3RC1-installer.jar

5 5October 16, 2005 JBoss Directory Structure  jboss-4.0.3  bin – run / stops scripts  client – client needed jars  docs – dtds, schemas and examples  lib – server side jars  server  default  conf – server instance configuration files  data – server instance db file (hypersonic)  deploy – deployment directory  lib – server instance libraries  log – server instance logs  tmp – location deployments are extracted to  work – web work directory

6 6October 16, 2005 Configuration of JBuilder  Enterprise --> Configure Servers  Select Jboss 4.0.1+  Configure home directory

7 7October 16, 2005 Starting a Project  Copy the preferred server configuration  Create JB project select this new server configuration for the deployment.  Create EJBs... etc.

8 8October 16, 2005 Managing JBoss  Jboss is running and ready when you see  14:59:11,304 INFO [Server] JBoss (MX MicroKernel) [4.0.3 (build: CVSTag=JBoss_4_0_3 date=200510042341)] Started in 34s:106ms  Web administration  http://localhost:8080/ http://localhost:8080/  Tomcat status  JMX Console  Jboss Web Console

9 DEMO – Stateless Session

10 10October 16, 2005 Tricks and Tips  Multi-Server  List deployments  Disable services

11 11October 16, 2005 Multi-Server  Configure JBoss for EJBs  Configure Tomcat for WAR. DEMO

12 12October 16, 2005 Agenda  JBoss Introduction  JBuilder JBoss Server Configuration  JBoss Services  EJB 3 Introduction  Annotations  EJB Annotation  Persistence Model

13 13October 16, 2005 SAR – Service ARchive  JAR with a SAR extension  JBoss specific  JMX components

14 14October 16, 2005 Creating SAR  Create JMX MBean  Create SAR  New Object Gallery  Select Enterprise --> Jboss Service Module  Assign Mbean class  Assign Name  JBuilder is misleading the name must have a domain!!!

15 15October 16, 2005 Agenda  JBoss Introduction  JBuilder JBoss Server Configuration  JBoss Services  EJB 3 Introduction  Annotations  EJB Annotation  Persistence Model

16 16October 16, 2005 Because of EJBs...  I became an expert at:  class loading  Transaction management  I got to jump through hoops  ejbPassivate.... for my stateless session bean ???  In the end...  was I more productive?  was the end result portable?

17 17October 16, 2005 EJB 3 Specification  JSR 220  public Review 27 Jun, 2005  3 Specification Documents  EJB 3.0 Simplified API  Quick Reference with great sample code  Java Persistence API  Lightweight (replacement) persistence framework  EJB Core Contracts  EJB Details outside of persistence

18 18October 16, 2005 EJB 3 Contributions  The EJB 2.1 Specification (JSR 153, Enterprise JavaBeans 2.1)  The J2EE 1.4 Platform Specification (JSR 151)  JSR 14 Add Generic Types to the JavaTM Programming Language  JSR 175 A Metadata Facility for the JavaTM Programming Language  JSR 181 Web Services Metadata for the JavaTM Platform  JSR 201 Enumerations, Autoboxing, Enhanced for loops and Static Import  JAX-RPC 2.0  JDBC 4.0

19 19October 16, 2005 EJB 3 Goals  The EJB 3.0 release is focused on a simplification of the Enterprise JavaBeans architecture from the developer’s point of view.  Elimination of requirements for:  home and component interfaces  the specification of the javax.ejb.EnterpriseBean interfaces.  Definition of a dependency injection  Facility and simpler look-up APIs.  Java metadata annotations vs. deployment descriptors.  Simplification of object persistence by the definition of a light-weight object/relational mapping  facility based on the direct use of Java classes rather than persistent components.

20 20October 16, 2005 Agenda  EJB 3 Introduction  Annotations  EJB Annotation  Persistence Model

21 21October 16, 2005 MetaData is Required in EJB 3  Annotations (JSR-175) were incorporated in JDK 1.5  Annotations seem like JavaDoc but are quite different. JavaDoc /** *@deprecated */ public void doThis() {... } Annotations @deprecated ??? public void doThis() {... }

22 22October 16, 2005 Annotation Types  Marker Annotations  @MarkerAnnotation  Single-value Annotations  @SingleValueAnnotation(“value”)  Full Annotations  @FullAnnotation( value1=”x”, value2=”y”)

23 23October 16, 2005 JDK Built-in Annotations  Override  Marker to indicate the intention to override  Deprecated  Marker (which is a duplication of the @deprecated javadoc)  SuppressWarnings  Suppresses warnings public class DeprecatedUser2 { @SuppressWarnings({"deprecation"}) public static void main(String[] args) { DeprecatedExample2.foo(); }

24 24October 16, 2005 Agenda  EJB 3 Introduction  Annotations  EJB Annotation  Persistence Model

25 25October 16, 2005 EJB 3 Annotations  @EJB  @Stateless  @Stateful  @Remote  @NamedQuery  @MessageDriven  @ActivationConfigProperty  @TransactionManagemen t  @TransactionAttribute  @RemoteHome  @Local  @LocalHome  @Interceptors  @AroundInvoke  @Inject  @Resource  @EJBs

26 26October 16, 2005 EJB 3 Annotations  @CallbackListener  @PostConstruct  @Timeout  @ApplicationException  @RolesReferenced  @RolesAllowed  @PermitAll  @DenyAll  @RunAs  @SecurityRoles  @Init  @PreDestroy  @PrePassivate  @PostConstruct  @PostActivate  @Remove

27 27October 16, 2005 Field Level Injection  Resource evaluates to JNDI name of resource  java:/com/env/com.codementor.devcon/ejbDS  Alternatively it can be declared  @Resource(name="jdbc/sqltestDB",type=javax.sql.DataSource.cl ass) package com.codementor.devcon; import javax.ejb.*; import javax.annotation.*; @Stateful public class StatefulBean implements RemoteStatefulInterface{ @Resource javax.sql.DataSource ejbDS; @Resource javax.mail.Session ejbmailSession; }

28 28October 16, 2005 Setter Injection  Same example on the setter of the EJB @Resource(name="jdbc/customerDB") public void setDataSource(DataSource myDB) { this.ds = myDB; } private DataSource myDB;

29 29October 16, 2005 EJB References  The following are examples of references to EJBs in JEE 5. @EJB LocalStatelessInterface sless; @EJB(name="ejb/stateless") LocalStatelessInterface sless; @EJB(name="ejb/stateless") RemoteStatelessHome slessHome;

30 30October 16, 2005 Explicit Dependency Lookup EJB 2.1: Initial Context ic=new InitialContext(); Object o=ic.lookup("ejb/FirstStateless"); statelessHome= (StatelessRemoteHome)PortableRemoteObject.narrow(objref, StatelessRemoteHome.class); EJB 3.0: @Resource SessionContect sc public void setup(){ FirstStatelessInterface fi=(FirstStatelessInterface)sc.lookup("ejb/FirstStateless"); }

31 31October 16, 2005 Stateless Session Example @Stateless public class ConferenceBean implements Conference { @Resource private EntityManager em; private ConfSession session; public ConfSession findSessionByID(int sessionID) { return ((ConfSession) em.find(ConfSession.class, sessionID)); } public void addSession(long sessionID, String title, Date date) { session = new ConfSession(); session.setTitle(title); session.setDate(date); session.setId(sessionID); em.persist(session); }

32 32October 16, 2005 Agenda  EJB 3 Introduction  Annotations  EJB Annotation  Persistence Model

33 33October 16, 2005 Persistence Framework  Packaging  Annotations / Mapping  API – EntityManager

34 34October 16, 2005 There's a new Archive in Town - PAR  PAR – Persistence Archive  Contains “persistence units”  Portability...  Contains persistence.xml  Details for the entity manager  provider  jta-data-source  non-jta-data-source  mapping-file  jar-file  class  properties

35 35October 16, 2005 Persistence.xml em1 com.acme.persistence jdbc/MyDB ormap.xml MyApp.jar com.widgets.Order com.widgets.Customer

36 36October 16, 2005 With or Without You  Any PAR without a persistence.xml will get the container defaults.

37 37October 16, 2005 Persistence Framework  Packaging  Annotations / Mapping  API – EntityManager

38 38October 16, 2005 EJB 3 Persistence  Good Bye Entity Beans!!!  EJB 3 Provides an ORM framework similar to Hibernate / JDO  No Descriptors necessary  Annotated POJOs are the persistent classes

39 39October 16, 2005 Entity Annotations  @Entity  named used to refer to persistent class  name – property defaults to name of class  access – property or field access  Callbacks  @PrePersist  @PostPersist  @PreRemove  @PostRemove  @PreUpdate  @PostUpdate  @PostLoad

40 40October 16, 2005 Entity Annotations  @PersistenceContext  Provides EntityManager  @PersistenceUnit  Provides EntityManagerFactory  @EntityTransaction

41 41October 16, 2005 Life-Cycle Method Example @Entity @EntityListener(com.acme.AlertMonitor.class) public class AccountBean implements Account { Long accountId; Integer balance; boolean preferred; public Long getAccountId() {... } public Integer getBalance() {... } @Transient // because status depends upon non-persistent context public boolean isPreferred() {... } public void deposit(Integer amount) {... } public Integer withdraw(Integer amount) throws NSFException {... } @PrePersist public void validateCreate() { if (getBalance() < MIN_REQUIRED_BALANCE) throw new AccountException("Insufficient balance to open an account"); } @PostLoad public void adjustPreferredStatus() { preferred = (getBalance() >= AccountManager.getPreferredStatusLevel()); }

42 42October 16, 2005 Life-Cycle Example Complete public class AlertMonitor { @PostPersist public void newAccountAlert(Account acct) { Alerts.sendMarketingInfo(acct.getAccountId(), acct.getBalance()); }

43 43October 16, 2005 Persistence Framework  Packaging  Annotations / Mapping  API – EntityManager

44 44October 16, 2005 EJB3 Persistence Interfaces  EntityManager  Interface to interact with persistence context.  EntityManagerFactory  Creates an EntityManager  Query  Queries made through this interface

45 45October 16, 2005 EJB Bean – Getting an Entity Manager Example 1: @PersistenceContext public EntityManager em; Example 2: @PersistenceContext(unitName="order") EntityManager em; //here only one persistence unit exists @PersistenceContext(type=PersistenceContextType.EXTENDED) EntityManager orderEM;

46 46October 16, 2005 Persisting with the Manager public void addSession(long sessionID, String title, Date date) { session = new ConfSession(); session.setTitle(title); session.setDate(date); session.setId(sessionID); em.persist(session); }

47 47October 16, 2005 Querying the Manager @Stateless public class ConferenceEJB implements Conference {... public ConfSession findSessionByTitle(String title) { String query = "SELECT OBJECT(session) FROM ConfSession “ + ”conSession WHERE conSession.title = :sessionName"; ConfSession session = (ConfSession)em.createQuery(query).setParameter("sessionName", title).getSingleResult(); return session; }

48 48October 16, 2005 EJB Sample Code @Stateless public class ShoppingCartImpl implements ShoppingCart { @PersistenceContext EntityManager em; public Order getOrder(Long id) { return em.find(Order.class, id); } public Product getProduct(String name) { return (Product) em.createQuery("select p from Product p where p.name = :name").setParameter("name", name).getSingleResult(); } public LineItem createLineItem(Order order, Product product, int quantity) { LineItem li = new LineItem(order, product, quantity); order.getLineItems().add(li); em.persist(li); return li; } Notice whats missing!

49 49October 16, 2005 Agenda  EJB 3 Introduction  Annotations  EJB Annotation  Persistence Model

50 50October 16, 2005 EAR Configuration  EAR  contains an application.xml in the META-INF  contains EJB, PAR and WAR modules  PAR  contains persistence.xml... maybe  contains persistence classes  EJB  contains EJB classes  WAR  contains JSP, servlet classes  web.xml

51 51October 16, 2005 Questions

52 52October 16, 2005 Session 3110 – JBoss Fundamentals with JBuilder Thank You 3110 JBoss Fundamentals with JBuilder Please fill out the speaker evaluation You can contact me further at … kensipe@codementor.net Thank You


Download ppt "JBoss Fundamentals with JBuilder: Session #3110 Ken Sipe Code Mentor, Inc. Nov. 5, 2005."

Similar presentations


Ads by Google