无法从restful Web服务调用消息bean

时间:2011-04-23 07:45:01

标签: web-services rest jms ejb nullpointerexception

我正在尝试将Restful Webservice创建为Message Driven Bean的客户端,但是当我调用restful方法时,它会给我以下错误

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) 

这是以下代码:

// 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>";
}

我是否需要在sun-web.xml或web.xml中指定任何内容?

3 个答案:

答案 0 :(得分:1)

我认为这取决于您的应用程序服务器设置。你在上面的某处注入了connectionFactory吗?或者进行了上下文查找?

答案 1 :(得分:0)

connectionFactory为空。它需要以某种方式初始化。

答案 2 :(得分: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);
     }
相关问题