openJMS 0.7.7 和 Tomcat 4.x的配置

openJMS 0.7.7 和 Tomcat 4.x的配置

                                      

 

OpenJMS + Tomcat

 

 

【说明】版本变化很大,操作时候请注意版本。

一、准备

1,  http://openjms.sourceforge.net/downloads.html

当前最新版本为0.7.7-alpha

支持JMS API 1.1规范。

2,  Tomcat 4.x下载

 

 

二、Tomcat工程

1,  建立Tomcat工程 :建立src/static/WEB-INF目录

2,  openjms lib目录下的所有包都拷贝到Tomcat工程的WEB-INF/lib目录下(注意:不是都需要,如果要知道具体,请参考:http://openjms.sourceforge.net/usersguide/jars.html

openjms-0.7.7-alpha-1.jar加到CLASSPATH中。

3,  建立SimplePublisher.html 存放在staitc目录下

<html>

  <title>

    Simple Publisher

  </title>

 

  <body>

    <form method="get" action="/openjms/SimplePublisher">

      <table cols="2" cellspacing="10">

        <tr>

          <td>destination</td>

          <td><input type="text" name="destination" size="80"></td>

        </tr>           

     

        <tr>

          <td>text</td>

          <td><input type="test" name="text" size="30"></td>

        </tr>

     

        <tr>

          <td>count</td>

          <td><input type="text" name="count" size="5"></td>

        </tr>

      </table>

      <input type="submit">

    </form>

  </body>

</html>

4,  建立SimplePublisher.java,存放在src目录下

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Hashtable;

 

 

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.TextMessage;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

 

public class SimplePublisher extends HttpServlet {

  public void doGet(HttpServletRequest request,

    HttpServletResponse response)

    throws IOException, ServletException {

    String destination = request.getParameter("destination");

    String text = request.getParameter("text");

    String count_str = request.getParameter("count");

    int count = 0;

    Destination dest =null;

    Connection connection = null;

    try {

      count = Integer.parseInt(count_str);

    } catch (Exception exception) {

      throw new ServletException("Error in converting count " + exception);

    }

 

 

    try {

      String factory_name = "ConnectionFactory";

      Hashtable properties = new Hashtable();

      properties.put(Context.INITIAL_CONTEXT_FACTORY,

                     "org.exolab.jms.jndi.InitialContextFactory");

      properties.put(Context.PROVIDER_URL, "tcp://localhost:3035/");

      Context context = new InitialContext(properties);

      ConnectionFactory factory = (ConnectionFactory)

      context.lookup(factory_name);

      System.out.println("factory created");

      // Look up the Destination

      try {

             dest = (Destination) context.lookup(destination);

             System.out.println("destination created");

      } catch (NamingException exception) {

          System.err.println("Failed to look up destination: " + exception);

      }

      try {

          // create the connection

          connection = factory.createConnection();

          // create the session

          Session session = connection.createSession(

              false, Session.AUTO_ACKNOWLEDGE);

          // create the sender

          MessageProducer sender = session.createProducer(dest);

          // start the connection, to enable message sends

          connection.start();

          for (int i = 0; i < count; ++i) {

              TextMessage message = session.createTextMessage();

              message.setText(text + (i + 1));

              sender.send(message);

          }

      } catch (JMSException exception) {

          System.err.println("JMS exception: " + exception);

      } finally {

          if (connection != null) {

              try {

                  connection.close();

              } catch (JMSException exception) {

                  System.err.println("Failed to close connection: "

                                     + exception);

              }

          }

      }

    } catch (Exception exception) {

      exception.printStackTrace();

      throw new ServletException("Exception in processign request " + exception);

    }

    response.setContentType("text/html");

    PrintWriter out = response.getWriter();

    out.println("<html>");

    out.println("<head>");

    out.println("<title>OpenJMS Response</title>");

    out.println("</head>");

    out.println("<body>");

    out.println("Published " + count + " messages   " + text + "on destination "+ destination);

    out.println("</body>");

    out.println("</html>");

  }

}

5,  配置web.xml文件,存放在…..我不说。

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app

  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<display-name>A Simple Publisher Application</display-name>

<context-param>

  <param-name>Webmaster</param-name>

  <param-value>jima@comware.com.au</param-value>

</context-param>

<servlet>

  <servlet-name>SimplePublisher</servlet-name>

  <servlet-class>SimplePublisher</servlet-class>

</servlet>

<servlet>

  <servlet-name>SimpleReceiver</servlet-name>

  <servlet-class>SimpleReceiver</servlet-class>

</servlet>

<servlet-mapping>

  <servlet-name>SimpleReceiver</servlet-name>

  <url-pattern>/SimpleReceiver</url-pattern>

</servlet-mapping>

<servlet-mapping>

  <servlet-name>SimplePublisher</servlet-name>

  <url-pattern>/SimplePublisher</url-pattern>

</servlet-mapping></web-app>

6,配置server.xml,增加一个openjms上下文。

三、运行

1,  启动OPENJMS服务

$OPENJMS_HOME/bin/startup.bat,出现

  

 

2,  启动Tomcat

http://localhost:8080/openjms/static/SimplePublisher.html

出现下面界面:

3,  启动OPENJMS监控台

$OPENJMS_HOME/bin/admin.bat,注意查看有没有相关记录

  

 

四、消息接收

1,  增加SimpleReceiver.html

<html>

  <title>

    Simple Publisher

  </title>

 

  <body>

    <form method="get" action="/openjms/SimpleReceiver">

      <table cols="2" cellspacing="10">

        <tr>

          <td>destination</td>

          <td><input type="text" name="destination" size="80"></td>

        </tr>           

     

    

        <tr>

          <td>count</td>

          <td><input type="text" name="count" size="5"></td>

        </tr>

      </table>

      <input type="submit">

    </form>

  </body>

</html>

2,  增加SimpleReceiver.java

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Hashtable;

 

 

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageConsumer;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.TextMessage;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

 

public class SimpleReceiver extends HttpServlet {

  public void doGet(HttpServletRequest request,

    HttpServletResponse response)

    throws IOException, ServletException {

    String destination = request.getParameter("destination");

//    String text = request.getParameter("text");

    String count_str = request.getParameter("count");

    int count = 0;

    Destination dest =null;

    Connection connection = null;

    MessageConsumer receiver = null;

    try {

        count = Integer.parseInt(count_str);

      } catch (Exception exception) {

        throw new ServletException("Error in converting count " + exception);

      }  

 

 

    try {

      String factory_name = "ConnectionFactory";

      Hashtable properties = new Hashtable();

      properties.put(Context.INITIAL_CONTEXT_FACTORY,

                     "org.exolab.jms.jndi.InitialContextFactory");

      properties.put(Context.PROVIDER_URL, "tcp://localhost:3035/");

      Context context = new InitialContext(properties);

      ConnectionFactory factory = (ConnectionFactory)

      context.lookup(factory_name);

      System.out.println("factory created");

      // Look up the Destination

      try {

             dest = (Destination) context.lookup(destination);

             System.out.println("destination created");

      } catch (NamingException exception) {

          System.err.println("Failed to look up destination: " + exception);

      }

      try {

          // create the connection

          connection = factory.createConnection();

          // create the session

          Session session = connection.createSession(

              false, Session.AUTO_ACKNOWLEDGE);

          // create the sender

          receiver = session.createConsumer(dest);

          // start the connection, to enable message sends

          connection.start();

          response.setContentType("text/html");

          PrintWriter out = response.getWriter();

          out.println("<html>");

          out.println("<head>");

          out.println("<title>OpenJMS Response</title>");

          out.println("</head>");

          out.println("<body>");         

          for (int i = 0; i < count; ++i) {

              Message message = receiver.receive();

              if (message instanceof TextMessage) {

                  TextMessage text = (TextMessage) message;

                  out.println("Received: " + text.getText());

              } else if (message != null) {

                  out.println("Received non text message");

              }

          }

//          out.println("Published " + count + " messages   " + text + "on destination "+ destination);

          out.println("</body>");

          out.println("</html>");

      } catch (JMSException exception) {

          System.err.println("JMS exception: " + exception);

      } finally {

          if (connection != null) {

              try {

                  connection.close();

              } catch (JMSException exception) {

                  System.err.println("Failed to close connection: "

                                     + exception);

              }

          }

      }

    } catch (Exception exception) {

      exception.printStackTrace();

      throw new ServletException("Exception in processign request " + exception);

    }

 

 

 

 

  }

}

3,  运行http://localhost:8080/openjms/static/SimpleReceiver.html

输入destinationcount,然后从控制台监控数据变化。

   

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值