JMS连接杀死websocket

时间:2017-03-30 09:27:57

标签: javascript java-ee websocket jms

我有一个网站,它打开一个websocket并在警报中显示从服务器发送的消息。但是,只要我创建一个JMS连接,websocket就不再连接了。该应用程序在glassfish 4上运行。这是代码:

wstest.xhtml:

Holo.Light

websocket.js:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
  <h:head>
    <title>TODO supply a title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <h:outputScript name="websocket.js" library="js" target="head"/>
  </h:head>
  <h:body>
    TODO
  </h:body>
</html>

WebsocketEndpoint.java:

var host = 'ws://localhost:8095/path';
var websocket = new WebSocket(host);

websocket.onopen = function () {
  alert('Websocket open!');
};

websocket.onmessage = function (event) {
  alert(event.data);
};

WebsocketChat.java:

@ServerEndpoint("/path")
public class WebsocketEndpoint {

  private WebsocketChat chat = new WebsocketChat();

  @OnOpen
  public void onOpen(Session session) {
    System.out.println("Session opened");
    session.getAsyncRemote().sendText("Hello Javascript");
  }
}

请注意,对WebsocketChat构造函数中的init方法的调用进行了注释。通过这种方式,我可以看到两个javascript警报:&#39; Websocket打开!&#39;打开websocket时,以及来自服务器的Hello Javascript&#39;消息&#34;会话已打开&#34;在glassfish输出中也可以看到。

但是,当我在WebsocketChat构造函数中取消注释init方法时,我仍然可以看到&#39; Websocket已打开&#39;提醒,但是你好Javascript&#39;警报将不会显示。 &#34;会话开始&#34;消息也从glassfish输出中消失了。 init方法负责创建JMS连接。

1 个答案:

答案 0 :(得分:1)

我希望您的init()方法投放NullPointerException,因为connectionFactory不会被初始化:

public class WebsocketChat {

    @Resource(mappedName="jms/ConnectionFactory")
    private ConnectionFactory connectionFactory;

    public WebsocketChat() {
        // You can't access the connectionFactory here because the container cannot
        // inject it before the the object is created.
        // The object will not be created until this constructor has completed
        // executing.
    }

    /* The container will call this after the object is created */
    @PostConstruct
    public final void init() {
        try {
            try (Connection con = connectionFactory.createConnection()) {
                // do something with the connection...
            }
        } catch (JMSException ex) {
            Logger.getLogger(WebsocketChat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

更新了WebsocketEndpoint:

@ServerEndpoint("/path")
public class WebsocketEndpoint {

    /*
     * You need to inject this. The container will create and initialise it for you.
     */
    @Inject
    private WebsocketChat chat;

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Session opened");
        session.getAsyncRemote().sendText("Hello Javascript");
    }
}
  

FWIW,上面的代码在WildFly 10.1中适用于我,但有一处改动:

    @Resource(lookup="java:/ConnectionFactory")  
    private ConnectionFactory connectionFactory;

恰好是WildFly中的默认ConnectionFactory。

此外,您的JavaScript可能需要在其Web套接字URL中包含webapp上下文:

 var host = 'ws://localhost:8095/<webapp-context>/path';
  

上面的代码也适用于GlassFish 4.1.1,并进行了以下更改:

    @Resource(name="java:comp/DefaultJMSConnectionFactory")  
    private ConnectionFactory connectionFactory;