我有一个在JavaEE 7中实现的简单WebSocket,我想充当JMS生产者。然而,当我尝试将ConnectionFactory
和Queue
注入它作为JavaEE @Resource
时,我总是看到它是空的。我的代码是这样的:
@Named
@ServerEndpoint("/hello")
public class WebSocketA implements Serializable {
@Resource(mappedName = "java:/JmsXA")
private ConnectionFactory connectionFactory = null;
@Resource(mappedName = "java:jboss/exported/jms/queue/test")
private Queue testQueue = null;
...
}
我为什么不能注射这些Resource
?这在JavaEE WebSockets中不支持吗?我在Wildfly 8.1上部署,我的应用程序是一个简单的WAR,其中只有WebSocket实现类。
似乎使用某些CDI bean的JMS注入存在一些问题。我能够通过使用@javax.inject.Singleton
或@javax.ejb.Stateless
来获得注射工作。在这两种情况下,我都删除了@Named
注释。我没有测试Queue
的注射,只测试了ConnectionFactory
我用一个简单的web项目测试了最新的Glassfish 4升级版,其中只有WebSocket类:
@Stateless
@ServerEndpoint("/hello")
public class WsTest implements Serializable {
private static final Logger LOG = Logger.getLogger(WsTest.class.getName());
@Resource(mappedName = "java:comp/DefaultJMSConnectionFactory")
private ConnectionFactory connectionFactory = null;
@OnOpen
public void onOpen(Session session) throws IOException {
LOG.log(Level.INFO, "onOpen {0}", connectionFactory);
session.getBasicRemote().sendText("onOpen");
}
@OnMessage
public String echo(String message) {
LOG.log(Level.INFO, "onMessage({0}) {1}", new Object[]{message, connectionFactory});
return message + " (from your server)";
}
@OnError
public void onError(Throwable t) {
LOG.log(Level.INFO, "onError {0}", connectionFactory);
}
@OnClose
public void onClose(Session session) {
LOG.log(Level.INFO, "onClose {0}", connectionFactory);
}
}