SlideShare ist ein Scribd-Unternehmen logo
1 von 17
EJB3.1

S.M. Mahmudul Hasan shohan
      Software Engineer
     Therap Services LLC
What Is an Enterprise Bean
•   is a server-side component that encapsulates the business logic of an application.



• Beneits of Enterprise Beans
    –  EJB container provides system-level services to enterprise beans, the bean
      developer can concentrate on solving business problems.
    – the beans rather than the clients contain the application’s business logic, the
      client developer can focus on the presentation of the client.
    – , because enterprise beans are portable components, the application
      assembler can build new applications from existing beans.
When to Use Enterprise Beans
• The application must be scalable.
• Transactions must ensure data integrity.
• The application will have a variety of clients
Types of Enterprise Beans
• Session
  – Performs a task for a client; optionally may
    implement a web service
  – Stateless, Statefull, Singleton
• Message-Driven
  – Acts as a listener for a particular messaging
    type, such as the Java Message Service API
What Is a Session Bean?

• A session bean encapsulates business logic that can be invoked
  programmatically by a client over local, remote, or web service client
  views.
• In a stateful session bean, the instance variables represent the state of a
  unique client/bean session.
•   A stateless session bean does not maintain a conversational state with the client.
    When a client invokes the methods of a stateless bean, the bean’s instance
    variables may contain a state specific to that client but only for the duration of the
    invocation.
•   A stateless session bean can implement a web service, but a stateful session bean
    cannot.
•   A singleton session bean is instantiated once per application and exists for the
    lifecycle of the application
When to Use Session Beans

• Stateful session beans
   – The bean’s state represents the interaction between the bean
     and a specific client.
   – To hold information about the client across method invocations.
• Stateless session bean
   – has no data for a specific client.
   – performs a generic task for all clients.
   – implements a web service.
• Singleton session
   –   state needs to be shared across the application.
   –   to be accessed by multiple threads concurrently.
   –   to perform tasks upon application startup and shutdown.
   –   implements a web service.
Stateful Session Beans
• the instance variables represent the state of a unique client/bean session
• the client interacts with its bean
• often called the conversational state.
Life Cycle
• @PostConstruct
   – to initialize any resources such as database connection, files etc
     required for the bean.
• @PreDestroy
   – to release or close any resources used by the bean.


• Rules for @PostConstruct and @PreDestroy
   –   method signature must return a void and take no arguments.
   –   method should not throw Checked Exception
   –   can have access modifiers
   –   may be FINAL
Lifecycle

•   Passivation and Activation of Stateful Session Bean
     –   The transfer from the working set to secondary storage is called instance passivation. In contrast called
         activation.

•   Conditions for Passivation
     –   When the instance is not in a transaction.
     –   with an extended persistence context
     –   All the entities in the persistence context are serializable.
     –   The EntityManager is serializable.

•   @PrePassivate
     –   Invoked before the stateful session bean is passivated.
     –   can be used to release or close any resources used by the bean.

•   @PostActivate
     –   after the stateful session bean is activated.
     –   to re-initialize any resources such as database connection, files etc which are closed before passivation.
•   Rules for @PostActivate and @PrePassivate
     –   signature must return a void and take no arguments
     –   can have access modifiers
     –   cannot be STATIC or FINAL
Lifecycle
• The Lifecycle of a Stateless Session Bean



• The Lifecycle of a Message-Driven Bean
Lifecycle
•   The Lifecycle of a Singleton Bean
     – Same as Stateless bean




•   Initializing Singleton Session Beans
        @Startup
        @Singleton
        public class StatusBean {
        private String status;
              @PostConstruct
              void init {
                          status = "Ready";
              }
              ….
    }
• Managing Concurrent Access in a Singleton
  Session Bean
  – container-managed concurrency and bean-managed
    concurrency.

  @Lock(LockType.READ)
  @Lock(LockType.WRITE)
  @AccessTimeout
