Search code examples
databasejakarta-eemessage-driven-bean

How to save to database data retrieved from a message driven bean?


I'm want to use the n-tier architecture for an application so the client tier, web tier, business tier and data tier is separate. I'm wondering how a message driven bean which has a message save it to the database without changing the architecture. (That is using a normal session bean i retrieved data entered through a JSP page to a servlet and from the servlet called the bean class which had operations to the database, it is not possible to do this with the message driven beans since it already has a overridden method onMessage)

So far i can retrieve the values from the servlet directly using the message bean but i want to change this to a 4-tier architecture where database operations are not in the servlet.

my servlet code

@Resource(mappedName = "jms/dest")
private Queue dest;
@Resource(mappedName = "jms/queue")
private ConnectionFactory queue;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    String str = request.getParameter("message");

    try {
        sendJMSMessageToDest(str);
    } catch (JMSException ex) {

    }


private Message createJMSMessageForjmsDest(Session session, Object messageData) throws JMSException{
    TextMessage tm = session.createTextMessage();
    tm.setText(messageData.toString());
    return tm;
}
private void sendJMSMessageToDest(Object messageData) throws JMSException{
    Connection connection = null;
    Session session = null;

    try {
        connection = queue.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer messageProducer = session.createProducer(dest);
        messageProducer.send(createJMSMessageForjmsDest(session,messageData));
    } catch (JMSException ex) {

    }
}

Solution

  • You must think in two possible workflows:

    1. Synchronous interaction.
    2. Asynchronous interaction.

    Below I draw a possible architecture wich covers boot workflows. The components are:

    • DAO: Data Access Object layer. This is responsible of persist and query database without business logic. Implemented with Stateless Session Beans
    • BL: Business Logic layer. This is responsible of process Business Logic, don't know where the data will be persisted or queried just invokes DAO layer. Also is independent of View layer (Web, Web Service, Rest, etc.).
    • Serlvet: In this case represents the view layer or web interaction with the user calls directly to BL for process de data retrieved.
    • MDB: This layer is for asynchronous events it dequeues messages from a Queue (or Topic) then call to BL layer for process the data retrieved.

    This architecture enables code reutilization and separation of responsibilities.

    Theres is the Diagram with two workflows.

    JEE Multi Layer Architecture example

    1. Syncrhonous workflow:
      1. Servlet call BL.
      2. BL call DAO.
      3. DAO inteacts with Database
    2. Asyncrhonous workflow:
      • i. Servlet enqueues message A,B,C
      • ii. MDB dequeue A
      • iii. MDB call BL.
      • iv. BL call DAO.
      • v. DAO interacts with Database