tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Thomas <ma...@apache.org>
Subject Re: Using custom Configurator with WebSockets
Date Fri, 26 Apr 2019 15:39:14 GMT
Hmm.

Looking at this some more I'm not sure this is going to work.

HarbourServerEndpoint needs to extend javax.websocket.Endpoint which it
can't do as an interface.

I think you are going to have to wrap the instance returned by the
registry. In which case you can probably go back to using POJO.

So you'd have something like (I'm typing directly in email so the
chances of this compiling first time are slim):

@ServerEndpoint(...)
HarbourServerEndpointWrapper

    private HarbourServerEndpoint inner;

    public HarbourServerEndpointWrapper() {
        inner = RegistryProxy.getService(HarbourServerEndpoint.class);
    }

    @OnOpen
    void onOpen(Session session, EndpointConfig config) {
        inner.onOpen(Session session, EndpointConfig config);
    }

    @OnMessage
    void onMessageMessage(Session session, Message message) {
        inner.onMessageMessage(Session session, Message message);
    }

    @OnClose
    void onClose(Session session, CloseReason reason) {
        inner.onClose(Session session, CloseReason reason);
    }

    @OnError
    void onError(Session session, Throwable throwable) {
        inner.onError(Session session, Throwable throwable);
    }
}

Mark

On 26/04/2019 11:07, Christopher Dodunski wrote:
> So I've converted the server endpoint from annotation-based to
> programmatic, to get around the constraint of the @ServerEndpoint
> annotation having to decorate a concrete class.  The elements of this
> annotation now occupy an implementation of ServerApplicationConfig:
> 
> 
> public class HarbourServerApplicationConfig implements
> ServerApplicationConfig {
> 
>     ...
> 
>     @Override
>     public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<?
> extends Endpoint>> endpointClasses) {
>         Set<ServerEndpointConfig> result = new HashSet<>();
>         ServerEndpointConfig configuration =
> ServerEndpointConfig.Builder.create(HarbourServerEndpoint.class,
> "/websocket/{port-name}/{username}")
>                 .configurator(configurator)
>                 .encoders(encoders)
>                 .decoders(decoders)
>                 .build();
>         result.add(configuration);
> 
>         return result;
>     }
> }
> 
> 
> All works fine when the Builder.create() parameter is a concrete endpoint
> class, but not if an interface (the goal is having Tomcat accept as
> endpoint instances IoC proxy objects cast back to their interface).
> 
> The error message suggests that Tomcat is treating this class as concrete.
> 
> 
> 26-Apr-2019 20:00:40.605 SEVERE [ajp-nio-127.0.0.1-8009-exec-197]
> org.apache.catalina.core.ContainerBase.addChildInternal
> ContainerBase.addChild: start:
>  org.apache.catalina.LifecycleException: Failed to start component
> [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/harbour]]
>         at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
>         at
> org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
>         at
> org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
>         at
> org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
>         at
> org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:985)
> ...
> Caused by: java.lang.NullPointerException
>         at
> org.apache.tomcat.websocket.pojo.PojoMethodMapping.<init>(PojoMethodMapping.java:85)
>         at
> org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:147)
>         at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:116)
>         at
> org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5245)
>         at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>         ... 46 more
> 
> 
> Lastly, below is the interface itself (my programmatic endpoint implements
> this).
> 
> 
> public interface HarbourServerEndpoint {
> 
>     void onOpen(Session session, EndpointConfig config);
> 
>     void onMessageMessage(Session session, Message message);
> 
>     void onClose(Session session, CloseReason reason);
> 
>     void onError(Session session, Throwable throwable);
> }
> 
> 
> Unfortunately online examples of programmatic endpoints are sparse, and
> the few to be found very basic.  So am quite reliant on those familiar
> with the WebSocket library, and Tomcat's application of it.
> 
> Kind regards,
> 
> Chris.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message