I am new to Jetty and client/server architectures.
I managed to write a jetty server in eclipse and it works.
But how I can stop a jetty server? I heard something about stop.jar and start.jar. Where I can find it? Is it integrated in jetty-all-jar?
The various jetty-all.jar artifacts can be used for embedded jetty usage. If you decide to use this jar, you have to manage your own startup / shutdown.
Update: 2015 : As of Jetty 9, the use of jetty-all.jar as a dependency is deprecated. This is because as of Jetty 9, it is now impossible to satisfy "all" of Jetty in a single aggregate jar. There are components of Jetty that cannot be included as they will cause problems with this aggregate jar. The future of Jetty, with HTTP/2 support, also makes this aggregate jar less useful as a dependency.
Typical Embedded Mode usage
The thread that starts the server:
Server server = new Server();
// various server configuration lines
// ...
// Start server (on current thread)
server.start();
// Have current thread wait till server is done running
server.join();
The other thread that tells the server to shutdown
// Have server stop running
server.stop();
At this point the original thread's wait on server.join(); is complete and that thread continues to run.
Standard Distribution Usage
If you use the standard distribution available from download.eclipse.org/jetty/ you have the start.jar that can be used to start/stop jetty itself.
Once you have unpacked your jetty-distribution, you'll find a start.jar in the top most directory. This can be used as the following.
The process that starts jetty:
$ java -jar start.jar STOP.PORT=28282 STOP.KEY=secret
The process that stops jetty:
$ java -jar start.jar STOP.PORT=28282 STOP.KEY=secret --stop
If jetty as maven plugin, you stop the server by pressing Ctrl+C and press Y to confirm terminate.
Related
I have a Jetty 9 server embedded in my app and found that when I launch the program in eclipse and then stop it via the eclipse red button, Jetty stays running in the background.
I have to do a netstat to find which process owns the port and then do a taskkill.
Now do I configure jetty to die when the host app dies?
The Server instance should be told to stop.
server.stop();
Jetty does not know about any host app, it runs on its own daemon thread.
You could also just use java.lang.System.exit(int) to close the JVM and all threads it has running.
Killing the JVM (via the Eclipse Red Button) should kill all threads on that JVM too. If it doesn't, you have discovered a bug in Eclipse.
But before you go there, you should know that Eclipse IDE itself has it's own Eclipse Jetty server running (used for a number of things internally, and also for serving the documentation / help page).
So just the fact that you see an Eclipse Jetty instance running is misleading, it could be the one that the Eclipse IDE itself is running for its own reasons.
I've been working on a computing server and I need to make sure it restart properly in case of crash. The computer restarts properly, so I'm trying to start my process.
Our server is a C++ executable attached to Apache on a wamp server, the whole thing on a Windows computer. So I need to find a way to :
Start Wamp when Windows restarts
Start my process when Wamp is ready
I found out how to start Wamp when Windows reboots but I'm stuck on how to get my process to start when Wamp is ready. How can I do that ?
I'm using NSSM to create windows services out of regular processes. It's painless and you can set another service as a dependency, ensuring your process won't start before it.
I have a jetty 9 installed on CentOS in /srv/jetty, and I have a webapp deployed inside jetty.
When I start jetty with java -jar start.jar & then inside the web application I can do this:
File base = new File(".");
System.out.println("Base Dir: " + base.getAbsolutePath());
And it returns me the correct directory of the jetty installation.
But then I add jetty as a linux service, to run with a user called jetty, and start with service jetty start then calling the above code, will allways give me back the home directory of the user, who is running jetty in this case /home/jetty/ or if I run as root then /root/
How can I set up jetty to find the correct directory? I tried with jetty.home in the config files, but nothing seems to work.
You have a few things to worry about.
The noteworthy paths for a WebApp on Jetty:
ServletContext Real Path
Jetty Home
Jetty Base (starting in Jetty 9.1)
ServletContext Real Path
All WebApps that are deployed and started, will occupy either a work directory, or a temp directory (on the whim of the container). The servlet spec mandates that this path should be discoverable via the ServletContext for that webapp. You can find out where your WebApp is, by calling ServletContext.getRealPath("/")
See my prior answer for 5 different ways you can configure this work/temp directory:
https://stackoverflow.com/a/19232771/775715
Jetty Home
By default, all Jetty Distribution instances will have a System Property called jetty.home that will be the path to the Jetty Home location on disk. This is to be assumed to be where the Jetty Binaries and Distribution configurations are found.
Jetty Base
Starting in Jetty 9.1, there is also a mandatory System Property called jetty.base that is where your specific instance of jetty's configuration + libraries + webapps are housed. This is often a different directory than jetty.home.
This separation of binaries vs configuration is a core concept of Jetty 9.1, adopting a clear separation will making upgrading the Jetty binaries easy.
See http://www.eclipse.org/jetty/documentation/current/startup-base-and-home.html
I thing I have found the easy answer:
usermod -m -d /srv/jetty jetty
As it was advised in many places on the internet I created a user jetty to run the jetty service. So whatever I did -even if I set JETTY_LOGS- the log files allways ended up in the users home directory. By modifying the jetty users home directory the File(".") and the logs all end up where I wanted to
Am using jettyrunner for executing my war files.I am using command java -jar jetty runner ex.war.But am running this jetty server from my java application by executing this commands from java.My problem is at first time its working good,but the second time if i am again executing another war file with the same code its executing the older war.i have found the reason that the older jetty server is keep on running.How could i stop this server from java in order to start the jetty server for another war.
One option should be:
http://wiki.eclipse.org/Jetty/Howto/Secure_Termination
Another would be to use the ShutdownHandler:
http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ShutdownHandler.java
Or don't use the jetty-runner directly like that from java code and just write a small embedded usage:
http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
Turned out, jetty-runner.jar doesn't have a feature to stop existing jetty process ran with stop-port and stop-key.
I found the answer in https://github.com/jetty-project/jetty-documentation/blob/master/src/docbkx/administration/runner/jetty-runner.xml
Then, to stop jetty from a different terminal, you need to
supply the same port and key information. For this you'll either
need a local installation of jetty, the jetty-maven-plugin jetty-ant plugin, or write a custom class
Fortunately, I was implementing gradle build, so jetty-ant satisfied my needs.
I have a Ubuntu Server 10.10 64-bit running a web application on Jetty 6.1.24-6 on Sun's JVM, both installed from standard Ubuntu repositories.
I'm trying to track down a problem with this server (100% cpu after some time, it might be related to a known bug on NIO's Selector, although it looks like changing the connector to old io SocketConnector didn't solve the problem!), and need to take a thread dump.
Unfortunately I'm unable to get the thread dump. I've tried to send a SIGQUIT to the process, and tried to attach JStack to it, but neither approach works.
I see no output at all from SIGQUIT (in any of the log files generated by Jetty), and JStack, even when run as root (or jetty) and with "-F", says that it has been attached to the process, but then blocks and produces no more output!
How can I obtain the thread dump?
You have to do this as the same user as the jetty process is running under. On Ubuntu this user is normally called jetty.
So try
sudo -u jetty jstack <pid>
this will send a thread dump to stdout (your shell).
You can also
sudo -u jetty kill -QUIT <pid>
which will send the thread dump to jetty's stdout (normally /var/log/jetty/out.log)
To get the pids use the jps command or ps ax|grep java
did you try VisualVM (/usr/lib/java-6-sun/bin/jvisualvm) with remote connection ? It can capture a thread dump