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
Related
Question: I'm looking for a way to configure Java to create new files with a particular permission set by default.
Problem: I have a Spring Boot app which uses the following:
Log4J2 for logging
H2 for flat file databases
Ehcache for cached entities
All of these libraries create new files on the local file system, and when they do, they produce world-writeable files (666 for files and 777 for directories). I have seen this on macOS 10.13 (user has "umask 0022") and on Amazon Linux (user has "umask 0002").
If I was directly managing the creation of the files, I can do what I need with PosixFilePermission, but since file creation is delegated to the libraries, I don't have that opportunity. I could potentially set a timer to discover new files and set the permissions directly, but I'm not wild about that approach.
Log4J2 v2.9 added a filePermissions field to RollingFileAppender, so I have hope for one of my problems, but I'm not able to find something similar for H2 or Ehcache. Ideally, I'd like to do this at the JVM/Boot level for simplicity and future-proofing.
Here's a topic of tomcat and umask. Seems tomcat has it's own behavior dealing with umask.
So maybe there is a way to config the 'umask behavior' of tomcat embedded in Spring Boot? Like properties or something.
I cannot pretending this is an Answer. But sadly I don't have enough reputation to comment your question. Hopes this would help you a little.
Turns out this is a red herring. The issue is not with java, it's with the YAJSW service wrapper that launches the java process. YAJSW has several parameters for setting umask, including on the child process, but they are not implemented yet. Launching the app outside of YAJSW produces files that obey the user's umask.
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
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.
I have a typical web application deployed in Tomcat. The requirement is to provide incremental update way instead of full-package delivery (a war file) when update the application.
For example, once I finish a bug fix which changed a jar file, an XML file and jpg file. I call these 3 files as a patch. I am supposed to deliver the patch file. Even when customers want to rollback to original version, I have to provider a way to rollback the patch.
All the process is supposed to automatically.
From my perspective, the requirement doesn’t make sense. full-package delivery is easy and reliable way to update a web application, I don’t want to introduce complex and error-prone way to update.
Do you have idea to implement incremental update requirement? Thanks!
When you deploy the .war or .ear, the application server usually unpack it into an internal directory. You can change files in this directory directly, with a finer granularity. However, for changes to take effect consistently, you will need to restart the server.
Your perspective is indeed fully correct. Nowadays, sizes of files don't play a significant role, I don't see the problem with whole updates. Why isn't the customer happy with whole updates?
Note: If what he wants is dynamic updates, i.e. without restarting the server, then this is anyway a complete different problem, and mostly impossible for production systems in java (but doable during development, with solutions like JRebel).
You can create a Java Program that uses Delta-Sync protocol i.e. Only those files need to uploaded which are updated. If you have used Dropbox then you will understand pretty well.
Dropbox uses Delta-Sync protocol to update file and sync data.
Either way for time being you can use Dropbox by installing on your client (mapping to server's WAR folder) and your local machine and share that folder. Then whenever you change the files in your local machine it will automatically upload and sync those CHANGED (PATCH) files to your client's machine.
I want a simple file format to store and retrieve data from disk in Java.
name=value
list=value1,value2,value3
this is mostly going to be used for initial config settings used at startup of the app. I could invision having a watcher on the file to notify the app if it changes so the new settings can be applied potentially but that would be a nice to have. The first part would be pretty easy to write. I just don't want to reinvent the wheel if something is already out there for this and I'd prefer to avoid something as heavy as spring.
Take a look at the java.util.Properties class.
Properties
You can use the Preferences class. It has a notification system, but alas it doesn't notice changes made outside the running JVM or directly to the underlying configuration store (e.g. the config file). It's a really nice class though.
Have a look at OWNER API.
It incorporates most of the feature of java.util.Properties and adds more.
Version 1.0.4 is under development and it will have:
support for arrays and collections (list, set, arrays). It is already implemented on master branch.
"hot reload", when you change the file the config object gets reloaded (it can be synchronous or asynchronous and it does support event notification for reload). Already implemented in master branch.
a lot of features (variable expansion, type conversion). Available since version 1.0.3 and available on maven central repository.
Also for 1.0.4 is planned a validation mechanism that will check the file to be compliant before discarding the old content of the config file during the reload. (not implemented yet)
If you need some particular feature, just ask on github issues or become a contributor.