How can I implement something like apache reload in java? - java

I have written a server application but I want to be able to edit the server's configuration while it is running and reload it into the server's memory without restarting, is there any way to do this in java other than creating a listening socket for configuration purposes?

In the library Apache Commons IO, you get for free a File monitor which you can use to know if a configuration file has been modified and handle the modification
In Java 7, you have a similar functionality in NIO2, a WatchService I think.
After that, correctly handling the modification depends on your architecture. You may have a look at ClassLoader to discard certain part of your configuration and load others

Related

Is there a way for force polling using WatchService?

I have working code that listens to a directory using the WatchService and responds to the events I specify. This works fine and has been tested on both linux and mac (although on the latter it's clear that polling is used).
However, when I deployed this in production it turns out the directory being monitored is an NFS mount. Since the WatchService uses inotify when running on linux there were never any events triggered because NFS mounts don't trigger inotify events (or something like this, there's more info here, which explains my problem: Java WatchService not generating events while watching mapped drives).
Since my code is already written I'd prefer to force the WatchService to use the polling implementation rather than the inotify one. Is there a way to do this?
I attempted this by finding the sun.nio.fs.PollingWatchService source code and creating an object directly (instead of using FileSystems.getDefault().newWatchService()) but when registering the service with the Path I got this exception: java.nio.file.ProviderMismatchException.
So, any ideas? Since I've already implemented the code using the WatchService and WatchKey API it'd be a lot easier to just force polling than rewrite everything using a custom or 3rd-party poller. Thanks!
You can try the open source project jpoller. It implements a class named DirectoryPoller which periodically poll the contents of one or more directories.
It is a periodic thread that looks for new files using the file last modification time.
To download source you can visit http://jpoller.sourceforge.net/
Honestly, I did not used jpoller. I used JDK 7 WatcherService events.
We had a similar problem and decided to abandon using WatchService. There are a few libraries implementing pooling. I would recommend Apache Commons IO Monitor:
https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/monitor/package-summary.html

Event for change in text file content

My java program needs to know if a text file content changes. Currently Im using File.lastModified() method to do it but ideally I don't want to perform the check. Instead I want an event to fire every time the file is modified. Are there any third party libraries available for this kind of thing? I've heard it can be accomplished using apache file descriptors but couldn't find any information regarding it.
You can accomplish this with the new Java 7 WatchService. This enables you to watch a directory and be notified of create, modified and delete events.
There are many factors that might determine your solution. How often the files updates, what type the information is, etc...
My advice would be either the Java 7 standard, or the Apache de facto standard (if the requirement wont allow Java 7 solution)...
Apache configuration
If it is a file that is kind of property information, a configuration, then you might want to look at Apache commons configuration. Which gives a refresh method each time the file is updated, to reload your configuration, etc. http://commons.apache.org/proper/commons-configuration/userguide/howto_events.html#An_example
Java 7, WatchService
If you use Java 7, look at the WatchService, link http://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html
Apache IO
If you dont want to use Java 7, and the file is not a configuration (which is better to use commons configuration that way), then you might want to look at FileAlterationObserver from Apache io. http://commons.apache.org/proper/commons-io/
Try Apache's DefaultFileMonitor.
All you need to do is add the file in question to the list of files that need to be monitored and start the monitoring thread.
If the file has been changed or deleted it shall fire an OnChange event.
Google say that you can use libraries like JNotify and inotify-java

Centralized Application properties for multiple system

I am looking for a open-source solutions which allow hosting different properties for different applications and allow changes. On any change of properties it should notify or push the change to the application appropriately.
So, instead every application managing the properties in physical file and deploying physically; these properties can be pushed to a single system. User will have GUI to load and change the properties as per right. Should allow push as mentioned.
If you have already similar open source solutions in mind please advice.
Is this something that Puppet can manage for you?
I don't think what you've described (as nice as it would be) will be likely to exist in an app server. If a program is looking for a file, it's either going to load it with a FileReader (or similar), or it will use ClassLoader.getResourceAsStream(). It might be looking for data that is returned in properties, format, XML properties format, or even something completely different like RDF with which to configure itself. Also many programs read their config on start-up and then hold the values in memory, so you would still need to reboot them to get them to change.
To get something like this to work there would need to be a standard for configuration provisioning and live updates. Once that existed the webapp authors and server vendors would each need to add support for the standard.
If you are the one writing the programs to be managed however, then you can write your programs to request configuration from a service, and have a configuration push feature.... there may be packages out there that can speed up adding that to your code, but I get the impression you are looking to manage programs written by others.
Have you considered to use JMX? I think he could be a good starting point to implement your requirements.
MBeans's attributes can store your application's properties, the MBeanServer will allow you to make them available from remotting, JConsole offers you an GUI to update properties values.
You also can write within the MBean some code that notify the corrrespondig application when a user change any properties using the GUI.

Can I close file handles opened by code I don't own?

I'm using a third-party commercial library which seems to be leaking file handles (I verified this on Linux using lsof). Eventually the server (Tomcat) starts getting the infamous "Too many open files error", and I have to re-start the JVM.
I've already contacted the vendor. In the meantime, however, I would like to find a workaround for this. I do not have access to their source code. Is there any way, in Java, to clean up file handles without having access to the original File object (or FileWriter, FileOutputStream, etc.)?
a fun way would be to write a dynamic library and use LD_PRELOAD to load it for the java instance you are launching ... this DLL could override the appropriate underlying open(2) system call (or use some other logic) to close existing file descriptors of the process before passing the call to the libc implementation (or the kernel). You need to do some serious accounting and possibly deal with threads; but it can be done. Especially if you take hints from /proc/pid/fd/ for figuring whether or not a close is appropriate for the target fd.
You could, on startup, open a bunch of files and use File*putStream.getFD() to obtain a bunch of java.io.FileDescriptors, then close them, but hold onto the descriptors. Later you might be able to create streams using those stored FileDescriptors and close them.
I have not tested this, so would not be surprised if it did not work on some platforms.

Auto-reload changed files in Java

I have a lot of configuration files that modify how my application behaves. I want to be able to make a change and it gets reflected in the application right away when saving the file. Is there a Java library to help with this?
I could simply keep a list of files with their timestamps and continuously check in a background thread when a timestamp changes. Doesn't seem too difficult, but maybe there's a more efficient way to do this? Custom triggers when certain properties have changed would be nice.
I'm using Spring 3.1, is there a built-in mechanism or solution which works nicely with Spring?
UPDATE: Apparently JDK7 now includes this functionality through its Watch Service API: "Most file system implementations have native support for file change notification. The Watch Service API takes advantage of this support where available. However, when a file system does not support this mechanism, the Watch Service will poll the file system, waiting for events." So this'll be my motivation to migrate to JDK7.
Edited:
http://commons.apache.org/configuration/userguide/howto_filebased.html

Categories

Resources