How would I specify a ThreadFactory
to change the Thread names that invoke the callbacks (onWebSocketConnect
and onWebSocketText
) of the WebSocketListener
?
The thread name would be a fixed name that is known already before I create the Server object.
I tried the following which didn't work unfortunately.
jettyServer = new Server(new NamedQueuedThreadPool("MyThreadNamePrefix");
import org.eclipse.jetty.util.thread.QueuedThreadPool;
class NamedQueuedThreadPool extends QueuedThreadPool {
private final String threadName;
public NamedQueuedThreadPool(final String threadName) {
this.threadName = threadName;
}
@Override
protected Thread newThread(final Runnable runnable) {
Thread t = super.newThread(runnable);
t.setName(threadName);
return t;
}
}
I still see thread names like: qtp1692017180-191
(which i guess stands for queued thread pool)
I am using jetty-server-9.2.9
Edit: For me it doesnt care which thread executes a specific client request, I only want that all the threads have the same name which I can choose somewhere.
For example I would prefix the thread name of all threads with "Adapter/SomeInstanceId", this is than written into the logs and gives me a convenient way of filtering/grouping.
The ThreadPool
in Jetty, and the java Executor
that it uses are in ultimate controls the thread names, and they are only really relevant to the the thread pool itself.
Besides, the lifetime of a WebSocket means that many different threads can be active for that WebSocket. So setting a name and expecting it to stick to a specific WebSocket endpoint instance is highly unlikely.
If the WebSocket goes idle long enough (think in terms of milliseconds), then the thread goes back in the pool for other resources on your Jetty server to utilize.
Once there is a need (say a WebSocket write or WebSocket endpoint has some content to read), then a new Thread is obtained from the ThreadPool to process this new activity.
Also the ThreadPool
in Jetty can be used for many things, unrelated to processing of a specific ServerConnector
endpoint, there is also cases where the Thread is needed to handle closure notifications of a WebSocket where the ServerConnector
and endpoint no longer exist. No adapter, no network anything, etc.
If you want filtering of your logs, don't do it via the Thread name, use a proper logging framework, like logback
, and setup some other sort of filtering on the logs themselves. (You can even capture all logging events from all logging frameworks and route them to a single log file)
Some options:
ServerConnector.configure(Socket)
to include connection details in the logging event.pattern
entries to indicate the details you need.In short, if your end goal is relating log events together, or log searching, or log sorting, or generally anything to do with logging, don't do it via Thread names (that's the wrong approach)