Class AgentReservationSessionEJB

  • All Implemented Interfaces:
    ejava.examples.txagent.bl.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 Detail

      • log

        private static final org.slf4j.Logger log
      • reservationSession

        private ejava.examples.txhotel.ejb.HotelReservationSessionRemote reservationSession
      • agentSession

        private ejava.examples.txagent.bl.AgentReservationSession agentSession
    • Constructor Detail

      • AgentReservationSessionEJB

        public AgentReservationSessionEJB()
    • 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 ejava.examples.txagent.bl.AgentReservationException
        Specified by:
        createBooking in interface ejava.examples.txagent.bl.AgentReservationSession
        Throws:
        ejava.examples.txagent.bl.AgentReservationException
      • addReservation

        public void addReservation​(ejava.examples.txhotel.bo.Person person,
                                   Date startDate,
                                   Date endDate)
                            throws ejava.examples.txagent.bl.AgentReservationException
        Specified by:
        addReservation in interface ejava.examples.txagent.bl.AgentReservationSession
        Throws:
        ejava.examples.txagent.bl.AgentReservationException
      • cancelBooking

        public void cancelBooking()
                           throws ejava.examples.txagent.bl.AgentReservationException
        Specified by:
        cancelBooking in interface ejava.examples.txagent.bl.AgentReservationSession
        Throws:
        ejava.examples.txagent.bl.AgentReservationException
      • commit

        public ejava.examples.txagent.bo.Booking commit()
                                                 throws ejava.examples.txagent.bl.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:
        commit in interface ejava.examples.txagent.bl.AgentReservationSession
        Throws:
        ejava.examples.txagent.bl.AgentReservationException
      • close

        public void close()
        Specified by:
        close in interface ejava.examples.txagent.bl.AgentReservationSession
      • afterBegin

        public void afterBegin()
        This method is called at the beginning of a transaction because of the implements SessionSynchronization interface.
        Specified by:
        afterBegin in interface SessionSynchronization
      • afterCompletion

        public void afterCompletion​(boolean status)
        This method is called just after the transaction has committed to tell us what happened.
        Specified by:
        afterCompletion in interface SessionSynchronization