Using JMS resources in an application

The primary reason for us to configure JMS resources on the application server is for applications and services deployed on the server, to get access to these resources in a vendor-independent way. In this section, we will see how we can use the JMS resources in an application to send or receive messages. Applications can either use JMS resources that are deployed on the application server, or they can use JMS resources that are defined at the application level.

We have provided two sample applications that demonstrate the use of JMS resources in an application, namely, JMS Sample and JMS Sample ear. In both of these cases, the Java code and functionality will be the same. The only difference will be in the deployment descriptors. Both of these applications send messages to a topic and have the same codebase, but one of them defines the JMS resources used at the client scope, while the other uses server-scoped resources. The code is the same for both the modules. The application has a servlet named JMSServlet, which accesses the JMS resources. The sevlet code is shown below:

package com.packtpub.jms;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class JMSServlet extends HttpServlet {
@Resource(name = "jms/ChatConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(name = "jms/ChatTopic")
private Topic topic;
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
MessageProducer producer = null;
MessageConsumer consumer = null;
Connection connection = null;
Session session = null;
String message = req.getParameter("message");
try {
if (message != null) {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(topic);
consumer = session.createConsumer(topic);
final PrintWriter pw = res.getWriter();
consumer.setMessageListener( new MessageListener(){
public void onMessage(Message msg) {
try {
pw.write(((TextMessage)msg).getText());
pw.close();
} catch (JMSException e) {
e.printStackTrace(System.out);
}
}
});
TextMessage txtMessage = session.createTextMessage();
txtMessage.setText(message);
producer.send(txtMessage);
Thread.sleep(1000);
}
} catch (JMSException e) {
e.printStackTrace(System.out);
if (connection != null) {
try {
connection.close();
} catch (JMSException e1) {
e1.printStackTrace(System.out);
}
}
} catch (InterruptedException e) {
e.printStackTrace(System.out);
}
}
}

Notice the @Resource annotations. These annotations are used to inform the container that it needs to inject objects of type javax.jms.ConnectionFactory and javax.jms.Topic into the two variables that have been annotated. The name attribute of these annotations is used to map the annotated element to the actual resource. The actual resources are mapped to the names used above in the server-specific deployment plan. In the case of a standalone application, this is given in the geronimo-web.xml file:

<nam:resource-ref>
<nam:ref-name>jms/ChatConnectionFactory</nam:ref-name>
<nam:pattern>
<nam:groupId>console.jms</nam:groupId>
<nam:artifactId>Sample RA</nam:artifactId>
<nam:version>1.0</nam:version>
<nam:name>SampleConnectionFactory</nam:name>
</nam:pattern>
</nam:resource-ref>
<nam:resource-env-ref>
<nam:ref-name>jms/ChatTopic</nam:ref-name>
<message-destination-link>SampleTopic</message-destination-link>
</nam:resource-env-ref>

You can see here that the ref-name element maps to the name that we specify in the annotation, and the pattern element refers to the SampleConnectionFactory that we define in the JMS plan, which is shown in its code listing, above. The only difference is that if the resources are application-scoped is that, instead of specifying a dependency on the JMS resource adapter in our application plan, we will include the JMS resource adapter plan also in our application plan. This is shown in the previous code listing. If we are not using annotations for accessing the resources, then we will need to look them up from JNDI using the names that we configure in the ejb-jar.xml, resource-ref, and resource-env-ref elements.

The applications will be present in the 6941_04/samples directory in the code bundle provided with the book. They can be built using Maven 2. The command to build them is mvn clean install. If you want to import the source code into Eclipse, then you can run mvn eclipse:eclipse from the root directory of the project and then use the Import existing projects into the workspace feature of Eclipse to import them.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset