Running programme to monitor file infiteley - java

I am trying to run infinitely a program that monitors a logfile and updates to the database the errors that have occured and keeping track of a max number of occurances for some error so to send an email if one error is occuring too many times. I managed to do the programme that listens and updates appropriately and sends the email but I don't know how I am gonna be able to start off the programm to run infinitely as it will be monitoring a live system. On the other hand I have a web UI that displays the info updated by the monitoring programme, in other words I don't want the updating programme to stop running even when I my web UI is closed in the browser, behind the scenes I want updating programme to continue to run. I want to separate them so they run independent of each other. I am using Servlets and JSPs for my front end. I was thinking of starting the updating programme in my servlet but I am thinking when it is closed the updating programme will stop run which is what I am trying to avoid. Any kind of advice is highly appreciated, any tool available that allows running a programme infinetely and only stops when server is down and thanks in advance.
NB: I wanted to use log4j to append errors to database to allow me easier
updating but the system wasn't designed to accommodate my requirements so
I have no choice but to use this approach.

Assuming that the Web UI displays information retrieved from the database and if you want to separate out the web UI from the monitoring program, I would suggest keeping a separate native Java process running that constantly monitors the log file and updates the database. The database will be used as a communication medium between the two Web UI and the Java process.
That way the Java process will remain independent of the Web UI and can run indefinitely.
Think it this way -
1. Java process updates the database as it monitors the log file
2. The Web UI reading from database whenever it is up.
The Java process can probably also communicate with Web UI service to provide some heart beat to acknowledge that it's up.
Hope this helps.

Related

How to get information from a Java Process?

So, say I have multiple instances of a java program running, and I need to get information from each instance, ie if the program is performing its function correctly and if its not I need to get information to the process on what to do, I essentially need to open a tunnel. How would I do so? I hope this question isn't to vague.
I'm basically writing a manager, the manager loads a bunch of clients and I need a way to communicate between the manager and each client.
Have you looked into Java Management Extensions? It's built-in and made for monitoring java processes:
http://openjdk.java.net/groups/jmx/
if you need to look at custom info for your processes (some state of execution, or some special log info), you can create your own info-providers (aka "MBeans"):
https://blogs.oracle.com/WebLogicServer/entry/developing_custom_mbeans_to_ma
hope that helps

Running a Thread What Happens after Page Refresh(Runaway Thread)

So I have a Grails App which uses a java jar. Through the grails app the java program can be run. It can also be stopped and paused. My problem is what happens if the user refreshes the page. The thread become a runaway thread and can be accessed. It will keep going until it finishes. Problem being it writes to a database.
My controller just initializes the java program and controls it, using commands from the gsp.
Is there some way of stopping this or is there a better way of doing it?
Any help is much appreciated,
Use services.
You need to put your database code insite a service and call this service in your controller.

How to run Apache-Solr server as background process?

I am using Apache-Solr for indexing and searching in my java application.
If want to perform any operation related to Apache-Solr then the solr server is must to be started.
Sometime the terminal in which Solr-server is running is closed accidentally then we have to start that server again, because we can not perform any Search/Index related operation.
I tried to run process in background by putting '&' at end of the command, but it does not working.
I do not want to that terminal is opened in which server is running. So is there any way to run the Apache-Solr-Server in background process?
Thanks in advance..
i think this link will be useful for you it talks about how to use screen if you are using linux and other stuff
start apache-solr-server in background
also you may start
Solr automatically by creating an init/LaunchDaemon script and let the
system handle running it.

Web applications and multi-threading

