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.
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.
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
- import javax.ejb.*;
- import javax.jms.*;
- import javax.naming.*;
- public class DualMessageBean implements MessageDrivenBean,
- MessageListener {
- public DualMessageBean() {}
- public void setMessageDrivenContext(MessageDrivenContext mdc) {}
- public void ejbCreate() {}
- public void onMessage(Message inMessage) {
- try {
- InitialContext ctx = new InitialContext();
-
- ConnectionFactory cf = (ConnectionFactory) ctx.lookup("java:comp/env/receiveQueue");
- Connection origin = cf.createConnection();
- Destination dest = (Destination) ctx.lookup("java:comp/env/sendQueue");
-
- Session session = origin.createSession(true, 0);
-
- MessageProducer producer = session.createProducer(dest);
-
- TextMessage msg = session.createTextMessage("FWD:" + ((TextMessage) inMessage).getText());
-
- producer.send(msg);
- producer.close();
- session.close();
- origin.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void ejbRemove() {}
- }
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
- ----
- <resource-ref>
- <res-ref-name>jms/QcfFinal</res-ref-name>
- <res-type>javax.jms.QueueConnectionFactory</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
- <resource-env-ref>
- <resource-env-ref-name>jms/receiveQueue</resource-env-ref-name>
- <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
- </resource-env-ref>
- <resource-env-ref>
- <resource-env-ref-name>jms/sendQueue</resource-env-ref-name>
- <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
- </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.