APACHE Geronimo 试用之JMS

APACHE Geronimo 试用之JMS

1、通过控制台的ActiveMQ创建如下JMS准备环境:
Type     Name     Deployed As     State     Actions
Connection Factory     jms/TestJMSFactory2     Server-wide     running     
Queue     jms/TestJMSQueue2     Server-wide     running
具体常见方法请参考:http://cwiki.apache.org/GMOxDOC21/web-application-for-jms-access.html

2、创建AdminServlet:
package  webjms;

import  java.io.IOException;
import  java.io.PrintWriter;

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.Queue;
import  javax.jms.Session;
import  javax.jms.TextMessage;
import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

/** */ /**
 * Servlet implementation class for Servlet: AdminServlet
 * 
 
*/

public   class  AdminServlet  extends  javax.servlet.http.HttpServlet  implements
        javax.servlet.Servlet 
{
    @Resource(name 
= "jms/TestJMSFactory2")
    
private ConnectionFactory connectionFactory;

    @Resource(name 
= "jms/TestJMSQueue2")
    
private Queue queue;

    
public AdminServlet() {
        
super();
    }


    
protected void doGet(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        PrintWriter out 
= response.getWriter();
        
if (connectionFactory == null{
            out.println(
"Connection Factory lookup has failed");
            
return;
        }

        
if (queue == null{
            out.println(
"Queue lookup has failed");
            
return;
        }

        Connection connection 
= null;
        
try {
            connection 
= connectionFactory.createConnection();
            Session session 
= connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            MessageConsumer consumer 
= session.createConsumer(queue);
            connection.start();
            out
                    .println(
"The following information has been received for updation\n");
            
while (true{
                Message m 
= consumer.receive();
                
if (m instanceof TextMessage) {
                    TextMessage message 
= (TextMessage) m;
                    out.println(message.getText());
                }
 else {
                    
break;
                }

            }

        }
 catch (JMSException e) {
            e.printStackTrace();
        }
 finally {
            
if (connection != null{
                
try {
                    connection.close();
                }
 catch (JMSException e1) {
                }

            }

        }

    }


    
protected void doPost(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        doGet(request, response);
    }

}

3、创建UserServlet:
package  webjms;

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

import  javax.annotation.Resource;
import  javax.jms.Connection;
import  javax.jms.ConnectionFactory;
import  javax.jms.JMSException;
import  javax.jms.MessageProducer;
import  javax.jms.Queue;
import  javax.jms.Session;
import  javax.jms.TextMessage;
import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

public   class  UserServlet  extends  javax.servlet.http.HttpServlet  implements
        javax.servlet.Servlet 
{
    @Resource(name 
= "jms/TestJMSFactory2")
    
private ConnectionFactory connectionFactory;
    @Resource(name 
= "jms/TestJMSQueue2")
    
private Queue queue;
    
static final long serialVersionUID = 1L;

    
public UserServlet() {
        
super();
    }


    
protected void doGet(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        PrintWriter out 
= response.getWriter();
        
if (connectionFactory == null{
            out.println(
"Connection Factory lookup has failed");
            
return;
        }


        
if (queue == null{
            out.println(
"Queue lookup has failed");
            
return;
        }


        Connection connection 
= null;
        
try {
            connection 
= connectionFactory.createConnection();
            connection.start();
            Session session 
= connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer 
= session.createProducer(queue);
            TextMessage message 
= session.createTextMessage();
            Enumeration arr 
= request.getParameterNames();
            
while (arr.hasMoreElements()) {
                String fields 
= (String) arr.nextElement();
                String paramname[] 
= request.getParameterValues(fields);
                
for (int i = 0; i < paramname.length; i++{
                    String s 
= null;
                    s 
= fields + ":" + paramname[i];
                    message.setText(s);
                    producer.send(message);
                }


            }

            out.println(
"Your request has been sent to administrator.");
            producer.send(session.createMessage());
        }
 catch (JMSException e) {
            e.printStackTrace();
        }
 finally {
            
if (connection != null{
                
try {
                    connection.close();
                }
 catch (JMSException e1) {
                }

            }

        }

    }


    
protected void doPost(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        doGet(request, response);
    }

}

4、创建index.jsp文件:
<% @ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding
="ISO-8859-1"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=ISO-8859-1" >
< title > Sample Web Application accessing JMS Resources </ title >
</ head >
< body >
< form  action ="/WebJMS2/UserServlet" >
< h1 > Please enter the updated information </ h1 >
< table >
    
< tr >
        
< td > UserName: </ td >
        
< td >< input  type ="text"  name ="username"  size ="30" ></ td >
    
</ tr >
    
< tr >
        
< td > UserID: </ td >
        
< td >< input  type ="text"  name ="userid"  size ="30" ></ td >
    
</ tr >
    
< tr >
        
< td > Old Address: </ td >
        
< td >< input  type ="text"  name ="oldaddress"  size ="30" ></ td >
    
</ tr >
    
< tr >
        
< td > New Address: </ td >
        
< td >< input  type ="text"  name ="newaddress"  size ="30" ></ td >
    
< tr >
</ table >
< input  type ="submit"  value ="Submit" ></ form >
</ body >
</ html >

5、geronimo-web.xml的配置:
<? xml version="1.0" encoding="UTF-8" ?>
< web-app  xmlns ="http://geronimo.apache.org/xml/ns/j2ee/web-1.2"  
         xmlns:nam
="http://geronimo.apache.org/xml/ns/naming-1.2"  
         xmlns:sec
="http://geronimo.apache.org/xml/ns/security-1.1"  
         xmlns:sys
="http://geronimo.apache.org/xml/ns/deployment-1.2" >
  
< sys:environment >
    
< sys:moduleId >
      
< sys:groupId > default </ sys:groupId >
      
< sys:artifactId > WebJMS2 </ sys:artifactId >
      
< sys:version > 1.0 </ sys:version >
      
< sys:type > car </ sys:type >
    
</ sys:moduleId >
    
< sys:dependencies >
            
< sys:dependency >
                
< sys:groupId > org.apache.geronimo.configs </ sys:groupId >
                
< sys:artifactId > activemq-ra </ sys:artifactId >
                
< sys:version > 2.1.4 </ sys:version >
                
< sys:type > car </ sys:type >
            
</ sys:dependency >
        
</ sys:dependencies >
  
</ sys:environment >
  
< context-root > /WebJMS2 </ context-root >
  
< nam:resource-ref >
        
< nam:ref-name > jms/TestJMSFactory2 </ nam:ref-name >
        
< nam:pattern >
            
< nam:groupId > org.apache.geronimo.configs </ nam:groupId >
            
< nam:artifactId > activemq-ra </ nam:artifactId >
            
< nam:version > 2.1.4 </ nam:version >
            
< nam:name > DefaultActiveMQConnectionFactory </ nam:name >
        
</ nam:pattern >
    
</ nam:resource-ref >
    
< nam:resource-env-ref >
        
< nam:ref-name > jms/TestJMSQueue2 </ nam:ref-name >
        
< nam:pattern >
            
< nam:groupId > org.apache.geronimo.configs </ nam:groupId >
            
< nam:artifactId > activemq-ra </ nam:artifactId >
            
< nam:version > 2.1.4 </ nam:version >
            
< nam:name > SendReceiveQueue </ nam:name >
        
</ nam:pattern >
    
</ nam:resource-env-ref >
</ web-app >

6、web.xml文件的配置:
<? xml version="1.0" encoding="UTF-8" ?>
< web-app  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
    xmlns
="http://java.sun.com/xml/ns/javaee"  
    xmlns:web
="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id
="WebApp_ID"  version ="2.5" >
    
< display-name > WebJMS </ display-name >
    
< welcome-file-list >
        
< welcome-file > index.jsp </ welcome-file >
    
</ welcome-file-list >
    
< servlet >
        
< description ></ description >
        
< display-name > AdminServlet </ display-name >
        
< servlet-name > AdminServlet </ servlet-name >
        
< servlet-class > webjms.AdminServlet </ servlet-class >
    
</ servlet >
    
< servlet >
        
< description ></ description >
        
< display-name > UserServlet </ display-name >
        
< servlet-name > UserServlet </ servlet-name >
        
< servlet-class > webjms.UserServlet </ servlet-class >
    
</ servlet >
    
< servlet-mapping >
        
< servlet-name > AdminServlet </ servlet-name >
        
< url-pattern > /AdminServlet </ url-pattern >
    
</ servlet-mapping >
    
< servlet-mapping >
        
< servlet-name > UserServlet </ servlet-name >
        
< url-pattern > /UserServlet </ url-pattern >
    
</ servlet-mapping >
</ web-app >

7、发布及查看运行结果:
将WebJMS.war发布即可,具体请 点击此处下载,运行结果的查看可通过下面的两个URL实现:
http://localhost:8080/WebJMS2/
http://localhost:8080/WebJMS2/AdminServlet

你可能感兴趣的:(APACHE Geronimo 试用之JMS)