aspose file tools*
The moose likes Java in General and the fly likes Creating CLI for java daemon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating CLI for java daemon" Watch "Creating CLI for java daemon" New topic
Author

Creating CLI for java daemon

Michael Golightly
Greenhorn

Joined: Feb 01, 2011
Posts: 27
I have a java daemon that is running constantly. It periodically gets data from other sources and process them autonomously. I would like to create a command line interface for the application that I can start up at any time while the daemon is running. I'd prefer to accomplish this without spawning a new JVM if possible.

I do have some ideas in mind, but I wanted to get some other opinions too.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If the program doesn't start from a console, or if you want to communicate with it from some console other than the one from which it start, you'll have to launch some process to communicate with it. If, for whatever reason, you're dead set against that being a Java process, then one simple approach would be to use telnet. Your deamon would be listening on a port for connections. You telnet to that port, you send it a command, it receives it, processes it, and perhaps sends a response.
Michael Golightly
Greenhorn

Joined: Feb 01, 2011
Posts: 27
Just wanted to give an update to anyone who might look at this thread:

I posted the same question on the ubuntu forums and received some help there:
http://ubuntuforums.org/showthread.php?t=1923518

I also ran into an issue using AJAX with JQuery along with it, which I posted on the JQuery forums:
https://forum.jquery.com/topic/problems-with-ajax-call

Short version:
Instead of writing a command line interface into the daemon, I realized (with help from the ubuntu forums) that I could just have the daemon listen for HTTP requests and create a webpage that would query the daemon like it would any other web server using ajax. The biggest problem is the fact that the two programs run on separate servers, which caused some issues with the AJAX call (which is what the question on the JQuery forums was about). I got past that using a simple PHP script on my existing website (which I used to test and make sure my AJAX call was working), so now I need to get my daemon to respond similarly.

On my daemon, I am receiving the HTTP request:


I need to parse the request in order to determine:
1) The type of request (GET, POST, etc.)
2) The command (somecommand)
3) The arguments to the command (jsonp_callback=... etc.)

I can parse this out manually, but I'd prefer to use an existing library if possible. Can anyone make any recommendations for a good HTTP request parsing library?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

There's an HTTP server built in to Java 6 JRE's. Here's the API: Java™ HTTP Server .
Michael Golightly
Greenhorn

Joined: Feb 01, 2011
Posts: 27
Paul Clapham wrote:There's an HTTP server built in to Java 6 JRE's. Here's the API: Java™ HTTP Server .

Thanks, I'll look into that.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Beware, however, that the HTTP server that's built in into Oracle's JDK is not part of the public API of the JDK.

See Why Developers Should Not Write Programs That Call 'sun' Packages

Since it's not part of the public API (it's an internal thing in Oracle's JDK), there is no guarantee that this will continue to exist in future versions of Java, and Java implementations by other vendors will not have the same feature.

Java does have a standard API to work with REST webservices, called JAX-RS. The reference implementation of this API is Jersey. This can be used to call REST webservices as a client as well as offering them as a server. See the Jersey user guide.

When you use JAX-RS you'll not have to deal with the low-level details, such as parsing REST URLs yourself and creating a JSON request; Jersey will do all of this for you. If you have things set up, the only thing you'll need to do is put some annotations on the methods that you'll want to expose as a RESTful webservice.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Michael Golightly
Greenhorn

Joined: Feb 01, 2011
Posts: 27
Jesper de Jong wrote:Beware, however, that the HTTP server that's built in into Oracle's JDK is not part of the public API of the JDK.

See Why Developers Should Not Write Programs That Call 'sun' Packages

Since it's not part of the public API (it's an internal thing in Oracle's JDK), there is no guarantee that this will continue to exist in future versions of Java, and Java implementations by other vendors will not have the same feature.

Java does have a standard API to work with REST webservices, called JAX-RS. The reference implementation of this API is Jersey. This can be used to call REST webservices as a client as well as offering them as a server. See the Jersey user guide.

When you use JAX-RS you'll not have to deal with the low-level details, such as parsing REST URLs yourself and creating a JSON request; Jersey will do all of this for you. If you have things set up, the only thing you'll need to do is put some annotations on the methods that you'll want to expose as a RESTful webservice.

I was looking into using Jersey last night, but I haven't had a go at the coding yet. I'll look into using JAX-RS as well. Do you have any other recommended links for setting up Jersey/JAX-RS in a java application? I'll read through the user guide and if I have any issues I'll do a google search, but if you happen to have any off hand, that would be very helpful. Thanks!
Michael Golightly
Greenhorn

Joined: Feb 01, 2011
Posts: 27
I set up a simple Jersey server and created a resource to run a command through to a simple daemon thread that I created. Unfortunately, because I can't seem to pass java objects to the constructor of the resource, I can't access the daemon thread(s) unless I make static calls to them. Is there a way that I can get around this necessity? Ideally, I'd like to be able to pass various Runnable classes (that are doing the work in the daemon) into the DaemonControlInterface and have that class call the various commands on the various other threads running in the daemon, with the various resource classes calling methods on the DaemonControlInterface class. If you know of a better way to implement this, I'd probably prefer it over this.

Here's the code that I have:
Main.java:


Daemon.java


RefreshConfigResource.java


DaemonControlInterface.java
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating CLI for java daemon