Auto Update Properties file value in Java project - java

I have java application hosted on apache tomcat. There are some values derived from project.properties file. But to reflect any new/changed property value, i need to restart tomcat service/application service.
So is there any way to reflect these changes on run-time / on-fly?
Thanks in advance.

Your question is very broad and does not specify if you're using a specific framework for building your application. Most frameworks have some default support for this, so if you building a plain Java application running in Tomcat you can do this by making use of for instance 'commons configuration'.
With 'commons configuration' you can setup a properties configuration with a reloading strategy:
String filePath = "/some/path/project.properties";
configuration = new PropertiesConfiguration(filePath);
FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
// 1 second refresh
fileChangedReloadingStrategy.setRefreshDelay("1000");
configuration.setReloadingStrategy(fileChangedReloadingStrategy);
A complete worked out example with Spring can be found in this tutorial.

Related

How to make configuration in Spring Boot project which can editable at production environment?

I have spring boot rest service which returns the currency list. Now, I have a requirement that this currency list should be configurable by prod support guys so that they can keep adding the currencies whenever they want.
I am from the asp.net background and I know in asp.net, web.config file is editable even on the prod and prod support guys can make the changes in that file and restart the IIS server to get the latest changes.
In spring boot if I add my currencies in a property file. Will that file editable on prod.
As far as I know Spring boot creates JAR or WAR file as package. Property files won't be editable on prod.
Config files are not editable at runtime. You'd need to stop the server, and modify any properties, and restart. If files aren't editable, obviously that won't work.
If you want to be able to modify runtime configs, use a database/dynamic cache lookup.
Other solutions include Spring Cloud Config, Zookeeper, or Consul.
Externalize config via Spring Cloud Config. This will let you update your config at runtime. And also allow you to keep a track of your config version.
Have a look at this for better understanding:
Spring boot-microservices config
-From java brains

How to load changed environment variable in spring boot application running in PCF without restarting application?

We have a spring boot application running in PCF and it reads the PCF environment variables(CF_INSTANCE_INDEX, CF_INSTANCE_ADDR,..) from an application. Based on those variables, we are trying to implement the logic for a scheduler. While running this scheduler, these variables' values could have changed. Is there a way to refresh/reload bean that have env values during runtime?
we used #RefreshScope annotation on config properties bean.
#Configuration
#RefreshScope
public class PcfEnvProperties{
#Value("${CF_INSTANCE_INDEX}")
private int intanceIndex;
#Value("${CF_INSTANCE_ADDR}")
private String intanceAddr;
...
}
and refresh using
context.getBean(RefreshScope.class).refresh("PcfEnvProperties");
PcfEnvProperties pcfEnv = context.getBean(PcfEnvProperties.class);
But It is not loading the recently changed env variable into running application. Any ideas on how to accomplish this?
You can use Spring Cloud Config Server in combination with Spring Actuator to expose an endpoint in your service that will refresh the application's properties on the fly. You could set up your scheduler to hit this endpoint on a timer or as needed.
Here is one tutorial I found that seems pretty straightforward: https://jeroenbellen.com/manage-and-reload-spring-application-properties-on-the-fly/
You may have to play with the setup depending on how your platform is configured, but I believe it should do what you're wanting. We have deployed many java web services on our PCF platform using this actuator/config server approach, and we can just make a call to the refresh endpoint and it successfully pulls in (and overwrites when necessary) the new properties and values from the config server. Also you can pull out a list of the property names and values that changed from the response.
I'm not familiar with the specific property values you mentioned, but as long as they are normally a part of Spring's ApplicationContext (where properties usually are found) then you should be able to pull in changed values using this approach with Spring's cloud config server and actuator libraries.
Hope this helps

Set application specific properties within the application

Is there any way by which we can set application specific properties within the application? Like server port. Searching solution other than Spring boot and Server.xml. I am using Spring MVC in my web application. Any help is highly appreciated.
You need to use -D option to specify the explicit configuration parameters if you are executing from command line as follows.
java -Dyour_config_param="value" -jar your_app.jar
Have a look at Spring application properties: https://www.tutorialspoint.com/spring_boot/spring_boot_application_properties.htm.
You can also have multiple properties files for your different environments such as E1, E2 or E3 but appending your properties file name like application-e1.properties and then passing the --Dspring.profiles.active=e1 flag to your application.
You can declare property sources in your application.
Here you have a Spring (not Spring Boot) specific blog entry explaining all you need to know about property management:
https://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/
Also, you can check some alternative solutions in this related question:
How to read values from properties file?

Websphere Server Profile for different node and cell name

I'm relatively new to Websphere. I have created a WAS ( Websphere Application Server 8.5) profile configuration on local, manually from scratch which contains multiple data sources, queues, connection factories and activation specifications. ( Windows 7 PC). I backed this up using backupConfig.bat. I wish to replicate this profile configuration on another system where the Websphere Application Server (installation cannot be altered) has a different Node and Cell name. Is there a way where I can replicate it , without having to manually create the resources again,one-by-one , in the local WAS profile on the new system? When I googled this, it threw up the manageprofiles command, though I am not sure if it's backup option will work in my case. Thanks in advance.
If these are simple queues, datasource etc you can use Property based configuration - see Using properties files to manage system configuration.
You export configuration from one cell and import to the other.
If you have few elements you can use WebSphere Configuration Migration Tool (Eclipse plugin), which will allow you to select graphically which resources to move - it generates jython script that you can execute on target environment.

How to secure liquibase properties (database password etc.) in springboot application?

While developing a springboot-liquibase application following this I need to specify the database username + password as liquibase.user and liquibase.password in the application.properties file. I am looking for a better secure way to use these parameter (dynamically fetched from some other place and use inside my java code)
Is there a way to achieve this?
There are couple of things you can do:
You can encrypt you properties file using jasypt-spring-boot. For more details have a look at demo app
If you are developing distributed system, then spring-cloud-config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments
Spring Cloud Config
This project allows you to use an external, centralized configuration repository for one or more applications. You don't need to rebuild your application if a property changes. You can simply change the property in your configuration repository and even push the changes to all of your applications.
See this Getting Started Guide.

Categories

Resources