• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
1-introduction to ejb
 

1-introduction to ejb

on

  • 375 views

Creating sample Sesssion Bean

Creating sample Sesssion Bean

Statistics

Views

Total Views
375
Views on SlideShare
375
Embed Views
0

Actions

Likes
0
Downloads
11
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    1-introduction to ejb 1-introduction to ejb Presentation Transcript

    • EJB Creating first Session Bean Session 1
    • Introduction to EJB 1. EJB provides component base Development 2. Provide write once deploy anywhere 3. Allows customization via Deployment Descriptor (no Java code modification) 4. Development focuses on Business Logic 5. EJB Server provides 1. Transaction Mgmt 2. Security 3. Concurrency 4. Resource Mgmt 5. Messaging 6. Depoly time Customization
    • Types of Bean 1. Entity Bean • Represent a thing in persistence store e.g. row on database or Serialized object in file. 2. Message Driven Bean • Can listen a message from JMS Messaging service 3. Session Bean • Represent a process or can contain a business logic • Stateful Session Bean • Can remember conversational state between method call • Stateless Session Bean • Can’t remember conversational state between method call
    • Rules for Building a Sample Bean To create sample bean we need to follow below steps • Code the bean class with all business method • Code to interfaces for bean (home & component) • Create XML Deployment Descriptor, must be name it as ejb-jar.xml • Deploy bean in application server
    • Bean Class HelloBean.java package com; import javax.ejb.*; public class HelloBean implements SessionBean{ public void ejbActivate(){} public void ejbPassivate(){} public void ejbRemove(){} public void setSessionContext(SessionContext ctx){} public String helloWorld(){ return "Hello there!!!"; } public void ejbCreate() {} }
    • Little (More) about Bean Class HelloBean.java If you are completely new to EJB, then you definitely says what the hec is that… here is your answers 1. Your bean class (which contains the actual definition of mth or says business logic) must implements SessionBean interface according to spec 2. According to java rule you have to implements all unimplemented mth from interface 3. The helloWorld() is your actual business mth, which will be called by client 4. The ejbCreate() not come from SessionBean interface, but we need to give the empty definition of it (???WTF??? this is what is going is your mind right now, b relax the answer is coming in next few slides.
    • Component Interface Hello.java package com; import javax.ejb.*; import java.rmi.*; public interface Hello extends EJBObject{ public String helloWorld() throws RemoteException; }
    • Little (More) about Component Interface 1. You definitely find some similarities between Component Interface & Bean class, if you are really thinking what I m thinking, then you are right. 2. The component interface contains declarations of all business mth which is to be defined by Bean class. 3. But according to java rules to implement the mth from interface, the class must have the these magical statements “implements Interface_Name”, which is in our case is missing. Then how these two are related. Forget this for now, I will explain this later. 4. Component interface must extends EJBObject interface 5. Its all mth must throws RemoteException 6. They can also thorws custom application (Checked) Exception. 7. Its return type must be primitives/Serializable Objs/Arrays/Collection of primitives or Serializable Objs/Remote obj (These are called as RMI-IIOP Complients).
    • Home Interface HelloHome.java package com; import javax.ejb.*; import java.rmi.*; public interface HelloHome extends EJBHome{ public Hello create() throws CreateException,RemoteException; }
    • Little (More) about Component Interface • Home interface must extends EJBHome interface • Must declare the create() (here is hint for your Bean class ejbCreate() mth) • Return type of create() must be component interface type • Mth must throws CreateException & RemoteException. • They can also thorws custom application (Checked) Exception.
    • Combining the all stuff together • Now what we have here : Component, Home & bean. we know all these stuff work together, but question is how? Here is ur ans. • We need to tell the container that all these things are work together by putting their entries in ejb-jar.xml • WTH is ejb-jar.xml? : It is Deployment Descriptor (DD) like web.xml in servlets. • It tells the container that which is home, component & bean to run the business logic. • It also states what the Bean type i.e. Session(stateless, stateful), Entity or Message-Driven (You can understand all this from syntax). • We are creating only sample stateless session bean, so only session tags are avail. For Entity & Message-Driver will see later.
    • Sample ejb-jar.xml <?xml version="1.0" encoding="UTF-8"?> <ejb-jar> <display-name>Hello EJB App</display-name> <enterprise-beans> <session> <display-name>Hello</display-name> <ejb-name>Hello</ejb-name> <home>com.HelloHome</home> <remote>com.Hello</remote> <ejb-class>com.HelloBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar>
    • Deploying & Calling the Bean mth • Now you have to create the jar of all compiled class with below structure. ejbApp.jar com Hello.class HelloHome.class HelloBean.class META-INF ejb-jar.xmlejb-jar.xml glassfish-ejb-jar.xml
    • Deploying & Calling the Bean mth • You noticed there is one more xml file glassfih-ejb-jar.xml, now lets see what is it. • As per my understanding, every server has its own ejb- jar.xml file for deployment of ejb application. For e.g. if you are deploying in weblogic server then you have additional weblogic-ejb-jar.xml • As I m using glassfish server for app deployment so I have glassfish-ejb-jar.xml • We will see the deployments of ejb & web application (part) in later. • Now I am writing the plane jsp to call the Bean mth. • You need to deploy this jsp(war) file in glassfish & run the jsp.
    • Client HelloClient.jsp <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@page import="javax.naming.InitialContext"%> <%@page import="javax.rmi.PortableRemoteObject"%> <%@page import="com.HelloHome"%> <%@page import="com.Hello"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% try{ InitialContext ic = new InitialContext(); Object obj = ic.lookup("Hello"); HelloHome home = (HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class); Hello hello = home.create(); String res = hello.helloWorld(); System.out.println(res); out.println(res); }catch(Exception e){ e.printStackTrace(); } %> </body> </html>
    • Thank you • I am not expert in EJB, but if you have any doubts in tutorial please feel free to contact me on ashishkirpan@gmail.com • Sorry for spelling & grammar mistakes :) Have a fun with EJB