EDIT: I uploaded a full working example, ready to be customized, on github. If you just want a working solution, go there. Otherwise, keep reading for the explanation.
When it comes to Java microframeworks for API development, I've been using Spark for some time in a couple of different projects. For most casual needs, it works out of the box; however, there are circumstances where the "easy" options for configuring the embedded Jetty instance, are simply not enough. In that case, you have to take control and basically replace the instance with one that you completely control.
Due to how Jetty works, there are multiple moving parts: the server takes care of things like thread pools, the socket is responsible for SSL and other low-level protocol stuff, and the handler is where you do high-level middleware (changing headers etc). This means that you might need several factories to take care of all these. The good news is that Spark is granular enough that you can (more or less) limit yourself to what you need. The main pattern is: there will be a factory for each element, with a create()
method returning an interface; you implement the interface and swap out the default factory with your own. However, these factories at the moment (Spark 2.8) are somewhat nested, so depending on where you need to work, you might have to replace a bunch of classes before you hit the point you're interested in.
The main entry point is EmbeddedServers.add(identifier, serverFactory)
. This is what you'll call from the main()
method you use for all the post()
and get()
configuration directives. It is important that EmbeddedServers should appear right at the top, before any other configuration directive, otherwise Spark will use its default factory. It should look more or less like this:
EmbeddedServers.add( EmbeddedServers.Identifiers.JETTY, new MyWonderfulServerFactory());
For the identifier, we re-use the default one because otherwise Spark will take over again. Nothing to do there.
The server factory is where real work begins. Your class (or lambda) needs to implement the spark.embeddedserver.EmbeddedServerFactory
interface, which has one method:
public EmbeddedServer create( Routes routeMatcher, StaticFilesConfiguration staticFilesConfiguration, ExceptionMapper exceptionMapper, boolean hasMultipleHandler)
As a starter, you can copy the content of spark.embeddedserver.jetty.EmbeddedJettyFactory
as-is. You don't need constructors (unless you want them). Strictly speaking you don't need withThreadPool() and withHttpOnly() methods either, but I suggest you keep them anyway; just change the return type to match MyWonderfulServerFactory.
Create() does three things:
- Initializing the route matcher, which you probably don't want to touch;
- Initializing the Jetty handler for that matcher, which you may want to configure for things like header manipulation and other middleware;
- Creating the embedded server, which is likely what you are after.
I will assume we want to tweak n.3. The main place where to pay attention is this line at the end of create():
return (new EmbeddedJettyServer(this.serverFactory, handler) ).withThreadPool(this.threadPool);
This will return the default configuration, and we don't want that. So we change it to something like:
return (new MyWonderfulEmbeddedServer(handler) ).withThreadPool(this.threadPool);
Now we need a MyWonderfulEmbeddedServer class, which should implement spark.embeddedserver.EmbeddedServer
. Again, as a starting point, you can copy spark.embeddedserver.jetty.EmbeddedJettyServer
, and change the return types to match. I also suggest you get rid of the factory parameter in constructor and the related field, which adds a bit of unnecessary complexity. That factory is actually used only in one place:
if (this.threadPool == null) { this.server = this.serverFactory.create( maxThreads, minThreads, threadIdleTimeoutMillis); } else { this.server = this.serverFactory.create(this.threadPool); }
Which you can replace with the following (straight from spark.embeddedserver.jetty.JettyServer):
if (this.threadPool == null) { if (maxThreads > 0) { int min = minThreads > 0 ? minThreads : 8; int idleTimeout = threadIdleTimeoutMillis > 0 ? threadIdleTimeoutMillis : '\uea60'; server = new Server(new QueuedThreadPool(maxThreads, min, idleTimeout)); } else { server = new Server(); } } else { this.server = threadPool != null ? new Server(threadPool) : new Server(); }
It's a bunch of stuff related to the amount of threads and timeouts. If that's what you were trying to configure, btw, this is where you can do it. To be honest, I believe Spark developers intended that the "proper" way, to do that particular customisation, would be to keep the factory parameter as it is, implement your own alternative to the badly-named JettyServer class -- it should be something like JettyThreadConfigFactory, really -- which implements the similarly badly-named JettyServerFactory, and pass it to the constructor in MyWonderfulServerFactory.create().
In my case, though, I was after an SSL customisation, and for that I had to swap out yet another factory, spark.embeddedserver.jetty.SocketConnectorFactory, mentioned in the second half of this block:
ServerConnector connector; if (sslStores == null) { connector = SocketConnectorFactory.createSocketConnector( this.server, host, port); } else { connector = SocketConnectorFactory.createSecureSocketConnector( this.server, host, port, sslStores); }
In this case there is no interface, you can just extend the existing class with what you need. I wanted to enforce a particular set of SSL ciphers and protocols, so I overrode createSecureSocketConnector() adding the following bits:
sslContextFactory.setExcludeProtocols("SSLv3", "SSLv2", "TLSv1.2"); // first we clear existing exclusions sslContextFactory.setExcludeCipherSuites(new String[]{}); // then we re-add what we need sslContextFactory.setIncludeCipherSuites(bigArrayOfCipherNames);
And that's it. Now you know how to instantiate your own Jetty instance for Spark. It's a bit convoluted, and hopefully a "spark 3" will give us a better architecture to work with, one day. In the meantime, this is how you can do it.