• Bean-Managed Concurrency
  – is responsible for ensuring that the state of the
    singleton is synchronized across all clients
Web Service
• annotated either the javax.jws.WebService or
  the javax.jws.WebServiceProvider
• must be annotated with javax.jws.WebMethod
• exposed to web service clients must have
  JAXB-compatible parameters and return type
• must have a default public constructor.
• must be annotated @Stateless.
Timer Service

• Creating Programmatic Timers
    – @Schedule or @Schedules

@Schedule(dayOfWeek="Sun", hour="0")
public void cleanupWeekData() { ... }
@Schedules ({
          @Schedule(dayOfMonth="Last"),
          @Schedule(dayOfWeek="Fri", hour="23")
})
• Canceling and Saving Timers
    – Timers can be cancelled by the following events.
    – When a single-event timer expires, the EJB container calls the associated timeout
      method and then cancels the timer.
    – When the bean invokes the cancel method of the Timer interface, the container
      cancels the timer.
Timers
• Canceling and Saving Timers
  – When a single-event timer expires, the EJB
    container calls the associated timeout method
    and then cancels the timer.
  – When the bean invokes the cancel method of the
    Timer interface, the container cancels the timer.
Message-Driven bean

• An application client that sends several messages to a queue.
• A message-driven bean that asynchronously receives and processes
  the messages that are sent to the queue




• The client starts by injecting the connection factory and queue
  resources:
• the client creates the connection, session, and message producer:
MDB
•   The Message-Driven Bean Class
     –   must be annotated with the @MessageDriven
     –   must be defined as public
     –   cannot be defined as abstract or final.
     –   a public constructor with no arguments.
     –   must not define the finalize method.
     –   implement the message listener interface for the message type it supports.
     –   @MessageDriven annotation typically contains a mappedName element that specifies the
         JNDI name of the destination from which the bean will consume messages

•   onMessage Method
     –   onMessage method of the MessageListener interface
     –   The method must be declared as public.
     –   The method must not be declared as final or static.
     –   The return type must be void.
     –   The method must have a single argument of type javax.jms.Message.

Weitere ähnliche Inhalte

Was ist angesagt?

Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJBPeter R. Egli
 
Entity beans in java
Entity beans in javaEntity beans in java
Entity beans in javaAcp Jamod
 
Lecture 8 Enterprise Java Beans (EJB)
Lecture 8  Enterprise Java Beans (EJB)Lecture 8  Enterprise Java Beans (EJB)
Lecture 8 Enterprise Java Beans (EJB)Fahad Golra
 
Enterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business LogicEnterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business LogicEmprovise
 
Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Rohit Kelapure
 
EJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionEJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionKelum Senanayake
 
Session 4 Tp4
Session 4 Tp4Session 4 Tp4
Session 4 Tp4phanleson
 
JEE Course - EJB
JEE Course - EJBJEE Course - EJB
JEE Course - EJBodedns
 
EJB Interview Questions
EJB Interview QuestionsEJB Interview Questions
EJB Interview Questionsguest346cb1
 
Session 1 Tp1
Session 1 Tp1Session 1 Tp1
Session 1 Tp1phanleson
 
Free EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsFree EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsVirtual Nuggets
 

Was ist angesagt? (20)

Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
 
EJB 3.0 and J2EE
EJB 3.0 and J2EEEJB 3.0 and J2EE
EJB 3.0 and J2EE
 
Entity beans in java
Entity beans in javaEntity beans in java
Entity beans in java
 
Lecture 8 Enterprise Java Beans (EJB)
Lecture 8  Enterprise Java Beans (EJB)Lecture 8  Enterprise Java Beans (EJB)
Lecture 8 Enterprise Java Beans (EJB)
 
Enterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business LogicEnterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business Logic
 
Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010
 
Enterprise java beans
Enterprise java beansEnterprise java beans
Enterprise java beans
 
EJB .
EJB .EJB .
EJB .
 
