One of my previous blog post discussed Programmatic WebSocket endpoints and their deployment methodology using the javax.server.ServerApplicationConfig class.
When annotated and programmatic endpoints co-exist…
you have to make use of the a custom implementation of ServerApplicationConfig
and explicitly return set of all annotated endpoints from the getAnnotatedEndpointClasses
method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HybridEndpointDeploymentStrategy implements ServerApplicationConfig { | |
@Override | |
public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) { | |
//return ALL the auto-detected (scanned) annotated endpoints which the container will deploy | |
return scanned; | |
} | |
@Override | |
public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> endpointClasses) { | |
Set<ServerEndpointConfig> result = new HashSet<>(); | |
for (Class epClass : endpointClasses) { | |
//need to ignore Client endpoint class | |
if (epClass.equals(ChatBot.class)) { | |
ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(epClass, "/chatclub").build(); | |
result.add(sec); | |
} | |
} | |
return result; | |
} | |
} |
Please note that: using this method, you can choose to restrict the endpoints (annotated and programmatic) being deployed i.e. not all the detected endpoints need to be (will be) deployed
This is how it works..
If the WebSocket container finds subclass of ServerApplicationConfig
, it uses the same to deploy both programmatic and annotated endpoints. The result of the WAR scanning process (by the Servlet container) is passed on the methods of this class
- For getEndpointConfigs, the set (java.util.Set) of detected programmatic endpoints are passed as the method parameter
- For getAnnotatedEndpointClasses method, the set (java.util.Set) of detected annotated endpoints are passed as the method parameter
Summary of deployment scenarios
Annotated Endpoint | Programmatic Endpoint | Behavior |
---|---|---|
Yes | No | Automatic detection of annotated endpoint. No custom code needed |
No | Yes | Custom implementation of ServerApplicationConfig is compulsory. Programmatic endpoints are not auto-detected and deployed |
Yes | Yes | Custom implementation of ServerApplicationConfig is compulsory. Need to explicitly return set of all annotated endpoints from the `getAnnotatedEndpointClasses` method |
Also check out …
… a new eBook – Java WebSocket API Handbook
Cheers!