【问题标题】:Concurrent Processing of JMS Message Listener WeblogicJMS消息监听器Weblogic的并发处理
【发布时间】:2013-12-16 10:19:10
【问题描述】:

我在 JMS 上运行一个测试用例,发现处理是连续的。当我向使用 JMS 发送消息的 servlet 发出 200 个请求时,receiver(messageListner) 正在按顺序接收请求。如何接收并发请求?我们有什么要设置的参数吗?我阅读了 JMS 教程和 API,它们在同一个会话中按顺序传递消息,即使我为每个发送请求和接收端的 10 个会话创建一个新会话,处理仍然是连续的。

public class ProducerServlet extends javax.servlet.http.HttpServlet implements
    javax.servlet.Servlet {

// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

// Defines the JMS context factory.
public final static String JMS_FACTORY = "jms/TestConnectionFactory";

// Defines the queue.
public final static String QUEUE = "jms/TestJMSQueue";

public final static String TOPIC = "jms/TestTopic";

TestJMSListener jms = new TestJMSListener();
ConnectionFactory connectionFactory = null;
Queue dest1 = null;
Topic dest =null;
Connection connection = null;
MessageProducer producer = null;

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        try {
            connection = connectionFactory.createConnection();              

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(dest1);
        TextMessage message = session.createTextMessage();

        message.setText("This is message from JMSSECOND DEMO "
                + request.getParameter("Num"));
        System.out.println("Sending message: " + message.getText());
        producer.send(message);
        producer.send(session.createMessage());
    } catch (Exception e) {
        System.out.println("Exception occurred: " + e.toString());
    }

}

@Override
public void init(ServletConfig arg0) throws ServletException {      
    Context jndiContext = null;
    try {

        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
        env.put(Context.PROVIDER_URL, "http://localhost:7001");
        jndiContext = new InitialContext(env);
    } catch (NamingException e) {
        System.out.println("Could not create JNDI API context: "
                + e.toString());            
    }

    try {
        connectionFactory = (ConnectionFactory) jndiContext
                .lookup(JMS_FACTORY);
        dest1 = (Queue) jndiContext.lookup(QUEUE);
    } catch (Exception e) {
        System.out.println("JNDI API lookup failed: " + e.toString());
        e.printStackTrace();            
    }

}

}

在收到消息后我要睡觉(做一秒钟的事情)的监听器实现。

