2015-04-20 2 views
3

Я извиняюсь перед всем кодом, но я смущен тем, почему этот же код, казалось бы, работал раньше, но не сейчас.Jetty WebSocketClientSelectorManager - Connection Failed java.io.IOException: невозможно инициировать SSL

У меня есть простой клиент WebSockets для причала, и я получаю ошибку в названии. Самое забавное в том, что у меня есть HTML-файл с javascript для использования того же SSL-сервера, и он отлично работает.

Поисковые запросы в Интернете не так сильно связаны с этой ошибкой.

Согласно исходному молы кода, нет никакого контекста SSL:

 if ("wss".equalsIgnoreCase(scheme)) 
     { 
      // Encrypted "wss://" 
      SslContextFactory sslContextFactory = getSslContextFactory(); 
      if (sslContextFactory != null) 
      { 
       SSLEngine engine = newSSLEngine(sslContextFactory,channel); 
       SslConnection sslConnection = new SslConnection(bufferPool,getExecutor(),endPoint,engine); 
       sslConnection.setRenegotiationAllowed(sslContextFactory.isRenegotiationAllowed()); 
       EndPoint sslEndPoint = sslConnection.getDecryptedEndPoint(); 

       Connection connection = newUpgradeConnection(channel,sslEndPoint,connectPromise); 
       sslEndPoint.setIdleTimeout(connectPromise.getClient().getMaxIdleTimeout()); 
       sslEndPoint.setConnection(connection); 
       return sslConnection; 
      } 
      else 
      { 
       throw new IOException("Cannot init SSL"); 
      } 
     } 
     else 
     { 
      // Standard "ws://" 
      endPoint.setIdleTimeout(connectPromise.getDriver().getPolicy().getIdleTimeout()); 
      return newUpgradeConnection(channel,endPoint,connectPromise); 
     } 

Вот отслеживающий от бега исполнения:

20:32:15.560 [main] INFO c.d.e.w.EspClientMain - client connected, waiting for close 
20:32:15.626 [[email protected]] DEBUG o.e.j.i.SelectorManager - Queued change [email protected]3d828 
20:32:15.627 [[email protected][email protected]/0] DEBUG o.e.j.i.SelectorManager - Selector loop woken up from select, 0/0 selected 
20:32:15.628 [[email protected][email protected]/0] DEBUG o.e.j.i.SelectorManager - Running change [email protected]3d828 
20:32:15.629 [[email protected][email protected]/0] DEBUG o.e.j.i.SelectorManager - Selector loop waiting on select 
20:32:15.630 [[email protected][email protected]/0] DEBUG o.e.j.i.SelectorManager - Selector loop woken up from select, 1/1 selected 
20:32:15.636 [[email protected][email protected]/0] DEBUG o.e.j.w.c.i.WebSocketClientSelectorManager - newEndPoint(java.nio.channels.SocketChannel[connected local=/10.214.156.230:39311 remote=node.dj2.dfdev.biz/10.214.156.230:8443], [email protected] keys=1 selected=1, [email protected]) 
20:32:15.648 [[email protected][email protected]/0] DEBUG o.e.j.i.AbstractEndPoint - onOpen [email protected]{node.dj2.dfdev.biz/10.214.156.230:8443<->39311,Open,in,out,-,-,300000,null}{io=0,kio=0,kro=8} 
20:32:15.648 [[email protected][email protected]/0] DEBUG o.e.j.i.IdleTimeout - [email protected]{node.dj2.dfdev.biz/10.214.156.230:8443<->39311,Open,in,out,-,-,300000,null}{io=0,kio=0,kro=8} idle timeout check, elapsed: 6 ms, remaining: 299994 ms 
20:32:15.649 [[email protected][email protected]/0] DEBUG o.e.j.w.c.i.WebSocketClientSelectorManager - newConnection(java.nio.channels.SocketChannel[connected local=/10.214.156.230:39311 remote=node.dj2.dfdev.biz/10.214.156.230:8443],[email protected]{node.dj2.dfdev.biz/10.214.156.230:8443<->39311,Open,in,out,-,-,300000,null}{io=0,kio=0,kro=8},[email protected]{false,false,null}) 
20:32:15.654 [[email protected][email protected]/0] INFO c.d.e.w.EspClientMain$EspClient - unexpected websockets exception: {} 
java.io.IOException: Cannot init SSL 
    at org.eclipse.jetty.websocket.client.io.WebSocketClientSelectorManager.newConnection(WebSocketClientSelectorManager.java:96) ~[datafascia-emerge-websocket-esp-client-1.0-SNAPSHOT.jar:na] 
    at org.eclipse.jetty.io.SelectorManager$ManagedSelector.createEndPoint(SelectorManager.java:735) [datafascia-emerge-websocket-esp-client-1.0-SNAPSHOT.jar:na] 
    at org.eclipse.jetty.io.SelectorManager$ManagedSelector.processConnect(SelectorManager.java:676) [datafascia-emerge-websocket-esp-client-1.0-SNAPSHOT.jar:na] 
    at org.eclipse.jetty.io.SelectorManager$ManagedSelector.processKey(SelectorManager.java:640) [datafascia-emerge-websocket-esp-client-1.0-SNAPSHOT.jar:na] 
    at org.eclipse.jetty.io.SelectorManager$ManagedSelector.select(SelectorManager.java:607) [datafascia-emerge-websocket-esp-client-1.0-SNAPSHOT.jar:na] 
    at org.eclipse.jetty.io.SelectorManager$ManagedSelector.run(SelectorManager.java:545) [datafascia-emerge-websocket-esp-client-1.0-SNAPSHOT.jar:na] 
    at org.eclipse.jetty.util.thread.NonBlockingThread.run(NonBlockingThread.java:52) [datafascia-emerge-websocket-esp-client-1.0-SNAPSHOT.jar:na] 
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:620) [datafascia-emerge-websocket-esp-client-1.0-SNAPSHOT.jar:na] 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:540) [datafascia-emerge-websocket-esp-client-1.0-SNAPSHOT.jar:na] 
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_71] 

