2015年9月30日星期三

EAP_034:EAP 6 功能演示 4:JMS

环境:JBoss  EAP 6.4

1. 概念与术语
(1)HornetQ:JBoss EAP 6 使用 HornetQ 作为 JMS Provider。
(2)HornetQ 使用 connectors 和 acceptors。其中,
connector 定义了 JMS Client 如何连接到 HornetQ server。
acceptor 定义了 HornetQ server 接受哪种类型的连接。
分别有两种类型的 connector 和 acceptor。
(1)invm: Intra Virtual Machine,client 和 server 在同一个 JVM 中。
(2)netty: client 和 server 在不同的 JVM 中。
注意,connector 和 acceptor 必须匹配使用,invm connector 只能连到 invm acceptor;netty connector 只能连接到 netty acceptor。
(3)Connection Factory:为 connector 定义了一系列的配置参数,总共有35个。
默认定义了两个 InVmConnectionFactory 和 RemoteConnectionFactory。
 (4)durable:A durable destination is one where a receiver can retrieve a message that was sent while the receiver was not active.
默认情况下, Queue是 durable的,Topic是非 durable 的。

2. 演示
(1)cp standalone-full.xml standalone-full-jms.xml
(2)vim standalone-full-jms.xml
   <jms-queue name="myQueue">
      <entry name="java:jboss/queue/myQueue"/>
      <durable>true</durable>
   </jms-queue>
   <jms-topic name="myTopic">
      <entry name="java:jboss/topic/myTopic"/>
   </jms-topic>
(3)./standalone.sh -Djboss.server.base.dir=/Users/maping/Redhat/eap/demo/myeap -c standalone-full-jms.xml -Djboss.socket.binding.port-offset=10000
(4)部署 JMSTest.war
(5)访问:http://localhost:18080/JMSTest/run

Connection Factory JNDI name:   java:/ConnectionFactory
Destination JNDI name: java:jboss/queue/myQueue 或 java:jboss/topic/myTopic

这里的Connection Factory JNDI name 不能使用 java:jboss/exported/jms/RemoteConnectionFactory,这个用于远程JMS调用,需要验证。
前缀java:jboss/exported/表明这个资源可以被客户端找到,即使不在EAP这个容器中。
 
3. JMSTestServlet.java

package com.jboss.jms;

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

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class JMSTestServlet
 */

public class JMSTestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JMSTestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Display the JMSTest HMTL form
        request.getRequestDispatcher("jms_form.html").include(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("

Results of JMSTest

");
        String factoryName = request.getParameter("factoryName");
        String  destName = request.getParameter("destName");
        Context context = null;      
        ConnectionFactory connectionFactory = null;
        Destination dest = null;
        out.println("Looking up Connection Factory: " + factoryName + "
");
        try {
            context = new InitialContext();
            connectionFactory = (ConnectionFactory) context.lookup(factoryName);
        } catch (Exception e) {
            out.println("Connection Factory lookup failed: " + e.getMessage() + "
");
            e.printStackTrace();
            return;
        }
        out.println("Successfully found Connection Factory: " + factoryName + "

");
        out.println("Looking up Destination: " + destName + "
");
        try {
            dest = (Destination) context.lookup(destName);
        } catch(Exception e) {
            out.println("Destination lookup failed: " + e.getMessage() + "
");
            e.printStackTrace();
            return;
        }
        out.println("Successfully found Destination: " + destName + "
");
        //If we get this far, both the factory and destination have been found successfully,
        //so let's send a few test messages
        int numOfTests = Integer.parseInt(request.getParameter("numOfTests"));
        if(numOfTests > 0) {
            out.println("Attempting to send " + numOfTests + " test messages to destination...
");
            try {
                Connection connection = connectionFactory.createConnection();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(dest);
                TextMessage message = session.createTextMessage();
                for(int i = 1; i <= numOfTests; i++) {
                    message.setText("This is test message " + i);
                    out.println("Sending test message: " + i + "
");
                    producer.send(message);
                }
            } catch(Exception e) {
                out.println("An exception occurred while sending test messages: " + e.getMessage() + "
");
            }
        }
    }

}

没有评论: