Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.

Similar presentations


Presentation on theme: "Enterprise Java Beans. Contents  Understanding EJBs  Practice Section."— Presentation transcript:

1 Enterprise Java Beans

2 Contents  Understanding EJBs  Practice Section

3 1. Understanding EJBs  Types of EJBs  Anatomy of an EJB  EJB Container  Embedded Container  Dependency Injection and JNDI  Packaging

4 1.1.Types of EJBs EJBs are server-side components that encapsulate business logic and take care of transactions, security … Architecture layering

5 The Java EE platform defines several types of EJBs. Session beans are used to encapsulate high- level business logic, which makes them the most important part of the EJB technology. A session bean may have the following traits:  Stateless: The session bean contains no conversational state between methods, and any instance can be used for any client.  Stateful: The session bean contains conversational state, which must be retained across methods for a single user.

6  Singleton: A single session bean is shared between clients and supports concurrent access.

7 1.2.Anatomy of an EJB Session beans encapsulate business logic, are transactional, rely on a container that does pooling, multithreading, security, and so on. A Simple Stateless EJB  The BookEJB is a is a stateless session bean that acts like a façade and handles CRUD operations on the Book entity.

8 The BookEJB becomes a stateless session bean. The EJB obtains a reference of an entity manager using dependency injection.

9 The client side invokes smethod on the BookEJB. The BookEJB is deployed in a container, the Main class (the client) needs to access the EJB remotely.  Adding a simple remote interface to the EJB will give it remote access capabilities.

10

11

12 1.3. EJB Container An EJB is a server-side component and needs to be executed in a container. This runtime environment provides core features common to many enterprise applications.  Remote client communication: Without writing any complex code, an EJB client (another EJB, a user interface, a batch process, etc.) can invoke methods remotely via standard protocols.  Dependency injection: The container can inject several resources into an EJB (datasources, other EJBs, environment variables,...).

13  State management: For stateful session beans, the container manages their state transparently.  Pooling: For stateless beans and message-driven beans (MDBs), the container creates a pool of instances that can be shared by multiple clients. Once invoked, an EJB returns to the pool to be reused instead of being destroyed.  Component life cycle: The container is responsible for managing the life cycle of each component.

14  Messaging: The container allows MDBs to listen to destinations and consume messages.  Transaction management: With declarative transaction management, an EJB can use annotations to inform the container about the transaction policy it should use. The container takes care of the commit or the rollback.  Security: Class or method-level access control can be specified on EJBs to enforce user and role authentication.

15  Concurrency support: Except for singletons, where some concurrency declaration is needed, all the other types of EJB are thread-safe by nature. You can develop high performance applications without worrying about thread issues.  Interceptors: Cross-cutting concerns can be put into interceptors, which will be invoked automatically by the container.  Asynchronous method invocation: With EJB 3.1, it’s now possible to have asynchronous calls without involving messaging.

16 Once the EJB is deployed, the container takes care of these features, leaving the developer to focus on business logic. When a client invokes an EJB, it doesn’t work directly with an instance of that EJB but rather with a proxy on an instance.  The call is actually proxied through the container, which provides services on behalf of the bean instance. Of course, this is completely transparent to the client.

17 In a Java EE application, the EJB container will usually interact with other containers: the servlet container (responsible for managing the execution of servlets and JSF pages), the application client container, or ACC (for managing stand-alone applications), the message broker (for sending, queuing, and receiving messages), the persistence provider, and so on.  These containers will all run inside an application server (GlassFish, JBoss, Weblogic, etc.).

18 1.4. Embedded Container EJBs have to be executed in a container that runs in a separate JVM. The application server first needs to be started before deploying and using your EJB. An issue with servers running in a different process is that unit-testing capabilities are limited, and unit tests cannot be easily run without deploying the EJB in a live server.  Some application server implementations came with embedded containers.

19 An embedded container is to be able to execute EJB applications within a Java SE environment allowing clients to run within the same JVM and class loader.  This provides better support for testing, offline processing (e.g., batch processing), and the use of the EJB in desktop applications. The embeddable container API (defined in javax.ejb.embeddable ) provides the same managed environment as the Java EE runtime container and includes the same services.

20 Creating an instance of an embeddable container, get a JNDI context, get a lookup for an EJB, and invoke a method:

21 1.5. Dependency Injection and JNDI EJBs use dependency injection to access several kinds of resources (other EJBs, datasources, environment resources, etc.).  In this model, the container pushes data into the bean. Injection is made at deployment time.  A client gets injected a reference to an EJB using the @EJB annotation: @EJB private static BookEJBRemote bookEJB;

22 JNDI is an alternative to injection; through JNDI, the code pulls data only if it is needed, instead of accepting pushed data that may not be needed at all. Using the InitialContext of JNDI and look up a deployed EJB named java:global/BookEJBRemote as follows:  Context ctx = new InitialContext(); BookEJBRemote bookEJB = (BookEJBRemote) ctx.lookup("java:global/BookEJBRemote");  Naming convention: java:global[/ ]/ / [! ]

23 1.6. Packaging EJBs need to be packaged before they are deployed into a runtime container.  In the same archive, you will usually find the enterprise bean class, its interfaces, interceptors, any needed superclasses or superinterfaces, exceptions, helper classes, and an optional deployment descriptor (ejb-jar.xml).  Once these artifacts are packaged in a jar (Java archive) file, they can be deployed directly into a container. Another option is also to embed the jar file into an ear (enterprise archive) file and deploy the ear file.

24 An ear file is used to package one or more modules (EJBs or web applications) into a single archive so that deployment into an application server happens simultaneously and coherently.

25 Since EJB 3.1, EJBs can also be directly packaged within a web module (war file).

26 2. Practice Section EJBs are transactional by nature, so our stateless session bean (BookEJB) will handle Create, Read, Update, Delete (CRUD) operations on the Book entity with container- managed transactions CMTs. The BookEJB and the Book entity will then be packaged and deployed into GlassFish. The EJB needs a remote interface, as an external client application (Main class) will invoke methods remotely on the EJB using the ACC.

27

28 1. Create an enterprise application  BookEnterpriseApplication

29

30 2. Create a datasource required by persistence unit. This datasource must be created using GlassFish administration console or command line.

31 Create JDBC connection pool

32

33

34 Create JDBC Resource

35

36 3. Write the Book entity in EBJ module

37

38

39 4. Writing the BookEJBRemote interface and BookEJB Stateless Session Bean

40

41 5. Writing the Main class in Application Client Module

42 6. Run the enterprise application

43 Run: Build, Undeploy, deploy, run Main class Check the database

44 6. Writing the BookEJBTest Class  To unit test the EJB The EJB embedded container.

45 Stop the GlassFish server and test the file.

46 A stand- alone java client to access Remote EJB component 1. Add gf-client.jar (glassfish\modules) to the classpath 2. Add the jar file of ejb module to the classpath

47 Reference Antonio Goncalves, Beginning Java EE 6 Platform with GlassFish 3, Chapter 6, Apress 2009


Download ppt "Enterprise Java Beans. Contents  Understanding EJBs  Practice Section."

Similar presentations


Ads by Google