Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a Java Console Program To Webapp

I have a Java class with a main() method. It contains logic to do some number crunching and analysis. Its scheduled to run once per day and may be manually run again if needed. The routine uses Log4j to log its activities. Running it and checking the log requires a remote login to the host box.

I would like to convert it to a web application, where I can trigger it through a web page and see the output of the log, ideally as it scrolls.

What would be the best way to do this? A Java webapp? A simple generic web based script runner? or any other option I may not be aware of.

Update: A bit more detail on requirements:

  1. Ability to launch the program from a web page
  2. Ability to see the scrolling output from Log4j
  3. If I leave the page and come back again, it should let me know that the last run is still running and show the log. I don't need to have the ability to run multiple instances in parallel.
like image 628
Danish Avatar asked Apr 11 '12 14:04

Danish


2 Answers

Have you considered setting up a Hudson/Jenkins server to run the task? Because it has the features you describe, it IS a java web app, and it would work without modification to your existing project.

like image 151
Kevin Avatar answered Sep 20 '22 06:09

Kevin


You could submit the form via AJAX and use Ajax with Spring MVC to poll for new log entries and add them to a table on your page. Here's some quick sample code for AJAX call to check for new log entry using JQuery and Spring MVC controller method to handle it.

JSP:

$.getJSON("logs.htm", { lastLogId: logId }, function(response) {
    $('#myTable tr:last').after('<tr><td>' + response + '</td></tr>');
});

Spring MVC Controller (returns JSON):

@Controller
public class LogController {

    @RequestMapping("logs.htm")
    public @ResponseBody String getLogs(@RequestAttribute("lastLogId") Integer lastLogId, HttpSession sess) {
        LogList logs = sess.getAttribute("logs"); // just an example using user-defined class "LogList"
        return logs.getNextLog(lastLogId);
    }
}

There are a few pieces I didn't mention here (would need to store log entries to session when log4j logs, etc..), but I hope it's at least helpful to see a way to do this using Spring MVC and AJAX.

like image 41
stephen.hanson Avatar answered Sep 21 '22 06:09

stephen.hanson