Deployment
A WebSocket container implementation has to detect and deploy both annotated and programmatic server endpoints. This chapter gives an overview of how WebSocket endpoints are deployed in Java EE (which includes a Servlet container)
Deploying annotated server endpoints
This is the easy part. Once you implement your annotated (decorated with @ServerEndpoint
) WebSocket server endpoint, the Servlet runtime weaves it scanning magic and extracts all such annotated classes from the WAR (and JARs within), deploys them and ensures they are ready for action
Deploying programmatic server endpoints
Things are different in case of programmatic server endpoints (which extend javax.websocket.Endpoint
) - they are not deployed automatically by the container unless we utilize the javax.websocket.server.ServerApplicationConfig
class
Let's look at an example
Assuming this is the only (programmatic) endpoint we want to deploy, this is how you would do it
Once the WebSocket container detects an implementation of ServerApplicationConfig
, it invokes it.
Things to note
Using
ServerApplicationConfig
is compulsory in order to deploy programmatic endpointsgetEndpointConfigs
method is for deploying programmatic endpointsgetAnnotatedEndpointClasses
method is for annotated endpointsCollections.emptySet()
is returned fromgetAnnotatedEndpointClasses
since it is assumed that there are no annotated endpoints
But why do we even need this for annotated endpoints ? Our annotated endpoints get deployed automagically... correct ? Well its partially correct since it's applicable to a scenario where we deploy annotated endpoints only The next section provides more details...
Deploying annotated & programmatic server endpoints together
When you have a combination of programmatic and annotated endpoints in your application, then you have to make use of a custom subclass of ServerApplicationConfig
and explicitly return set of all annotated endpoints from the getAnnotatedEndpointClasses
method
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
Going back to the question which was posed above.. here is the answer
This is how it works..
If the WebSocket container finds implementation of the ServerApplicationConfig
class, 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 parameterFor
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. Progr 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
Programmatic API for deploying endpoints
In addition to the above, WebSocket specification also provides a Programmatic API (not to be confused with programmatic endpoints) to deploy endpoints.
Why is it needed ?The above mentioned deployment strategies rely on the
Servlet
scanning mechanism for endpoint discovery. The programmatic APIs provides an alternative route and can be used in web (Servlet) container without the automatic scanning as well as in case WebSocket endpoints are deployed in standalone mode
The API to be used in this case is javax.websocket.server.ServerContainer
- it has separate methods for annotated (void addEndpoint(Class<?> endpointClass)
) and programmatic endpoints (void addEndpoint(ServerEndpointConfig serverConfig)
)
The instance of ServerContainer
is obtained in different ways depending on whether the application is executing in a Servlet (web container) or standalone mode
Usage in a Servlet container
Reference to an instance of the ServerContainer
interface is made available by using the javax.websocket.server.ServerContainer
attribute in javax.servlet.ServletContext
Additional notes
Standalone usage of
ServerContainer
is depended upon how a specific runtime allows the developer to obtain its instanceThis is not be mixed with the above mentioned deployment strategy which uses
ServerApplicationConfig
. The implementation will ignore duplicate endpoints (submitted by the separate deployment techniques) since this is mandated by the specification
In the next chapter ...
... we will look at how the WebSocket components work in concert with the other Java EE APIs from a platform context
Last updated