Deploying WebSocket annotated & programmatic server endpoints together

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

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!

About Abhishek

Loves Go, NoSQL DBs and messaging systems
This entry was posted in Java EE and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s