Skip to content

1. Session Beans

JOHNYBASHA SHAIK edited this page Sep 6, 2018 · 10 revisions

EJB3x

Session Bean Types
  • Stateless - bean instance per request
  • Stateful - bean instance per client
  • Singleton - bean instance per application

JNDI Convension : java:[/app]/module/bean[!]

     Scope : 	
         global, 
         app, 
         module, 
         comp

@Remote – Remote business interface
@Local – Local business interface
@LocalBean – No interface view
@EJB – Injects reference of session bean.
@PostConstruct – Invokes the method immediately after the bean creation.
@PreDestroy – Invokes the method immediately before the container destroys the bean.
@Startup – Eagerly initializes singleton bean instance
@DependsOn – States that the current bean is dependent on some other bean

@Resource - used to acquire logical resources specific to the component type.
Eg:

@Resource
private SessionContext context;

SessionSynchronization: Stateful or stateless session bean can optionally implement SessionSynchronization interface

*SessionContext – Gives programatic access to runtime context provided by session bean instance

@Asynchronous – Marks a session bean method as an asynchronous method. Annotated at class level, marks all the methods asynchronous. These methods must have return types either void or Future.v- is the result type.

Deployment descriptor (ejb-jar.xml) overrides the annotations during deployment, if one exist. This is to minimize code touch for property value changes.

Timer
Timers can be created in two ways.

  • Declarative way: @Schedule, @Schedules or deployment descriptor
             Eg: This creates two timers one for every two hours and another every wed 2pm
                        @Schedules({
                                   @Schedule(hour="2"),
                                   @Schedule(hour="14", dayOfWeek="Wed")
                                  })
  • Programatic way:
    TimerService interface with ScheduleExpression, to create the timer and @Timeout on callback method, to invoke the timer
             Eg:
                        @Resource
                        TimerService timerService
                        timerService.createCalenderTimer(new scheduleExpression(), ….)
                        @Timeout
                        public void sendNotification(Timer timer){...}

@EJB or @Inject

As a thumb rule, use @Inject for local and no-interface session beans and @EJB to inject remote session beans.
For better understanding
Check this blog:inject vs ejb
and page 534 from Beginning Java EE 7
and page 260 from Java EE 7 Developer Handbook

@ApplicationScoped vs @Singleton

  • ApplicationScoped belongs to CDI. package javax.enterprise.context
  • Singleton belongs to EJB. Package javax.inject
  • CDI doesn't offer concurrency management by default. So @ApplicationScoped simply states the cardinality of the injected object. That means it instructs the injection engine to create only one instance of the injected bean and use it across all the application. But it does not enforce any concurrency constraint. Where as in case of singleton, in addition to instruct the application server to create only one instance of the injected bean and use it across all the application, it also instructs to manage concurrency. This transforms a regular bean to an EJB and by default enforces @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER) and @Lock(LockType.WRITE).
    Please do remember @ConcurrencyManagement and @Lock are applicable only to Singleton beans.
Clone this wiki locally