How to use Apache Commons Daemon with Cross-Platform Service? - java

I have a Java daemon (system service running 24/7/365) that needs to deploy as an executable JAR, however I would like users (on Windows, Linux and Mac alike) to be able to start/stop the application via:
service myapp start
service myapp stop
(Or whatever is the equivalent for Windows/Mac services). Obviously, this requires something at the OS-level to map the myapp "service" to a particular method call from inside my Java app (perhaps, LifecycleManager#start() and LifecycleManager#stop() respectively).
I heard that Apache Commons Daemon can be used for this purpose, and after perusing their site, it looks like it does just this. However it looks like it's an old project and there really isn't any documentation for accomplishing what I am looking for.
So I ask: can commons-daemon do what I need, or do I need something else or in addition? How can I get a cross-platform daemon out of an executable JAR? Thanks in advance!

Yes, Apache Commons Daemon can run your jar as a service on Windows (using procrun) or as a daemon on *NIX (using JSVC). I only used it on Windows, so the rest only applies to procrun:
Procrun supports proper Windows service shutdown (it can invoke a stop method in your running application). It also has a little bit of extra functionality like optionally redirecting your stdout and stderr to separate log files, and running the service wrapper exe directly (assuming you renamed it to your service name) runs a non-service mode console that lets you see the output immediately. You probably want to use procrun's "jvm mode". Read the details of how to do that in the documentation.
If you need additional fancier capabilities, such as if the service needs to be able to restart itself, take a look at YAJSW (Yet Another Java Service Wrapper) instead. YAJSW is quite likely better overall, but I haven't tried it yet.

Related

Run Java app on Mesos without Docker

I'm just figuring out Mesos and testing it on a vagrant node.
While in the process of checking out mesos, my work ran into the JVM memory leak in Docker issue posted here -> https://github.com/docker/docker/issues/15020
I would like to know if there are some tips or examples where we can test the containerization on Mesos to see if the memory problems are specific to Docker Containerizers. So far most of the examples are for marathon running docker, but not much else on non-docker Mesos.
Do I need to write my own Mesos JVM Containerizer Isolator?
Do containerizer just parse job details, and executes a script?
Can I pass the Mesos containerizer a .war and job package and have it run my Java App?
Any simple samples or specific code on large github projects that illustrates this?
My work is deciding if we should deploy our Java apps without containerization or if changing a containerization solution may help resolve the issue.
Any tips or links to documentation I can check to look at how to setup a Tomcat JVM app on Mesos Containerizers? I am still learning how mesos works, so anything helps!
So let me step back a bit: you don't run a Java app on Mesos, you run it on one of its frameworks, such as Marathon (for long-running tasks such as a webserver) or Chronos (for scheduled batch jobs). Think of Mesos as the Linux kernel of a distributed operating system, that might help.
Coming back to your question, and unless you plan to write your own framework (for example tobilg/mesos-js-framework), the following assumes you use Marathon to launch Tomcat (since it is a long-running task).
Marathon, as most frameworks, doesn't do the heavy lifting, it leverages Mesos core features such as the Fetcher to pull resources necessary to execute an app into the sandbox on the Mesos Agent. Via uris you can specify a list of URLs that Marathon (via the Mesos Fetcher) downloads and, depending on the file extension, also extracts before it launches the app specified via the content of cmd.

Starting a netty application on a linux server

I have written a little netty server application, packed in a jar file that I want to deploy on a linux server.
Since I have no professional experience with deploying java applications, I was wondering if it is enough to start the netty server by doing:
java -jar NettyServer.jar NettyServer &
Obviously a script could be created to ensure the correct user starts the process etc., but is this the way (stand-alone) java services is being deployed?
It seems almost too easy, considering every other question/answer seems to mention some big hunky container-bean-glassfish-tomcat-whatnot (which I might consider later on if/when issues arise)
yes thats the way - no container needed!! I built a middleware (http://sourceforge.net/projects/serviceconnecto/) using netty as underlaying framework. It's the way i start my server as well! Just verify the classpath is set correctly - meaning libraries are in correct place and the jar archive is correctly built.
I personally prefer Upstart to start services on linux. http://upstart.ubuntu.com/
It is very easy to use, and can also restart your application on crash.
I hope it helps.

Java daemon under Linux: Choices?

I have a console application (written in Java), which should run on a Linux machine until it is stopped.
Logging is done by the application itself.
The application needs to be stopped whenever a new version is available (i. e. I login, stop the application, copy the new JAR file, and then launch it again).
What are the options for implementing this, apart from those specified below?
Known ways to do it:
1) Tanuki service wrapper
2) nohup java -jar myapp-1.32.jar &
I use Java Service Wrapper, but you already mentioned it. I think it should suit your needs.
Apache Commons Daemon is also popular: http://commons.apache.org/daemon/, but I never used it.
I'm using this startup script: http://shrubbery.homeip.net/c/display/W/Java+Daemon+Startup+Script
[Changed domain name - by wiki owner]
Some modern Linux distributions have switched to upstart. That's a daeomon starting and stopping all the other services. I'd definitely look into that. Since it solves some tricky problems with production ready start scripts. The downside is that it has no java specific functionality.
There also the apache commons deamons http://commons.apache.org/proper/commons-daemon/jsvc.html
Jsvc is a set of libraries and applications for making Java applications run on UNIX more easily.
Jsvc allows the application (e.g. Tomcat) to perform some privileged operations as root (e.g. bind to a port < 1024), and then switch identity to a non-privileged user.