I'm working on porting a desktop application (WinForm) to a web application (Java/Spring/JPA). The problems are many and I'm struggling a bit...
Now the problem is threading!
In the original application, that performs the export of certain data from the DB, there is a progress-bar indicating the progress of the process.
I want to port this progress-bar in the new web application. To do this I thought of using AJAX and use a separate thread to run the data export.
The main concerns are:
Am I following the right approach? Are there problems using multi-threading in web applications?
If during the export process F5 or refresh button are pressed what exactly happens? How can I stop the process?
How do I update the progress bar periodically? Do I have to make calls via ajax to the server?
I'm primarily an ASP.Net developer but from what I know of the HTTP protocol this just isn't the way to go about it. I've seen a lot of fairly clever solutions for this but in the end what becomes clear is that the HTTP protocol simply isn't designed to work like this.
Obviously you're aware that a flash or silverlight app would be able to do this but that comes with it's own set of issues.
Myself I prefer to keep all the weirdness on the server. In the past I've had to come up with a way to deliver several thousand emails through a web application and update the user on how it's coming along. I designed a set of tables to act as a queue. The web application would simply place any delivery requests in this queue and the progress bar would be determined by a request that checks the status of the items in the queue. Running in the background was a windows service which would also check this queue and was actually responsible for delivering the mail and setting the status of each item as it completed or failed.
It was a bit difficult to develop since windows services can be tricky but once it was up and running it was extremely smooth and reliable. Depending on your circumstances perhaps a simple scheduled task set to run every few minutes would do the trick for you.
I wouldn't necessarily jump straight to running a separate thread explicitly for the export. While it would be ideal to do this, the capability of the web container to do this is going to be a limiting factor. Your traditional Java EE app server generally discourages spawning threads for this (though you can hook up to a thread pool for this). Some containers are great at freeing up the threads from blocking until the work is done (Karaf with Jetty and Camel, for instance) so that they can service other web requests while the export is occurring. But my guess is that you're probably okay with the "start export" thread blocking until it receives a response.
How long does this export take? A couple of seconds, or are we talking closer to minutes here? If it's shorter, I'd think that just putting a little "Waiting" icon with the little circular spinner on it (using your favorite Ajax library, whatever that is) would be sufficient.
If you really want a true status bar that periodically refreshes itself, then yes you'd have to poll for it at some frequency. Presumably that could be a simple request that would load some kind of progress for the job from a database table for that job ID.
Find my answers Inline
I am following the right approach? Are there problem in using multi-threading in web applications?
-Yes you are on correct path. No there is no such problem in multi-threading in web application and its as easy as you do it in WinForm. Instead of using Dispatcher to update the UI, you would be making AJAX calls and with javascript DOM manipulation would take place.
If during the export process F5 or refresh button are pressed what exactly happens? How an I stop the process?
-Unfortunately there is no easy way. The standard way is, when such kind of processing is done and the user hits F5, you would show a dialog(with help of javascript) and inform user that the job is still running. If the user still wants to refresh then you have make another request to the server for cancelling the task.(You need to store thread id or cancellation token some where to cancel the task)
How do I update the progress bar periodically? Do I have to make calls via ajax to the server?
-The standard way is, generally you show show a loading image. IF you want to show a context senstive progress bar, it would mean you have to do polling. Here is an example by Dino Espito. Though its in ASP.NET, you could understand the underlying principle
Dino Espito

Detecting/controlling a process

I have a java application where I want to check to see if an application is running. If it is not running, I want to start it. If it is running, I want to kill it and then restart it.
Can someone tell me how to do this? I can start/stop the program easily enough, with the ProcessBuilder. But I cannot detect a process that is already running.
Thanks for your help!
John
Without the cooperation of the application (ideally have it listening on a network port), that may be impossible (your Java app might not have the rights to kill the app) and requires OS-specific code. On Linux, you'd uase the ps and kill commands.
Might sound silly, but you can create a file with a known name on application startup. Doesn't need to contain anything. To check if your application is running, check if that file exists.
Is the other application (the process you are monitoring) under your responsibility? If so, you can use a method I used in some high-availability system a few years back:
Open a connection to that other application and "ping" it. If the other application does not respond within a given timeout, it is either down or "hung", which is as bad (and something you can't detect through process monitoring.
Opening a connection can be done using sockets, or though more sophisticated protocols (SOAP?).
An alternative is to have the application send a "I'm alive" message every so often. If you haven't received it in some time - your application needs restarting.
In order to actually "kill" the other process, you can keep the Process instance you get from the exec() method, and destroy() it when you so choose.
Thanks for the replies. That was what I was afraid off. We are trying NOT to add more things to the application that I want to start up. We are basically trying to add a remote control web interface to a collection of applications.
The web server application that I am writing would basically start/stop 3 apps that all talk to each other to achieve a goal. If my web server starts and stops them, all is well. But if, for some reason they are already running when I try to start them bad things happen.
It is something I know I could handle with Visual Studio (C++/C#/etc). But this project has to be written in java due to a platform independence requirement.
Thanks for your help everyone!

Categories