简体   繁体   English

JMS队列客户端onMessage()未从发送方接收消息

[英]JMS Queue client onMessage() not receiving message from sender

I am new to JMS and working with Jdeveloper and Weblogic 12c, My code is running well but the Consumer onMessage() method is not receiving any message from Producer. 我是JMS的新手,并且使用Jdeveloper和Weblogic 12c,我的代码运行良好,但是Consumer onMessage()方法未从Producer接收任何消息。 Though I can get a message when I didn't use the onMessage() . 虽然我可以在不使用onMessage()收到消息。 Do I have to configure any file before it work? 我必须配置任何文件才能工作吗?

Here is the message consumer code 这是消息使用者代码

package com.soap_jms;



import javax.jms.ExceptionListener;
import java.util.*;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.Context;

import utils.system;


public class QueueConsume  implements MessageListener, ExceptionListener{

        private final static String JMS_FACTORY="jms/ThesisConnectionFactory";
        private final static String QUEUE="jms/ThesisJMSQueue"; 
        private static Connection mConnection;
        private static ConnectionFactory mConnectionFactory;
        private static Queue mQueue;
        private static MessageConsumer mConsumer;
        private static Session mSession;


       public static void main (String [] args) throws JMSException, NamingException {
       mConnection=null;
       MessageListener listener = null;
       System.out.println("Entering into JMS Queue Consumer........");
       Context ctx = QueueConsume.getInitialContext();
       mConnectionFactory=(javax.jms.ConnectionFactory)ctx.lookup(JMS_FACTORY);
       mQueue=(javax.jms.Queue)ctx.lookup(QUEUE);
       mConnection=mConnectionFactory.createConnection();
       mSession=mConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
       mConsumer=mSession.createConsumer(mQueue);
       QueueConsume mConsume = new QueueConsume();
       mConsumer.setMessageListener(mConsume);
       mConnection.setExceptionListener(mConsume);
       mConnection.start();

       System.out.println("JMS is set to accept sent message.....");
       System.out.println("Exiting from JMS Queue Consumer ........"); 
       close();
       } // closes main method


    @SuppressWarnings("oracle.jdeveloper.java.insufficient-catch-block")
    public void onMessage(Message message) {
        try {
            System.out.println("Message is " + ((TextMessage) message).getText());
        } catch (JMSException e) {
        }
    }
      @SuppressWarnings("oracle.jdeveloper.java.unchecked-conversion-or-cast")
    public static Context getInitialContext() throws JMSException, NamingException {
            Hashtable htb = new Hashtable();
            htb.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
            htb.put(Context.PROVIDER_URL,"t3://localhost:7101");
            Context ctx= new InitialContext(htb);
        return ctx;   
        }       


    public static void close() throws JMSException {
        mSession.close();
        mConnection.close();
        mConsumer.close();
    }



    public void onException(JMSException exception)
        {
          System.err.println("an error occurred: " + exception);
        } 

}

I suspect the problem is because the main thread is ending before the onMessage method is called. 我怀疑问题是因为在调用onMessage方法之前主线程已结束。 After Connection.start() method is called you are calling close . 调用Connection.start()方法后,您将调用close Typically the setMessageListener is an asynchronous operation, so the call returns immediately but actual starting of onMessage thread happens in parallel. 通常,setMessageListener是异步操作,因此调用会立即返回,但onMessage线程的实际启动是并行发生的。 Suggest you to put some sleep or some other way to make the main thread to wait while onMessage thread receives messages. 建议您在onMessage线程接收消息时让睡眠或其他方式使主线程等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM