Package ejava.examples.txagent.ejb
Class AgentReservationSessionEJB
- java.lang.Object
-
- ejava.examples.txagent.ejb.AgentReservationSessionEJB
-
- All Implemented Interfaces:
AgentReservationSession,AgentReservationSessionLocal,AgentReservationSessionRemote,SessionSynchronization
public class AgentReservationSessionEJB extends Object implements AgentReservationSessionLocal, AgentReservationSessionRemote, SessionSynchronization
This class provides an example of a Stateful session bean that will try to coordinate its local transaction with the tranaction of a remote session bean. The actual configuration may not really proove anything since the two applications will probably be deployed to the same server. We would need to break them apart into separate servers and separate server implementations to show anything really significant. Note too that this session bean declares its transaction properties using class annotations. By default, this stateful session bean runs without a tranaction while caching reservations for the user. A transaction is required only for the commit() call, which also signals the removal of this bean instance. The bean also implements the javax.ejb.SessionSynchronization interface. This allows it to be called at the start and end of a transaction so that it has a chance to update its cached values appropriately. This bean, however, just prints some debug.
-
-
Field Summary
Fields Modifier and Type Field Description private AgentReservationSessionagentSessionprivate SessionContextctxprivate EntityManageremprivate static org.slf4j.Loggerlogprivate HotelReservationSessionRemotereservationSession
-
Constructor Summary
Constructors Constructor Description AgentReservationSessionEJB()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddReservation(Person person, Date startDate, Date endDate)voidafterBegin()This method is called at the beginning of a transaction because of the implements SessionSynchronization interface.voidafterCompletion(boolean status)This method is called just after the transaction has committed to tell us what happened.voidbeforeCompletion()This method is called just before the transaction has committed to give the bean a chance to complain.voidcancelBooking()voidclose()voidclosing()This method should be called when the bean is destroyed because of the @PreDestory annotation.Bookingcommit()This method implements the big-bang.voidcreateBooking()voidinit()This method is called immediately after dependency injection and before any business method because of the @PostConstruct annotation.
-
-
-
Field Detail
-
log
private static final org.slf4j.Logger log
-
ctx
private SessionContext ctx
-
em
private EntityManager em
-
reservationSession
private HotelReservationSessionRemote reservationSession
-
agentSession
private AgentReservationSession agentSession
-
-
Method Detail
-
init
@PostConstruct public void init()
This method is called immediately after dependency injection and before any business method because of the @PostConstruct annotation. It provides a chance to do any manual construction of the overall bean. In this case, we need to instantiate out business logic implementation, provide it a DAO, and register the EntityManager for the DAO(s) to use.
-
closing
@PreDestroy public void closing()
This method should be called when the bean is destroyed because of the @PreDestory annotation.
-
createBooking
public void createBooking() throws AgentReservationException- Specified by:
createBookingin interfaceAgentReservationSession- Throws:
AgentReservationException
-
addReservation
public void addReservation(Person person, Date startDate, Date endDate) throws AgentReservationException
- Specified by:
addReservationin interfaceAgentReservationSession- Throws:
AgentReservationException
-
cancelBooking
public void cancelBooking() throws AgentReservationException- Specified by:
cancelBookingin interfaceAgentReservationSession- Throws:
AgentReservationException
-
commit
public Booking commit() throws AgentReservationException
This method implements the big-bang. All information, up to this point is being cached in the business logic instance and remote hotel reservation session; all owned by this specific instance. This method causes a transaction to be started, the remote reservations to be commited by the hotel, and the booking to be stored locally with references to the reservations. If it all works; all should be stored. If something fails (e.g., an invalid reservation date), then nothing will be saved.- Specified by:
commitin interfaceAgentReservationSession- Throws:
AgentReservationException
-
close
public void close()
- Specified by:
closein interfaceAgentReservationSession
-
afterBegin
public void afterBegin()
This method is called at the beginning of a transaction because of the implements SessionSynchronization interface.- Specified by:
afterBeginin interfaceSessionSynchronization
-
beforeCompletion
public void beforeCompletion() throws EJBException, RemoteExceptionThis method is called just before the transaction has committed to give the bean a chance to complain.- Specified by:
beforeCompletionin interfaceSessionSynchronization- Throws:
EJBExceptionRemoteException
-
afterCompletion
public void afterCompletion(boolean status)
This method is called just after the transaction has committed to tell us what happened.- Specified by:
afterCompletionin interfaceSessionSynchronization
-
-