public class TestJMSListener implements MessageListener {

// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

// Defines the JMS context factory.
public final static String JMS_FACTORY = "jms/TestConnectionFactory";

// Defines the queue.
public final static String QUEUE = "jms/TestJMSQueue";

public final static String TOPIC = "jms/TestTopic";

public TestJMSListener() {

    System.out.println("********* Consumer check **********");

    Context jndiContext = null;
    ConnectionFactory connectionFactory = null;
    Connection connection[] = null;
    Session session[] = null;
    Queue dest1 = null;
    Topic dest = null;
    MessageConsumer consumer[] = null;
    // TextMessage message = null;

    try {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
        env.put(Context.PROVIDER_URL, "http://localhost:7001");
        jndiContext = new InitialContext(env);
    } catch (NamingException e) {
        System.out.println("Could not create JNDI API context: "
                + e.toString());
        System.exit(1);
    }

    try {
        connectionFactory = (ConnectionFactory) jndiContext
                .lookup(JMS_FACTORY);
        dest1 = (Queue) jndiContext.lookup(QUEUE);
    } catch (Exception e) {
        System.out.println("JNDI API lookup failed: " + e.toString());
        System.exit(1);
    }
    connection = new Connection[10];
    session = new Session[10];
    consumer = new MessageConsumer[10];
    for (int i = 0; i < 10; i++) {
        try {

            connection[i] = connectionFactory.createConnection();
            session[i] = connection[i].createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            consumer[i] = session[i].createConsumer(dest);
            consumer[i].setMessageListener(this);
            connection[i].start();
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
        }
    }
}

@Override
public void onMessage(Message m) {

    if (m instanceof TextMessage) {
        TextMessage message = (TextMessage) m;
        try {
            System.out.println("Reading message from Listener: "
                    + new Date() + message.getText());
            Thread.sleep(1000);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 }

我使用的是 Weblogic 11g,默认配置为 ConnectionFactory & Queue。当我使用 Topic 时,它实际上每秒只发送一条消息(即在完成第一条消息之后),而对于 Queue,它每秒发送 2 到 3 条消息。如何让我的监听器支持并发处理。

最终解决方案

添加了更多的侦听器对象,在侦听器中安装了多个会话/消费者,从而解决了这个目的。 在下面找到修改后的代码。

public class ProducerServlet extends javax.servlet.http.HttpServlet implements
    javax.servlet.Servlet {

// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

// Defines the JMS context factory.
public final static String JMS_FACTORY = "jms/TestConnectionFactory";

// Defines the queue.
public final static String QUEUE = "jms/TestJMSQueue";

public final static String TOPIC = "jms/TestTopic";
TestJMSListener listeners[] = new TestJMSListener[20];
ConnectionFactory connectionFactory = null;
Queue dest1 = null;
Topic dest =null;
Connection connection = null;
MessageProducer producer = null;

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        try {
            connection = connectionFactory.createConnection();              

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(dest1);
        TextMessage message = session.createTextMessage();

        message.setText("This is message from JMSSECOND DEMO "
                + request.getParameter("Num"));
        System.out.println("Sending message: " + message.getText());
        producer.send(message);
        producer.send(session.createMessage());
    } catch (Exception e) {
        System.out.println("Exception occurred: " + e.toString());
    }

}

@Override
public void init(ServletConfig arg0) throws ServletException {      
    Context jndiContext = null;
    try {

        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
        env.put(Context.PROVIDER_URL, "http://localhost:7001");
        jndiContext = new InitialContext(env);
    } catch (NamingException e) {
        System.out.println("Could not create JNDI API context: "
                + e.toString());            
    }

    try {
        connectionFactory = (ConnectionFactory) jndiContext
                .lookup(JMS_FACTORY);
        dest1 = (Queue) jndiContext.lookup(QUEUE);
        for(int i=0;i<listeners.length;i++ ){
        listeners[i]=new TestJMSListener(Integer.toString(i+1));    
        }

    } catch (Exception e) {
        System.out.println("JNDI API lookup failed: " + e.toString());
        e.printStackTrace();            
    }

}

}


public class TestJMSListener implements MessageListener {

// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

// Defines the JMS context factory.
public final static String JMS_FACTORY = "jms/TestConnectionFactory";

// Defines the queue.
public final static String QUEUE = "jms/TestJMSQueue";

public final static String TOPIC = "jms/TestTopic";

public String listnerNum = "";
public TestJMSListener(String listerNo) {
    super();
    System.out.println("********* Consumer check **********");
    listnerNum = listerNo;
    Context jndiContext = null;
    ConnectionFactory connectionFactory = null;
    Connection connection = null;
    Session session = null;
    Queue dest1 = null;
    Topic dest = null;
    MessageConsumer consumer = null;
    // TextMessage message = null;

    try {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
        env.put(Context.PROVIDER_URL, "http://localhost:7001");
        jndiContext = new InitialContext(env);
    } catch (NamingException e) {
        System.out.println("Could not create JNDI API context: "
                + e.toString());
        System.exit(1);
    }

    try {
        connectionFactory = (ConnectionFactory) jndiContext
                .lookup(JMS_FACTORY);
        dest1 = (Queue) jndiContext.lookup(QUEUE);
    } catch (Exception e) {
        System.out.println("JNDI API lookup failed: " + e.toString());
        System.exit(1);
    }
    try{
            connection = connectionFactory.createConnection();
            session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            consumer = session.createConsumer(dest1);
            consumer.setMessageListener(this);
            connection.start();
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
        }


}

@Override
public void onMessage(Message m) {

    if (m instanceof TextMessage) {
        TextMessage message = (TextMessage) m;
        try {
            System.out.println("Reading message from Listener: "+listnerNum+ " : "
                    + new Date() + message.getText());
            Thread.sleep(1000);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

}

【问题讨论】:

  • 如果您不注册侦听器,而是使用 MessageConsumer#receive 手动触发 10 个使用消息的线程,那么消息消费的行为如何?。
  • Robin Green:附上的答案不清楚,能否请您澄清一下。我已经创建了多个会话,但仍然是按顺序处理的。

标签: java concurrency jms


【解决方案1】:

我不是 WebLogic JMS 方面的专家,但您的代码看起来不错(除了您创建的多个连接,这不是必需的,多个会话就足够了。我什至认为多个连接在消耗时可能会产生负面影响线程),它应该同时消耗。既然您说使用队列,您每秒会收到 2-3 条消息,实际上您确实会同时收到它们(因为每个侦听器都睡一秒钟)。

既然你说你每秒得到 2-3,我认为你实际上得到 4-6,因为生产者发送了一个 TextMessage ('producer.send (message)' 和一个空的 'producer.send(session.createMessage())')。 除此之外,我会检查 ConnectionFactpry 的服务器配置。我记得需要为 MDB 配置 WorkManager 线程,但遗憾的是不确定“手动”JMS 客户端。

所以我会这样工作:

  • 移除双重发送('producer.send(session.createMessage())')
  • 为生产者使用一个连接(但保留新会话)(而不是在每个请求上重新创建)
  • 在客户端使用一个连接(但有多个会话)
  • 正确关闭生产者会话(可能会消耗线程)
  • 检查是否生成了足够的消息
  • 检查管理限制

希望对您有所帮助。

问候,

梅西

【讨论】:

    【解决方案2】:

    在您的代码中,您只有一个侦听器实例(在创建 Servlet 实例时创建),因此您将仅按顺序接收消息,
    无论您有多少发送方会话......它只是队列。
    如果您想同时接收,那么您可能需要多个侦听器,并且只有一次消息将在任何一个侦听器中传递。
    如果您想同时处理消息,一旦按顺序交付,则创建线程池并在单独的线程中委托进程并返回侦听模式。
    注意** 在此模式下,您可能无法正确处理 Ack 模式,因为您在未完成消息过程的情况下确认。

    【讨论】:

      【解决方案3】:

      我查看了您的解决方案并意识到您有多个并发会话、消费者等,但只有一个队列可以处理所有这些。队列是一个队列,它们都按顺序通过管道排队。如果您碰巧只有其中一个 - 那么此时您有一个执行线程并且一切都按顺序进行,因为队列不允许并发事情发生。

      如果您在不同的线程中实现several queues,您的机器可以同时处理多个调用。多个队列可能意味着使用不同的队列名称等,但对于这种麻烦,您可以使用像Apache Camel 这样的负载平衡器解决方案来实际为您选择队列。至少this closed post 让我了解到这种队列和线程的组合是可能的。

      然后,平衡器为每个请求选择单独的队列,每个队列都有自己的顺序工作来处理请求。并发会话的数量是一个配置问题。

      【讨论】:

      • 复杂的解决方案。简单的解决方案是添加更多的侦听器对象。当我添加 20 个侦听器时,它们的并发处理速度非常快。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 2017-12-27
      • 1970-01-01
      • 2015-12-10
      • 2010-10-12
      • 1970-01-01
      相关资源
      最近更新 更多