Ejb notes
Ejb notesEjb notes
Ejb notes
 
EJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionEJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another Introduction
 
Session 4 Tp4
Session 4 Tp4Session 4 Tp4
Session 4 Tp4
 
Session bean
Session beanSession bean
Session bean
 
JEE Course - EJB
JEE Course - EJBJEE Course - EJB
JEE Course - EJB
 
Java EE EJB Applications
Java EE EJB ApplicationsJava EE EJB Applications
Java EE EJB Applications
 
Ch4 ejb
Ch4 ejbCh4 ejb
Ch4 ejb
 
EJB Interview Questions
EJB Interview QuestionsEJB Interview Questions
EJB Interview Questions
 
Session 1 Tp1
Session 1 Tp1Session 1 Tp1
Session 1 Tp1
 
Free EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsFree EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggets
 
EJB Clients
EJB ClientsEJB Clients
EJB Clients
 
Java beans
Java beansJava beans
Java beans
 

Ähnlich wie Ejb3.1 for the starter

Introduction to Long Running Workflows 3.7
Introduction to Long Running Workflows 3.7Introduction to Long Running Workflows 3.7
Introduction to Long Running Workflows 3.7StephenKardian
 
Spring introduction
Spring introductionSpring introduction
Spring introductionLê Hảo
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivitypkaviya
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologySimon Ritter
 
JAVA_BEAN.pptx
JAVA_BEAN.pptxJAVA_BEAN.pptx
JAVA_BEAN.pptxkoodalcsc
 
CDI Best Practices with Real-Life Examples - TUT3287
CDI Best Practices with Real-Life Examples - TUT3287CDI Best Practices with Real-Life Examples - TUT3287
CDI Best Practices with Real-Life Examples - TUT3287Ahmad Gohar
 
Coherence sig-nfr-web-tier-scaling-using-coherence-web
Coherence sig-nfr-web-tier-scaling-using-coherence-webCoherence sig-nfr-web-tier-scaling-using-coherence-web
Coherence sig-nfr-web-tier-scaling-using-coherence-webC2B2 Consulting
 
Web flowpresentation
Web flowpresentationWeb flowpresentation
Web flowpresentationRoman Brovko
 
InterConnect 2016 Java EE 7 Overview (PEJ-5296)
InterConnect 2016 Java EE 7 Overview (PEJ-5296)InterConnect 2016 Java EE 7 Overview (PEJ-5296)
InterConnect 2016 Java EE 7 Overview (PEJ-5296)Kevin Sutter
 
Effective out-of-container Integration Testing
Effective out-of-container Integration TestingEffective out-of-container Integration Testing
Effective out-of-container Integration TestingSam Brannen
 
Core web application development
Core web application developmentCore web application development
Core web application developmentBahaa Farouk
 
WebLogic Stability; Detect and Analyse Stuck Threads
WebLogic Stability; Detect and Analyse Stuck ThreadsWebLogic Stability; Detect and Analyse Stuck Threads
WebLogic Stability; Detect and Analyse Stuck ThreadsMaarten Smeets
 
Real world RESTful service development problems and solutions
Real world RESTful service development problems and solutionsReal world RESTful service development problems and solutions
Real world RESTful service development problems and solutionsMasoud Kalali
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptxssuser92282c
 
Spring Framework Presantation Part 1-Core
Spring Framework Presantation Part 1-CoreSpring Framework Presantation Part 1-Core
Spring Framework Presantation Part 1-CoreDonald Lika
 
session and cookies.ppt
session and cookies.pptsession and cookies.ppt
session and cookies.pptJayaprasanna4
 

Ähnlich wie Ejb3.1 for the starter (20)

EJB 3.2 part 1
EJB 3.2 part 1EJB 3.2 part 1
EJB 3.2 part 1
 
Introduction to Long Running Workflows 3.7
Introduction to Long Running Workflows 3.7Introduction to Long Running Workflows 3.7
Introduction to Long Running Workflows 3.7
 
