4 ms·
It's a little bit bigger than a "snippet" but probably smaller than a typical "library". But I have a little bit of code I reuse quite frequently for server app
by mindcrime 2y ago
It's a little bit bigger than a "snippet" but probably smaller than a typical "library". But I have a little bit of code I reuse quite frequently for server applications. I have server implementations in Java and Python and a Java client.
So what is it? I call it "stoppable". Basically in the main() of the service I wrap everything in a while (!stopped) where stopped is a static boolean variable that defaults to "false". But before entering this loop, I start a thread that just loops, listening on a socket for a message reading "STOP". If it finds that message, it toggles the stopped variable to true.
Then in my systemd unit file for the service, the stop command just becomes something like:
java -jar stoppable-client.jar -p $SOME_PORT
It's simple but works well for my simple use-cases. There's no authentication or anything at the moment, so not really something I would use for production use in an important app; but in my context it's something I use for little POC / prototype services, or random little things I run for my own purposes on my private network.