Spring spel expression not working, for boolean value - java

I have my application config have a property like
#Value("#{new Boolean(environment['spring.profiles.active']!='dev')}")
private boolean is24;
Now in two runtime env application-prod where VM variable is set like
-Dspring.profiles.active=prod
and an env application-dev where VM variable is like
-Dspring.profiles.active=dev
but the value of is24 is always coming as true. Any idea how can I fix it?

While I posted a comment how to achieve a goal you might have, this doesn't answer your question, and so here is the answer:
Your Spring Expression is (if working correctly) checking the content of the environment variable spring.profiles.active, while you are setting the active spring profile with a Java system property. Those two do not automatically reflect each other.
You can set the Spring profile both via environment variable and System property, but this is not a general behaviour of environment variables and System properties.

Related

How to get and set the value of networkaddress.cache.ttl

I have a java application running in an alpine container. I would like to find a reliable way to determine what is the value of networkaddress.cache.ttl. I tried to use this in the code of the application: java.security.Security.getProperty("networkaddress.cache.ttl"); but it always returns null, even when I use -Dnetworkaddress.cache.ttl=30 in the JAVA_OPTS arguments. So I am doing something wrong obviously but I don't know what. According to this
networkaddress.cache.ttl null in openjdk I don't even need to add the argument in JAVA_OPTS because the default value is 30 anyway. Looking at /usr/local/openjdk-8/jre/lib/security/java.security inside the container I can see #networkaddress.cache.ttl=-1 but it is commented out, so I have no idea what the default value is. So how can i reliably check what is the value of networkaddress.cache.ttl?
For reasons unknown to me -Dnetworkaddress.cache.ttl=30 fails to set anything. I managed to set it by adding this to my Dockerfile
sed -i '313,313 s/^#//' /usr/lib/jvm/zulu8-ca/jre/lib/security/java.security
sed -i '313,313 s/-1/30/' /usr/lib/jvm/zulu8-ca/jre/lib/security/java.security
Another way it can be done is described here. The idea is to use another file for overriding the security property.
Both approaches worked fine for me.
According to the [JDK 11 docs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/doc-files/net-properties.html):
Since these 2 properties are part of the security policy, they are not set by either the -D option or the System.setProperty() API, instead they are set as security properties.
A note from AWS Java SDK uses SecurityProperty instead of SystemProperty as in:
java.security.Security.setProperty("networkaddress.cache.ttl" , "60");
so you could use
String ttl = java.security.Security.getProperty("networkaddress.cache.ttl");

Java HotSpot VM Command-Line HeapDumpPath option use hostname for resulting .hprof

Summary: Id like to alter the way .hprofs are named when automatically created to incorporate machine's hostname + process PID
Long version: According to documentation (https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/clopts001.html) you can pass an option to the VM to create heapdumps under certain conditions (HeapDumpOnOutOfMemoryError), and can further customize this behavior by specifying a different path for the resulting .hprof file. You can change the name/path. What I would like to know is if its possible to make this option (HeapDumpPath) incorporate the hostname+PID to have uniquely named .hprofs (to be saved in a centralized location, but thats another scope). Could it be done? Can HeapDumpPath accept a parameter to get the hostname?
It doesn't look like there are options to inject values into the path. Depending on your shell you should be able to at least insert the hostname. The PID may not be possible since it is created after the process starts. You may be able to use a different variable like the application name though. In bash it would look something like this:
java -XX:+HeapDumpOnOutOfMemoryError -mn256m -mx512m ConsumeHeap -XX:HeapDumpPath=/disk2/dumps/$(hostname)/${APPLICATION_NAME}
Managed to do it just by using the Windows Environment Variable COMPUTERNAME in the wrapper.conf file for that particular Java VM. The full line is:
wrapper.java.additional.32=-XX:HeapDumpPath=./%COMPUTERNAME%.hprof

How to change system property using command line option

I start my java program(Test.java) like this:
java -DMyParam=2 Test
I can change this in code using System.setProperty() but is there any command line tool to change value of MyParam property ?
I tried using VM.set_flag but getting below error, probably because set_flag can set flags not system properties.
XXX#XXX-Air:~/javacode$ jcmd 11441 VM.set_flag MyParam 1
11441:
flag MyParam does not exist
This does not seem possible.
You might be able to change some manageable JVM flags using jinfo (https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jinfo.html) but that command is also experimental and maybe no longer supported in next releases according to Oracle.
No, don't think that is possible. If this is the way you want to go, you would probably have to expose that functionality yourself. Perhaps creating a JMX bean and calling it with jmxterm?

How can I specify a default value for a system property in a Java web app?

It seems like this should be so easy, but I have been banging my head on it for days.
I have a simple Java web app that uses Jetty embedded in an executable jar, and I am using system properties to define configuration info that I'm keeping separated from the app. I like that I can set system properties on the command line with -D. For example,
java -Dbackend=backend_url -jar ...
Is there a way to set a default value for such a system property?
If you are the one doing the reading of those properties. when you read them you can specify what is their default (in case they were not given via the cmd).
you can read about it here:
link

getting environment variable's value in java?

I have set an environment variable in windows by name "sample". Can I get this environment variable's value using below code?
System.getProperty("sample");
in UAT environment I have LINUX OS. Can I set the same environment variable in LINUX OS and retrieve the same using System.getProperty("sample")?
I mean does System.getProperty("sample"); retrieves irrespective of the operating system?
Also, I would like to get the environment variable's value in JSP.I know it is not recommended to keep logic in jsp but still, I need to use it. Can I use System.getProperty("sample"); in jsp? Please help me.
Use System.getenv() to retrieve environment variables, not System.getProperty(). The latter is for (as the name implies) system properties.
What's the difference? Java system properties and environment variables
If you need to access an environment variable in a JSP, you're doing something wrong. At the very least, use a servlet. More reading: How to avoid Java code in JSP files?
yes this is irrespective of OS and you can use it in JSP.

Categories