Мой источник, который создает соединение довольно простое и как представляется, следуют примерам:

// Run forever and recover lost server connections 
while (true) { 
    WebSocketClient client = new WebSocketClient(); 
    EspClient socket = new EspClient(); 
    try { 
    logger.info("starting client"); 
    client.start(); 
    logger.info("client started"); 
    URI espServerURI = new URI(espURI); 
    logger.info("requesting WebSockets upgrade"); 
    ClientUpgradeRequest request = new ClientUpgradeRequest(); 
    logger.info("WebSockets upgrade complete, connecting"); 
    client.connect(socket, espServerURI, request); 
    logger.info("client connected, waiting for close"); 

    try { 
     socket.awaitClose(); 
    } catch (InterruptedException e) { 
     logger.error("sleep interrupted ...", e); 
    } 
    } catch (Throwable t) { 
    logger.error("could not connect to {}: {}", espURI, t); 
    try { 
     Thread.sleep((long)(10 * DateTimeConstants.MILLIS_PER_SECOND)); 
    } catch (Exception e) { 
     logger.error("sleep interrupted ...", e); 
    } 
    } finally { 
    try { 
     logger.error("unexpected ESP Server termination. Retrying connection: {}", espURI); 
     client.stop(); 
     Thread.sleep((long)(10 * DateTimeConstants.MILLIS_PER_SECOND)); 
    } catch (Exception e) { 
     logger.error("exception stopping the ESP client: {}", e); 
    } 
    } 
} 

Класс клиента:

 /** 
     * The Emerge ESP WebSockets Client 
     */ 
     public static class EspClient extends WebSocketAdapter { 
    private Logger logger = LoggerFactory.getLogger(EspClient.class); 
    private final String pingMessage = "I am still here"; 
    private final ByteBuffer pingBuffer = ByteBuffer.wrap(pingMessage.getBytes()); 

    private final CountDownLatch closeLatch; 

    // Setup the JMS queues 
    private static AbstractApplicationContext context = 
     new ClassPathXmlApplicationContext("applicationContext.xml"); 
    private static JmsConnector jmsEspConnection = 
     (JmsConnector) context.getBean("JmsEspMessageBean"); 

    /** 
    * Setup the synchronization 
    */ 
    public EspClient() { 
     this.closeLatch = new CountDownLatch(1); 
    } 

    /** 
    * Synchronizes on the countdown latch. 
    */ 
    public void awaitClose() throws InterruptedException { 
     this.closeLatch.await(); 
    } 

    @Override 
    public void onWebSocketClose(int statusCode, String reason) { 
     logger.error("ESP Server session closed: {}, {}", statusCode, reason); 
     espSession = null; 
     this.closeLatch.countDown(); 
    } 

    @Override 
    public void onWebSocketConnect(Session session) { 
     logger.info("Connected: {}", session); 
     espSession = session; 
     readAndSendMessage(); 
    } 

    @Override 
    public void onWebSocketError(Throwable cause) { 
     logger.info("unexpected websockets exception: {}", cause); 
     espSession = null; 
    } 

    @Override 
    public void onWebSocketBinary(byte[] payload, int offset, int len) { 
     logger.info("unexpected binary message received"); 
    } 

    @Override 
    public void onWebSocketText(String message) { 
     if (message != null) { 
     logger.info("received ESP Server response: {}", message); 
     } 
    } 
    /** 
    * Reads a message from the ESP queue and sends it to the ESP server. 
    */ 
    private void readAndSendMessage() { 
     while (espSession != null) { 
     String message = jmsEspConnection.receiveTextMessage((long)(4 * DateTimeConstants.MILLIS_PER_MINUTE)); 
     if (message != null) { 
      logger.info("read message: {}", message); 
      sendMessage(message); 
     } else { 
      logger.info("no message available, sending keepalive message"); 
      try { 
      espSession.getRemote().sendPing(pingBuffer); 
      } catch (IOException e) { 
      logger.error("error sending keepalive message to ESP Server: {}", e); 
      } 
     } 
     } 
     logger.error("ESP Server connection lost"); 
    } 

    /** 
    * Sends a message to the ESP server. 
    */ 
    private void sendMessage(String message) { 
     if (espSession != null) { 
     try { 
      espSession.getRemote().sendString(message); 
     } catch (Exception e) { 
      logger.error("error sending sensor message to ESP Server: {}", e); 
      espSession = null; 
     } 
     } 
    } 
    } 

ответ

12

Вы должны предоставить SslContextFactory в конструкторе.

ака:

SslContextFactory ssl = new SslContextFactory(); 
// Configure ssl for your client here, such as 
// what ciphers and protocols are available, 
// what client certificates to use, 
// what CA's you want to validate against, 
// what your blacklist behavior is, 
// your truststore location and access, 
// your keystore location and access, 
// caching, CRL, OCSP, renegotiation, etc ... 

WebSocketClient client = new WebSocketClient(ssl); 
+0

Спасибо! После добавления контекста SSL я смог обработать ожидающие сообщения более 20 000. Как javascript в моем тесте HTML работает без контекста SSL? Он встроен в javascript? Вы можете сказать, что я новичок в этом. –

+0

Спасибо миллион! – vikingsteve

Смежные вопросы