Given that:
I'd like to deploy webapp, packaged as WAR having web.xml
in it, to Jetty server.
Within that app, I'd like to be able to have a JSR-356 specified javax websocket endpoints configured. I prefer, that those endpoints to be provided via ServerEndpointConfig
, not an annotation scan.
There are many resources exemplifying that with an embedded Jetty, utilizing already famous WebSocketServerContainerInitializer.configureContext(context);
API. I can't do that, obviously.
There are others, jumping directly to ServletContextListener
and obtaining the ServerContainer
via famous context.getAttribute("javax.websocket.server.ServerContainer"
). So far I'm getting pretty much NULL
via this API, so obviously container is not added.
Question:
What is that bit of configuration that is missing? Can it be done, preferably, via web.xml
? If it is about config files like jetty.xml
or jetty.ini
- example would be nice, again, preferably for xml
syntax.
Update:
As per answer below (the accepted one) and as I've actually tried to describe here - the known way of configuration is absolutely working just fine. Saying known I mean either by adding --module=websocket
to some *.ini
file for a non-embedded Jetty, or by calling WebSocketServerContainerInitializer.configureContext
for an embedded one.
So rephrasing the question: is there any experience/knowledge from someone to enable websocket module by purely XML
based configuration?
If using the
${jetty.base}
and${jetty.home}
recommended installation process for Standalone Jetty, you should go to your${jetty.base}
instance directory and enable thewebsocket
module.Now you have websocket enabled for that
${jetty.base}
instance.If you want Jetty to discover your Server WebSocket endpoints via bytecode scanning your deployed webapps for annotations, then you'll also want the
annotations
module.Once that's complete, you can do one (or more) of the following to have the websocket server endpoints deployed with your webapp.
@ServerEndpoint
(fromjavax.websocket.server.ServerEndpoint
)javax.websocket.server.ServerApplicationConfig
in your project and return the Server endpoints you want Jetty to deploy.javax.websocket.server.ServerContainer
from theServletContext.getAttribute("javax.websocket.server.ServerContainer")
and use it'saddEndpoint()
methods. Note that this is only possible from either aServletContextListener.contextInitialized(ServletContextEvent sce)
orServletContainerInitializer.onStartup(Set<Class<?>> c, ServletContext ctx)
Why does this work in standalone Jetty? What is standalone Jetty doing to make this possible?
The following happens:
websocket
module adds thelib/websocket/*.jar
to the server classpathwebsocket
module depends on bothclient
andannotations
modulesclient
module addslib/jetty-client-<jetty.version>.jar
to the server classpathannotations
module addslib/jetty-annotations-<jetty.version>.jar
andlib/annotations/*.jar
to the server classpathannotations
module depends on theplus
moduleannotations
module selectsetc/jetty-annotations.xml
for execution on startupannotations
module adds JPMS modules by nameorg.objectweb.asm
plus
module addslib/jetty-plus-<jetty.version>.jar
to the server classpathplus
module selectsetc/jetty-plus.xml
for execution on startupplus
module depends on theserver
,security
,jndi
,webapp
, andtransactions
modules(I'll skip the rest of the modules that are selected this way)
In short, with just adding
websocket
module you gain the following server classpath entriesAnd the following XML files
Both of these XML files simply modify the default
Configuration
list on the server side, making theConfiguration
behavior they introduce available to all deployed WebApps.You can alternatively set the
Configuration
on theWebAppContext
(before it's started) for webapp specific behaviors.Example:
You can see all of this from the command line too.
Show the active
${jetty.base}
configuration, what the XML property values are, what the server classpath is, and what XML is going to be executed (and in what order!!)Show the list of modules and how they relate (along with which ones are selected in your
${jetty.base}
configuration)