Reinitialization of spring bean during runtime? - java

I have a problem ."This is just a configuration which I would like to manage during runtime. I don't want to redeploy whole app to update configuration".I am reading some values from the property files.How can i acheive this?

There are lots of possible solutions, depending on the exact need (and this like possibility to reload these properties on demand etc.).
The simplest one seems to be this one:
Create a bean that internally has a cache defined (like Guava Cache)
Set certain TTL for the cache contents (this will cause your properties to be reloaded every TTL seconds)
Provide a way to populate the cache
Optionally provide a way to force refresh of cache contents
As for the last point I cannot give you a way to do this because I know nothing of your project, but there are at least few good options here depending on the project you are working on.

Assuming your file is not part of deployable (WAR/EAR/JAR), you can watch for directory changes and identify whether your file has changed (refer Can I watch for single file change with WatchService (not the whole directory)?). Once you notice the file has changed, you can execute your logic.

Related

How to check javafx based program run first time or not?

I want to show configuration page if its run for the first time else its show other pages. How can I implement in my netbeans project?
There are several ways to do this, but this might be the easiest:
Save your configuration at the applications startup to a persistent storage. You could use a database (e.g. h2), a file (e.g. txt/ini/config, XML, YAML, ...) or even the users registry with the Java Persistence API.
After that, on every startup the application loads that configuration and displays the appropriate configuration menu when either all or some information cannot be found.
That way you can also ensure that there is always a valid configuration in case it gets lost for whatever reason.

Java web app: Force browser to load static content (js, css, html) if deployed file changed

We're trying to force the client's browser to reload static files when they change. Currently, we have our build scripts automatically creating a new directory with the build timestamp and replace the content in the code to point to that directory.
However, I feel this is hardly an optimal solution. It forces the client browser to load every single file if a new build exists, even if only 1 file changed, and build time increases considerably by scanning every file and replace every static file reference.
I know we can also set the version when we declare files (something like < link src="blahblah.css?version=1.1" />), but this forces us to change all our code to include a version placeholder and still have our build scripts replacing it.
Is there a smarter way to do this? We're using Spring MVC. Is there any option in mvc:resources that I'm not aware of to do this without changing code? Or something on web.xml?
We're using tomcat. Is there a way to do this at server level? Would it help to use a cache like Varnish or something? Or these caches only allow to set expiry times and not check that the file changed? Bear in mind I'm not comfortable at all in server and cache configuration tasks.
I found out about this project https://code.google.com/p/modpagespeed/, but since it's far from my comfort zone, I'm struggling to understand capabilities and if this helps with what I want.
Anyone has any ideas?
Thanks
You can use version as a query parameter, e.g. /resources/foo.js?_version=1.0.0. If you are using Maven, it is not that hard to get version information from /META-INF/maven/{groupId}/{artifactId}/pom.properties. Of course this will force reload all scripts with every new version... but new versions are probably not deployed that often.
Then it is always a good practice to properly set HTTP caching headers. <mvc:resources> should correctly handle Last-Modified header for you. And you can set cache-period to make browser check the for resource modifications more often.
Here is a working solution: CorrectBrowserCacheHandlerFilter.java
Basically, when your browser requests the static files, the server will redirect every requests to the same one but with a hash query parameter (?v=azErT for example) which depends on the content of the target static file.
Doing this, the browser will never cache the static files declared in your index.htmlfor example (because will always received a 302 Moved Temporarily), but will only cache the ones with the hash version (the server will answer 200 for them). So the browser cache will be used efficiently for those static files with hash version.
Disclaimer: I'm the author of CorrectBrowserCacheHandlerFilter.java.

modifying properties-service.xml

I have a question related to the properties-service.xml. There is a value inside this file that needs to be updated every once in a while programmatically. So I have the following questions about this operation which I wasn't able to find some details about them:
Is there a way to update properties-service.xml programatically other than parsing it and changing the value? If parsing is the only way, is there an available method of retrieving the location of the used properties file?
Can we reload the properties-service.xml after the update without restarting jboss or do I need to set the property manually?
Any help is appreciated.
Thanks,
Sami
To get location of the properties file - acquire System property "jboss.server.home.dir" and suffix "/deploy/properties-service.xml" to it.
Once you save your changes to properties files, JBoss will hot-deploy it as it scans for changes every 5 seconds. If it doesn't hot deploy check value of attribute "ScanEnabled" in /conf/jboss-service.xml to confirm that hot deploy is enabled

Is it possible to track resource read-only property in Eclipse?

I am working with eclipse resources right now and interested whether it is possible to handle file read-only property change ? For example user changes file read-only property outside application and then I can handle this property change event in my application.
I don't think, it is possible to do it automatically, as Eclipse resources do not synchronize all the time with the file system. More specifically, file changes do not trigger events in the Eclipse resources directly. Files are refreshed only when the resources are read.
Maybe if you are refreshing continously, it would be possible, but that can have quite an overhead. After a refresh it is possible to listen to changes in resources - thats what Builders and resource listeners are for.
To be more precise than Zoltan's answer:
No, it is not possible to do this directly. However, it is possible to periodically refresh the workspace and look for changes that you are interested in.
Create and schedule a Job that runs every XXX seconds. This job will run IProject.refreshLocal(IResource.DEPTH_INFINITE, null). After running this operation, it will re-schedule itself to run in another XXX seconds.
Add an IResourceChangeListener that listens for the changes you are interested in.
Now, if the Read-only changes happen from inside the workspace, you will not have to do #1, and #2 (the resource change listener) will run automatically.

EHCache Disable/Shutdown

Is there a way to disable ehache externally using a property file?
CacheManager.shutdown() doesnt seem to work?
Actually we have 2 app with the same source code i require ehcache in one and not the other.
one where i dont need cache is a webapp!
Unable to figure to out yet how to go about this?
Setting this System Property to true disables caching in ehcache. If disabled no elements will be added to a cache. i.e. puts are silently discarded.
-Dnet.sf.ehcache.disabled=true

Categories