Glassfish初体验之五

JMS的各种资源都已经创建好了,下面将介绍如何发送一条消息到JMS的队列里。
有两种方式来写客户端代码,使用annotation或者不使用。
1、使用annotation
1)创建web工程。
      在eclipse里创建web工程很简单。”File“-”New“-”Web Project“,然后输入工程名:TestJMS2,然后”Finish“。
2)新建Servlet
   新建包名:com.test.jms,在此包下新建Servlet:TestJMSServlet。
package com.test.jms;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestJMSServlet extends HttpServlet
{

/**
*
*/
private static final long serialVersionUID = 1L;

@Resource(mappedName = "jms/myCF")
private ConnectionFactory queueCF;

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

/**
* Constructor of the object.
*/
public TestJMSServlet()
{
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy()
{
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
*          the request send by the client to the server
* @param response
*          the response send by the server to the client
* @throws ServletException
*           if an error occurred
* @throws IOException
*           if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request
*          the request send by the client to the server
* @param response
*          the response send by the server to the client
* @throws ServletException
*           if an error occurred
* @throws IOException
*           if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
Connection connection = queueCF.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer((Destination) queue);
TextMessage message = session.createTextMessage();
message.setText("This is message ");
producer.send(message);

QueueBrowser browser = session.createBrowser(queue);
Enumeration msgs = browser.getEnumeration();

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println("<br>");
out.println("TCF: " + queueCF);
out.println("<br />Topic: " + queue);

out.println(", using the POST method");
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("your messages sent are:<br>");
if (!msgs.hasMoreElements())
{
System.out.println("No messages in queue");
out.println("No messages in queue<br>");
} else
{
while (msgs.hasMoreElements())
{
Message tempMsg = (Message) msgs.nextElement();
out.println("Message: " + ((TextMessage)tempMsg).getText() + "<br>");
System.out.println("Message: " + tempMsg);
}
}

out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();

browser.close();
producer.close();
session.close();
connection.close();

} catch (JMSException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException
*           if an error occurs
*/
public void init() throws ServletException
{
// Put your code here
}

}

3)web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>TestJMSServlet</servlet-name>
    <servlet-class>com.test.jms.TestJMSServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>TestJMSServlet</servlet-name>
    <url-pattern>/servlet/TestJMSServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

这样一个测试程序就算完成了,
部署应用到Glassfish上,然后启动服务,
地址栏中输入:http://localhost:8080/TestJMS2/servlet/TestJMSServlet
得到如下信息:
This is class com.test.jms.TestJMSServlet
TCF: com.sun.messaging.jms.ra.DirectConnectionFactory@6711b0
Topic: Sun Java System MQ Destination getName(): myQueueDes Class: com.sun.messaging.Queue getVERSION(): 3.0 isReadonly(): false getProperties(): {imqDestinationName=myQueueDes, imqDestinationDescription=A Description for the Destination Object} , using the POST method


your messages sent are:
Message: This is message
Message: This is message
Message: This is message
Message: This is message

你可能感兴趣的:(eclipse,Web,servlet,jms,Glassfish)