openJMS 0.7.7 和 Tomcat 4.x的配置

转载 2007年10月10日 00:14:00

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,然后从控制台监控数据变化。

   

 

tomcat+opemjms+mysql配置

  • 2009年09月08日 21:20
  • 626KB
  • 下载

OpenJMS程序实现消息的发送和接收

1、消息发送者(Sender) package com.boonya.openjms; import java.util.Hashtable; import javax.jms.JMSExcept...
  • boonya
  • boonya
  • 2013-09-24 23:47:39
  • 1881

OpenJMS安装配置及其验证测试

Open JMS 安装测试 1、内容简述 开源的JMS服务器openJms,及怎样使用openJms来构建系统之间健全、高度可用的通讯,从而简化企业级应用的开发。openJms符合SUN的JMS ...
  • boonya
  • boonya
  • 2013-09-24 23:37:57
  • 1760

Tomcat 7.x的安装和配置

这里我以Win7为例 Tomcat 7.x可以到Apache官方站点下载压缩版,放到自己指定的位置上即可。下载地址 对于高版本的Tomcat,无需配置什么,只要你的机子上配置好了JAVA_HOM...
  • belinwu
  • belinwu
  • 2012-04-10 12:41:22
  • 2578

OpenJMS配置

1、安装OpenJMS   将下载的解压缩包解压即可,最好是英文目录。2、运行   startup.bat---启动   admin.bat----管理员界面3、测试   cd %OPENJMS_HO...
  • haonan9122
  • haonan9122
  • 2010-07-14 11:23:00
  • 422

JMS OPENJMS的实现例子

  • 2009年02月02日 16:05
  • 10KB
  • 下载

myeclipse中servers视图优化(删除自带tomcat derby)配制& 删除MyEclipse中的 tomcat 6.x [Custom]

删除MyEclipse中的 tomcat 6.x [Custom] - 夜少少 - 博客园 http://www.cnblogs.com/progress01/archive/2013/01/15/...
  • robert_lizhiqiang
  • robert_lizhiqiang
  • 2014-10-20 09:36:38
  • 2087

tomcat 4.X 與tomcat 5.x的幫助文檔

 在apache的主站上,可以下載到tomcat 5的doc文檔,卻下載不到tomcat 4的幫助文檔.找了半天,原來tomcat 4中,在安裝是,就會將幫助文檔安裝在本地.而tomcat 5中,將幫...
  • chian_xxp
  • chian_xxp
  • 2008-07-15 17:56:00
  • 263

修改java 不重启tomcat

  • 2012年03月06日 09:49
  • 2.88MB
  • 下载

OPENJMS操作消息传输示例

  • 2009年11月29日 21:21
  • 15KB
  • 下载
收藏助手
不良信息举报
您举报文章:openJMS 0.7.7 和 Tomcat 4.x的配置
举报原因:
原因补充:

(最多只允许输入30个字)