2016-05-03 168 views
0

我是JMS的新手,並且使用Jdeveloper和Weblogic 12c,我的代碼運行良好,但消費者onMessage()方法沒有收到來自Producer的任何消息。雖然我沒有使用onMessage()時可以收到消息。在工作之前,我必須配置任何文件嗎?JMS隊列客戶端onMessage()沒有收到來自發件人的消息

這裏是消息消費者代碼

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); 
     } 

} 
+0

您是否已將onMessage消息偵聽器附加到使用者? – Shashi

+0

是的Shashi。我附上了它。看到上面的代碼。謝謝。 – TheGood

回答

1

我懷疑問題是因爲被稱爲onMessage方法之前,主線程結束。在調用Connection.start()方法後,您正在致電close。通常情況下,setMessageListener是一個異步操作,所以該調用立即返回,但實際的onMessage線程並行發生。建議你放一些睡眠或其他方式讓主線程等待,而onMessage線程接收消息。

+0

謝謝莎西。它以這種方式工作。 – TheGood

+0

你介意接受我的回答嗎?我貪婪:) – Shashi

相關問題