Unit4wt
Unit4wtUnit4wt
Unit4wt
 
Unit4wt
Unit4wtUnit4wt
Unit4wt
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans Technology
 
JAVA_BEAN.pptx
JAVA_BEAN.pptxJAVA_BEAN.pptx
JAVA_BEAN.pptx
 
CDI Best Practices with Real-Life Examples - TUT3287
CDI Best Practices with Real-Life Examples - TUT3287CDI Best Practices with Real-Life Examples - TUT3287
CDI Best Practices with Real-Life Examples - TUT3287
 
Coherence sig-nfr-web-tier-scaling-using-coherence-web
Coherence sig-nfr-web-tier-scaling-using-coherence-webCoherence sig-nfr-web-tier-scaling-using-coherence-web
Coherence sig-nfr-web-tier-scaling-using-coherence-web
 
Web flowpresentation
Web flowpresentationWeb flowpresentation
Web flowpresentation
 
InterConnect 2016 Java EE 7 Overview (PEJ-5296)
InterConnect 2016 Java EE 7 Overview (PEJ-5296)InterConnect 2016 Java EE 7 Overview (PEJ-5296)
InterConnect 2016 Java EE 7 Overview (PEJ-5296)
 
Effective out-of-container Integration Testing
Effective out-of-container Integration TestingEffective out-of-container Integration Testing
Effective out-of-container Integration Testing
 
Core web application development
Core web application developmentCore web application development
Core web application development
 
WebLogic Stability; Detect and Analyse Stuck Threads
WebLogic Stability; Detect and Analyse Stuck ThreadsWebLogic Stability; Detect and Analyse Stuck Threads
WebLogic Stability; Detect and Analyse Stuck Threads
 
Real world RESTful service development problems and solutions
Real world RESTful service development problems and solutionsReal world RESTful service development problems and solutions
Real world RESTful service development problems and solutions
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptx
 
Spring Framework Presantation Part 1-Core
Spring Framework Presantation Part 1-CoreSpring Framework Presantation Part 1-Core
Spring Framework Presantation Part 1-Core
 
session and cookies.ppt
session and cookies.pptsession and cookies.ppt
session and cookies.ppt
 
Advanced java+JDBC+Servlet
Advanced java+JDBC+ServletAdvanced java+JDBC+Servlet
Advanced java+JDBC+Servlet
 

