2011-04-23 142 views
1

我想創建RESTful Web服務作爲驅​​動Bean消息的客戶端,但是當我調用RESTful方法它給我以下錯誤,當無法從RESTful Web服務

Connection connection = connectionFactory.createConnection(); 

SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container 
java.lang.NullPointerException 
     at com.quant.ws.GetConnection.startThread(GetConnection.java:99) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 

這裏是調用的messge豆下面的代碼:

// Inside class declaration 
@Resource(mappedName = "jms/testFactory") 
private static ConnectionFactory connectionFactory; 

@Resource(mappedName = "jms/test") 
private static Queue queue; 

Web服務方法

@GET 
@Path("startThread") 
@Produces("application/xml") 
public String startThread() 
{ 

    try{ 
    Connection connection = connectionFactory.createConnection(); // its line number 99 
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
    MessageProducer producer = session.createProducer(queue); 
    Message message = session.createTextMessage(); 
    message.setStringProperty("name", "start"); 
    producer.send(message); 
    }catch(JMSException e){ 
     System.out.println(e); 
    } 
    return "<data>START</data>"; 
} 

我是否需要指定anyt在sun-web.xml或web.xml中進行操作?

+0

的NullPointerException異常說,它發生在99線也就是上面一行99解決了嗎? – 2011-04-23 08:30:20

+0

Connection connection = connectionFactory.createConnection();是行號99 – Hunt 2011-04-23 08:50:14

回答

0

我已經通過替換下面的代碼

try{ 
     InitialContext ctx = new InitialContext(); 
     queue = (Queue) ctx.lookup("jms/test"); 
     QueueConnectionFactory factory = 
     (QueueConnectionFactory) ctx.lookup("jms/testFactory"); 
     Connection connection = factory.createConnection(); 
     Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
     MessageProducer producer = session.createProducer(queue); 

     Message message = session.createTextMessage(); 
     message.setStringProperty("name", "start"); 
     producer.send(message); 

    } 
    catch(NamingException e){ 
     System.out.println(e); 
    } 
    catch(JMSException e){ 
    System.out.println(e); 
    } 
1

我認爲這取決於您的應用程序服務器設置。你在上面的某個地方注入了connectionFactory嗎?或者做了上下文查找?

+0

請檢查我的編輯 – Hunt 2011-04-23 08:49:55

+0

這應該這樣做,只要您的工廠和隊列都註冊了這些名稱。 – Dirk 2011-04-23 10:09:10

0

connectionFactory爲空。它需要以某種方式進行初始化。

+0

嗯,我不知道該怎麼做 – Hunt 2011-04-23 09:20:58