How do I log every time I start an application in Windows?

I'd like to log or record every time I start an application to gain insight into which applications I use most on my Windows system. I was thinking I could create an event in the event log and listen for it in a .Net program.
Questions:
Is this the best way to solve this problem?
If so, which .Net library should I use?
I am also open to using Java to solve this problem. Thanks!
In .NET, you could probably create a shell extension which you would register for EXE programs which would really just be a filter for the EXE extension. When your shell extension is called, you would execute the program (by invoking the old functionality) after you logged your information.
Note, however, that you can ONLY do this with .NET 4.0 or above, which is currently in beta. Because of the way that previous versions of the CLR worked, only one version was allowed to run in a process at a time (including explorer, the OS process).
.NET 4.0 introduces Side-by-Side (SxS) CLR instances within the same process, so it is safe to use it from .NET 4.0 on as a mechanism for shell extensions.
It will also require a good deal of COM interop, but it can be done.
In regards to LWoodyiii's comment asking if this can be done in older versions of .NET: Could it? Yes, it can be done, but the official decree from MS is that you shouldn't. The reason for this is because if someone else decides to run a shell extension, or interface with the OS in some way using .NET, and the version is different from the one that you are using, you run the risk of hosing the OS process.
maybe you can try to hook the CreateProcess API in system wide using unmanaged c++.
and in C# use .NET interop to handle events/notifies from you unmanaged hook module.
related links:
http://www.codeproject.com/KB/system/hooksys.aspx?msg=1322916
http://www.madshi.net/madCodeHookDescription.htm

Best Method to run a Java Application as a *nix Daemon or Windows Service?

I am looking for the best method to run a Java Application as a *NIX daemon or a Windows Service. I've looked in to the Java Service Wrapper, the Apache Commons project 'jsvc', and the Apache Commons project 'procrun'. So far, the Java Service Wrapper looks like it's the best option... but, I'm wondering if there are any other "Open Source friendly" licensed products out there.
I've had great success with Java Service Wrapper myself. I haven't looked at the others, but the major strengths of ServiceWrapper are:
Great x-platform support - I've used it on Windows and Linux, and found it easy on both
Solid Documentation - The docs are clear and to the point, with great examples
Deep per-platform support - There are some unique features in the window service management system that are supported perfectly by service wrapper (w/o restarting). And on Windows, you will even see your app name in the process list instead of just "java.exe".
Standards Compliant - Unlike many ad-hoc Java init scripts, the scripts for service wrapper tend to be compliant with LSB standards. This can end up being very important if you ever want high availability management from something like Linux Heartbeat/HA.
Anyway, just my 2 cents... :)
Another option is WinRun4J. This is windows only but has some useful features:
32 bit and 64 bit support
API to access the event log and registry
Can register service to be dependent on other services (i.e serviceA and serviceB must startup before serviceC)
Its also open source friendly (CPL) so no restrictions on use.
(full disclosure: I work on this project).
Are there any special attributes that you need to apply (like OS guided resource management) that you need to support? Otherwise, for Unix you should be able to daemonize your application by writing an appropriate init.d script and setting your app to start automatically.

Categories