Git Product home page Git Product logo

websocket-exception-handling's Introduction

WebSocket Exception Handling

Demonstrates that GlassFish 4.0.1-b08-m1/Tyrus x.x successfully handles application exceptions occuring in a @ServerEndpoint. WildFly 8.1.0/Undertow 1.0.15 force-close the endpoint without even calling @OnClose. A bug (with a finer description) has been filed here: https://issues.jboss.org/browse/UNDERTOW-284

How-to setup this project

Just open the project like any other Maven-based Java project. The project uses Arquillian as test runner. The POM file include two profiles for GlassFish- and WildFly remote. Thus make sure that either GlassFish or WildFly is running, and have the corresponding maven build profile activated, then just build/test the project and the test application will be deployed and executed live on the server.

Workaround

A workaround for Undertow is to write a decorator method that intercept the message processing and wrap the real message handler in a try-catch statement. If a RuntimeException is caught, then programmatically call the @OnError annotated method. Here's a code sketch:

private boolean serverIsUsingUndertow() {
    // Alternatively, use Session.getContainer()
    return ContainerProvider.getWebSocketContainer()
            .getClass().getName()
            .equals("io.undertow.websockets.jsr.ServerWebSocketContainer");
}

/** Is decorator. */
@OnMessage
public void onMessage(String data)
{
    if (serverIsUsingUndertow()) // <-- should be cached
        try { __onMessage(data); }
        catch (RuntimeException e) { onError(e); } // <-- the Undertow hack
    else
        __onMessage(data);
}

/** Is the real message handler. */
private void __onMessage(String data) {
    // ... process as usual
}

@OnError
public void onError(Throwable throwable) {
    // ... process as usual
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.