Ejb3.1 for the starter

  • 1. EJB3.1 S.M. Mahmudul Hasan shohan Software Engineer Therap Services LLC
  • 2. What Is an Enterprise Bean • is a server-side component that encapsulates the business logic of an application. • Beneits of Enterprise Beans – EJB container provides system-level services to enterprise beans, the bean developer can concentrate on solving business problems. – the beans rather than the clients contain the application’s business logic, the client developer can focus on the presentation of the client. – , because enterprise beans are portable components, the application assembler can build new applications from existing beans.
  • 3. When to Use Enterprise Beans • The application must be scalable. • Transactions must ensure data integrity. • The application will have a variety of clients
  • 4. Types of Enterprise Beans • Session – Performs a task for a client; optionally may implement a web service – Stateless, Statefull, Singleton • Message-Driven – Acts as a listener for a particular messaging type, such as the Java Message Service API
  • 5. What Is a Session Bean? • A session bean encapsulates business logic that can be invoked programmatically by a client over local, remote, or web service client views. • In a stateful session bean, the instance variables represent the state of a unique client/bean session. • A stateless session bean does not maintain a conversational state with the client. When a client invokes the methods of a stateless bean, the bean’s instance variables may contain a state specific to that client but only for the duration of the invocation. • A stateless session bean can implement a web service, but a stateful session bean cannot. • A singleton session bean is instantiated once per application and exists for the lifecycle of the application
  • 6. When to Use Session Beans • Stateful session beans – The bean’s state represents the interaction between the bean and a specific client. – To hold information about the client across method invocations. • Stateless session bean – has no data for a specific client. – performs a generic task for all clients. – implements a web service. • Singleton session – state needs to be shared across the application. – to be accessed by multiple threads concurrently. – to perform tasks upon application startup and shutdown. – implements a web service.
  • 7. Stateful Session Beans • the instance variables represent the state of a unique client/bean session • the client interacts with its bean • often called the conversational state.
  • 8. Life Cycle • @PostConstruct – to initialize any resources such as database connection, files etc required for the bean. • @PreDestroy – to release or close any resources used by the bean. • Rules for @PostConstruct and @PreDestroy – method signature must return a void and take no arguments. – method should not throw Checked Exception – can have access modifiers – may be FINAL
  • 9. Lifecycle • Passivation and Activation of Stateful Session Bean – The transfer from the working set to secondary storage is called instance passivation. In contrast called activation. • Conditions for Passivation – When the instance is not in a transaction. – with an extended persistence context – All the entities in the persistence context are serializable. – The EntityManager is serializable. • @PrePassivate – Invoked before the stateful session bean is passivated. – can be used to release or close any resources used by the bean. • @PostActivate – after the stateful session bean is activated. – to re-initialize any resources such as database connection, files etc which are closed before passivation. • Rules for @PostActivate and @PrePassivate – signature must return a void and take no arguments – can have access modifiers – cannot be STATIC or FINAL
  • 10. Lifecycle • The Lifecycle of a Stateless Session Bean • The Lifecycle of a Message-Driven Bean
  • 11. Lifecycle • The Lifecycle of a Singleton Bean – Same as Stateless bean • Initializing Singleton Session Beans @Startup @Singleton public class StatusBean { private String status; @PostConstruct void init { status = "Ready"; } …. }
  • 12. • Managing Concurrent Access in a Singleton Session Bean – container-managed concurrency and bean-managed concurrency. @Lock(LockType.READ) @Lock(LockType.WRITE) @AccessTimeout • Bean-Managed Concurrency – is responsible for ensuring that the state of the singleton is synchronized across all clients
  • 13. Web Service • annotated either the javax.jws.WebService or the javax.jws.WebServiceProvider • must be annotated with javax.jws.WebMethod • exposed to web service clients must have JAXB-compatible parameters and return type • must have a default public constructor. • must be annotated @Stateless.
  • 14. Timer Service • Creating Programmatic Timers – @Schedule or @Schedules @Schedule(dayOfWeek="Sun", hour="0") public void cleanupWeekData() { ... } @Schedules ({ @Schedule(dayOfMonth="Last"), @Schedule(dayOfWeek="Fri", hour="23") }) • Canceling and Saving Timers – Timers can be cancelled by the following events. – When a single-event timer expires, the EJB container calls the associated timeout method and then cancels the timer. – When the bean invokes the cancel method of the Timer interface, the container cancels the timer.
  • 15. Timers • Canceling and Saving Timers – When a single-event timer expires, the EJB container calls the associated timeout method and then cancels the timer. – When the bean invokes the cancel method of the Timer interface, the container cancels the timer.
  • 16. Message-Driven bean • An application client that sends several messages to a queue. • A message-driven bean that asynchronously receives and processes the messages that are sent to the queue • The client starts by injecting the connection factory and queue resources: • the client creates the connection, session, and message producer:
  • 17. MDB • The Message-Driven Bean Class – must be annotated with the @MessageDriven – must be defined as public – cannot be defined as abstract or final. – a public constructor with no arguments. – must not define the finalize method. – implement the message listener interface for the message type it supports. – @MessageDriven annotation typically contains a mappedName element that specifies the JNDI name of the destination from which the bean will consume messages • onMessage Method – onMessage method of the MessageListener interface – The method must be declared as public. – The method must not be declared as final or static. – The return type must be void. – The method must have a single argument of type javax.jms.Message.