J2EE Application Architecture With Messaging

Introduction

 
In non-JMS J2EE architecture, the client and the enterprise beans a function on the basis of the request-response paradigm. In most cases, a session bean acts as a request interceptor and passes on the requests to the other entity or session beans, which are designed to do the actual work. This architecture is referred to as the session façade.
 
Session Façade is shown in the figure below: The Session bean in the middle passes the requests to other beans and the responses to the client.
 
J2EE Application Architecture with Messaging
Figure 1: Non-JMS J2EE Architecture
 
The drawback of session façade architecture is that session bean and entity bean methods, work on synchronous mode, which effectively means that when the client calls the beans with a request, it waits for a response. This can be overcome by including JMS in this kind of architecture. In such a scenario, the JMS Server and a Message-Driven bean, together, they take the place of a session bean interceptor.
 
However, what they receive is not a request, but a message which they acknowledge. Internally, the Message-Driven bean still uses request-response with other enterprise beans. This architecture is referred to as a message façade. The main advantage of the message façade is that the client sends a message to the JMS server, but does not wait for any response. Figure 2 illustrates a message façade. The JMS server is instrumental in passing messages and acknowledgments between the clients and the Message-Driven bean.
 
J2EE Application Architecture with Messaging
Figure 2 : JMS J2EE Architecture
 

Message- Driven Bean that Sends and Receives Messages

 
Message-Driven bean can be designed to both send and receives messages to separate queues. You can develop this kind of a Message-Driven bean, by adding all the message handling code in the onMessage() method. In the code below, the onMessage() method is executed when the Message-Driven bean receives a message using receiveQueue. The received message is stored in a TextMessage object and the characters, FWD: are added to the message. A message producer using sendQueue is created and the method TextMessage object is sent across to it.
 
Source Code
  1. import javax.ejb.*;  
  2. import javax.jms.*;  
  3. import javax.naming.*;  
  4. public class DualMessageBean implements MessageDrivenBean,  
  5. MessageListener {  
  6.     public DualMessageBean() {}  
  7.     public void setMessageDrivenContext(MessageDrivenContext mdc) {}  
  8.     public void ejbCreate() {}  
  9.     public void onMessage(Message inMessage) {  
  10.         try {  
  11.             InitialContext ctx = new InitialContext();  
  12.             //Lookup the origin factory for receiving messages  
  13.             ConnectionFactory cf = (ConnectionFactory) ctx.lookup("java:comp/env/receiveQueue");  
  14.             Connection origin = cf.createConnection();  
  15.             Destination dest = (Destination) ctx.lookup("java:comp/env/sendQueue");  
  16.             //Create session for the message origin  
  17.             Session session = origin.createSession(true0);  
  18.             //Create producer for the message destination  
  19.             MessageProducer producer = session.createProducer(dest);  
  20.             //Append the text "FWD:" to the message received  
  21.             TextMessage msg = session.createTextMessage("FWD:" + ((TextMessage) inMessage).getText());  
  22.             //Send the modified message to the destination producer  
  23.             producer.send(msg);  
  24.             producer.close();  
  25.             session.close();  
  26.             origin.close();  
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31.     public void ejbRemove() {}  
  32. }  
The resource required by DualMessageBean is declared in the deployment descriptors.
 
Source code below illustrates the declaration of both sendQueue and receiveQueue along with the QueueConnectionFactory in ejb-jar.xml
  1. ----  
  2. <resource-ref>  
  3.     <res-ref-name>jms/QcfFinal</res-ref-name>  
  4.     <res-type>javax.jms.QueueConnectionFactory</res-type>  
  5.     <res-auth>Container</res-auth>  
  6. </resource-ref>  
  7. <resource-env-ref>  
  8.     <resource-env-ref-name>jms/receiveQueue</resource-env-ref-name>  
  9.     <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>  
  10. </resource-env-ref>  
  11. <resource-env-ref>  
  12.     <resource-env-ref-name>jms/sendQueue</resource-env-ref-name>  
  13.     <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>  
  14. </resource-env-ref>  
Similarly, the JNDI names of the resources are also added in the vendor-specific deployment descriptors.
 

Summary

 
In session facade, the client and the enterprise beans function on the basis of the request-response paradigm. In message facade, J2EE architecture makes use of a Message-Driven bean for fulfilling the requests of the client. A Message-Driven bean can be designed to both send and receives messages.


Similar Articles