我正在尝试创建Restful Webservice作为消息驱动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中指定任何东西吗?
我认为这取决于您的应用程序服务器设置。您是否在上面的某个地方注入了connectionFactory ?还是上下文查找?
connectionFactory
为空。它需要被初始化
我通过替换下面的代码解决了这个问题
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);
}