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

   

 

搭建Spring4.x.x开发环境

先去Spring官网下载Spring4.x.x开发包(本人使用的版本是Spring4.2.5),这个网上有一大堆博客有介绍,故省略。下载下来之后,我就来详细说明如何在Eclipse中搭建Spring4...
  • yerenyuan_pku
  • yerenyuan_pku
  • 2016年10月16日 17:00
  • 2026

OpenJMS安装配置及其验证测试

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

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

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

Spring事务之四(Spring声明式事务基本配置)

更多文章:http://zhuqiuhui.space/ 一、基于XML的事务配置 ...
  • zhuqiuhui
  • zhuqiuhui
  • 2017年04月21日 00:35
  • 200

Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解(4) - Scheduled注解实现任务调度

Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解(4) - Scheduled注解实现任务调度 (一)基本介绍 (二)基础框架...
  • chwshuang
  • chwshuang
  • 2016年08月15日 15:45
  • 5673

【solr专题之四】在Tomcat 中部署Solr4.x

1、安装Tomcat (1)下载并解压至/opt/tomcat中 [root@jediael44 solr]# cd /opt [root@jediael44 opt]# tar -zxvf apa...
  • jediael_lu
  • jediael_lu
  • 2014年07月17日 16:08
  • 3179

Tomcat安装与环境变量的配置

Tomacat的下载  去Tomcat官网下载,我使用的是apache-tomcat-7.0.78的版本。 安装  下载完成之后,我们解压缩到相应的目录。这里我解压缩到d盘下面    ...
  • Edison_03
  • Edison_03
  • 2017年06月25日 09:30
  • 28111

Netty 4.x 实战详解

Netty 4.x 实战详解目前Netty的最新稳定版本是 Netty 4.1.9, 本文根据该版本进行开发介绍。目录 Netty 4x 实战详解 目录 开发前准备工作 代码实现 一 服务器 1服务器...
  • lms1719
  • lms1719
  • 2017年04月21日 18:03
  • 604

JavaWeb开发 Tomcat安装及配置教程

步骤:1、Java环境的配置。2、安装Tomcat。3、配置Tomcat。4、Eclipse下配置Tomcat。...
  • dante_feng
  • dante_feng
  • 2016年03月06日 08:25
  • 3875

myeclipse10配置tomcat详细过程

首先确保你已经成功的安装 了myeclipse10和tomcat(我用的是tomcat6) 1:在myeclipse10中添加tomcat: 选择属性preferences之后进入配置框: ...
  • vspeter
  • vspeter
  • 2013年03月15日 01:10
  • 21595
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:openJMS 0.7.7 和 Tomcat 4.x的配置
举报原因:
原因补充:

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