Is there a Java equivalent to a windows service [duplicate] - java

This question already has answers here:
Run Java application at Windows startup
(8 answers)
Closed 6 years ago.
I know you can run a jar as a windows service by calling it from a batch file or something similar to that, and I know that when you use an application server such as WAS, or Glassfish you have JVMS's that are "running" per-se. But unless I am mistaken, those aren't necessarily "services". That is to say processes that are executing without input from any user. From my understanding you still need to call a JVM to get it to execute a process (and please feel free to correct me if I am wrong). But I have always been curious as to what is the Java/Linux equivalent of the Windows Service? Maybe it is JVM's running in an application server like Tomcat, or Glassfish.

A Windows Service is basically a background task that runs a process for you. In Windows, they are typically binary's (.exe's) that can hook into the Windows Service platform for startup and shutdown messages from the OS (and the services administration screen).
Anytime you execute Java, you need runtime component (JVM) to run it. Even Websphere and the other app servers are running in a JVM.
There isn't anything in Linux for you to hook into a "Service" per say, but you can emulate the functionality and run startup items in Linux at boot time.

It depends on the exact flavor of Linux being used, but the conceptual siblings to a Windows Service (i.e. a long-running daemon that the OS starts when the OS is started) are upstart, systemd, etc.

Related

Java, JVM internals

I am running Eclipse, Tomcat, a servlet based webapplication my windows 10 machine. I was wondering how many JVM instances will be created?
Also, When will JVM be instantiated in windows 10 on startup or on starting any java based app.
One per process. See the Processes tab of your Task Manager.
When starting a JVM process (typically once per app, but could be more if it forks).

When launching Spring app (STS) with embedded Tomcat, both java.exe and javaw.exe processes are created on the same port

As the title says - when launching Spring app (STS) with embedded Tomcat, both java.exe and javaw.exe processes are created on the same port.
That means I have troubles with busy port, even when using the Relaunch option (which typically shuts Tomcat down and then restarts it). Indeed, relaunching does stop one of the two processes but then I get an error stating that the port must be busy. Killing the remaining one does solve the issue but I life is too short to both develop in Java AND kill the process manually.
Is there a convenient way to overcome this? I see that is STS (and Eclipse too, of course) there's a way to specify project specific JRE but I'm unsure on how to proceed.
Note: I have both 32- and 64-bit java8 instances installed.
Try running it as a java application (ie, just run main).
It sounds like you are deploying it on Tomcat, while at the same time there is an embedded Tomcat being started... :)

run separate background process on java web server

Basically I want to use a jsp web page, so in Java, to run(manager) different background process (could be anything that runs) on a linux server.
They need to be run as different user than the web itself.
I wonder what options do I have?
I just found out that
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("linux command");
may work.
But I don't know whether the (child?) process just started will be completely detached from the java servlet process? And is it possible to run it under different account?
Using Runtime in a supposedly lifelong running Java EE web application is a bad idea.
First and foremost, a new process created by Runtime will by design allocate as many new heap memory as the currently running Java environment. This may not necessarily harm inside a simple Java application which uses by default 64MB or something, but in a Java EE web application which ususually allocates memory in terms of gigabytes, this is going to be a complete memory waste.
Second, you just don't want so spawn unmanaged processes/threads inside a Java EE web application. What if the process/thread stalls and/or runs forever which can cause unability to shutdown/restart the Java EE web application when necessary (you'd need to kill it altogether first)? What if the process crashes and takes down your whole Java EE runtime along it?
Last, you cannot change the user running the process. It will always be the same user who has executed the currently running Java runtime.
You have basically 2 options:
Don't use Java for this at all. For example, just do the job using platform provided background job manager, such as Cron in Unix based platforms and Task Scheduler in Windows based platforms.
Do it in 100% Java. Perform the same goal using pure Java, without the need to spawn a process. You can if necessary manage background jobs using ExecutorService API or a 3rd party library like Quartz. Note that even those jobs have still to run 100% pure Java code.

How to start a application without getting killed after log off?

I have a Java application which should run on a server machine in the background (the application can be started by a command without GUI). The problem is: When I log off from the server, the application gets killed. I'm looking for something similar like nohup under Linux. I found some solutions, but I'm unsure, what is the best for my situation (Windows Server 2003, Java Application run from BAT-Skript, restart after booting the machine)? What are the pros and cons of the solutions?
psexec: Do the process really need to run under the SYSTEM account?
Combination of instsrv and srvany: But srvany should should never be used in a production environment
I have started the application with the scheduled tasks with the option run as NT AUTHORITY\SYSTEM. But after log off, the application was still killed. Can I get this working?
The DOS Task Scheduler AT command
Write a windows service (on Java, C# - I don't think it does matter)
Start the app from your service
or
Run your app as a window service using 3d party utility
The only way in which you can get a process to run without a user being logged in is to have the process run as a windows service or at the very least called by a windows service.
Just to add to above answers.
Consider using http://wrapper.tanukisoftware.com.
It's rather mature and popular (in contrast to ServiceEx and RunAsService).
Yes, a Windows Service is definitely the way to go but there are a few things to watch out for when you run a Java application as a Windows Service. Most of them are covered in this tutorial showing how to setup a java application with our commercial run-anything-as-a-service application, AlwaysUp:
http://www.coretechnologies.com/products/AlwaysUp/Apps/RunJavaApplicationAsAService.html
Beware: You will almost surely need the "-Xrs" flag on Windows 2003 to prevent the closing-on-logoff behavior but things can get tricky if you are catching shutdown events. Let me know if that is an issue for your situation.

Jboss/Tomcat monitor

I'm looking for a tool (in linux) that can montior a tomcat/jboss process and if the process fails, I can respawn either or both without having to manually ssh into the box, do any house keeping and then start them up again. I'm not too sure if there is a good tool out there that can monitor the health of jboss/tomcat and report on it's performance. I know jvisualvm gives you various tools, but I'm looking for a disaster recovery solution that is a bit higher level than jvisualvm.
Java Service Wrapper is an application that wraps your Java process and installs it in the system using service (Windows) or daemon (Linux). It pings the VM periodically and restarts it when it does not respond. Worked for us in production with several application, including Tomcat, JBoss, Mule, etc. Actually Mule ESB is even bundled with this application in the distribution.
Also you don't have to run the application manually when the system starts.
I'm currently working on a daemon to do this and more, since JOPR nor naggios didn't do what we needed, but those are good tools you could use. I'm not sure but JOPR (or whatever is called today) can restart your servers in case something goes wrong.
A custom made solution as we're working on shouldn't take you more than a week. The main problem, is that to start either JBoss or Tomcat you have to call the startup scripts. But the startup script will restart the service if the exit code is 10, something like this:
while $? -ne 10; do
start_jboss
done
So, this daemon which is made in Java uses JMX to connect to the JBoss server and tells JBoss to go down and exit with status code 10 using a method in a MBean. I'm at home, so I'm not sure the exact name of the MBean you have to call for this but I'll provide more info tomorrow.
I am using monit to control the launch of Tomcat/JBoss.

Categories

Resources