how to tune jvm when using spring boot application as unix service - performance

i'm using the new spring-boot 1.3.0 feature to run tomcat embedded spring-boot application jar as a unix service.
All is working fine but i don't know how to tune jvm (with -Xms and -Xmx parameters for example)
I've searched in spring documentation and around the web without success.

It's missing from the documentation (I've opened an issue to correct that), but you should be able to use the JAVA_OPTS environment variable.
You can configure it in a .conf file that's situated next to the jar. For example, if you jar file is /var/myapp/myapp.jar, the file /var/myapp/myapp.conf will be sourced by the launch script.

There is one more option to achieve the same , if you are running jar with mvn you can do something like this
mvn spring-boot:run -Drun.jvmArguments="-Xmx512m"
And If you are running with java -jar ,you can try something like this
java -Xmx1G -jar myapp.jar

Related

mvn spring-boot:run vs java -jar

I know it may sound silly question but I am unable to understand the difference between mvn spring-boot:run and java -jar (.jar file generated with mvn install)
I have a spring boot application with jsp pages in /src/main/resources/META-INF/resources/WEB-INF/. If I use mvn spring-boot:run these pages are served. But If I use java -jar these pages are not found by application.
The application that I am working on is at https://github.com/ArslanAnjum/angularSpringApi
UPDATE:
It works with spring boot 1.4.2.RELEASE while I intend to use the latest version i.e., 1.5.8.RELEASE.
UPDATE:
Well I solved the problem by putting jsps in src/main/webapp/WEB-INF/views/ and changing packaging type to war and then running this war using java -jar target/myapp.war and its working fine now.
Short answer: spring-boot:run is a java -jar command on steroïd running as part of your Maven build, ensuring all required parameters are passed to your app (such as resources). spring-boot:run will also ensure that your project is compiled by executing test-compile lifecycle goals prior to running your app.
Long answer:
When you run java -jar, you launch a new JVM instance with all the parameters you passed to this JVM. For example, using the Spring doc example
java -Xdebug -Xrunjdwp:server=y, \
transport=dt_socket, address=8000, suspend=
-jar target/myproject-0.0.1-SNAPSHOT.jar
You will launch a brand new JVM with the given parameters. You need to make sure to include everything needed, such as classpath elements, application parameters, JVM options, etc. on the command line.
When you run mvn spring-boot:run, you launch a Maven build that will:
Run the test-compile lifecycle goals, by default it will be resources:resources, compiler:compile, resources:testResources, compiler:testCompile goals of the Maven Resources and Compiler plugin.
Launch your application with a bunch of parameters that will depend on the
Spring Boot Maven Plugin configuration you defined in your project (your pom.xml, parents and settings, command line, etc.). This includes among other things:
A lot of classpath elements: your target/classes folder which may contain resources and libraries required by your app, your Maven dependencies, etc.
Whether to fork your JVM or not (whether to create a brand new JVM to run your app or re-use the JVM of the Maven build), see fork and agent parameter of the plugin
As per:
I have a spring boot application with jsp pages in
/src/main/resources/META-INF/resources/WEB-INF/. If I use mvn
spring-boot:run these pages are served. But If I use java -jar these
pages are not found by application.
It's because the mvn spring:boot command will make sure your target/classes folder is present in the Classpath when your app is running. After compilation, this folder will contain target/classes/META-INF/resources/WEB-INF among other things. Your app will then be able to find META-INF/resources/WEB-INF and load them when asked. When you ran java -jar command, this folder was probably not on the classpath, your app was then not able to find your resources. (these resources were copied from the src/main/resources folder during the resources:resources goal)
To have a similar result with your java -jar command, you must include your resources on the classpath such as javar -jar myapp.jar -cp $CLASSPATH;/path/to/my/project/target/classes/
Have you tried creating a jar file using mvn package instead of mvn install when you are running jar file using java -jar? package will create a jar/war as per your POM file whereas install will install generated jar file to the local repository for other dependencies if present.

Running springboot using maven vs. via java directly?

Essentially, the Jenkins CI job runs mvn spring-boot:run .... in a productions cluster as the only way to run the application. With this build step, in effect, we are running the springboot app only via maven and this has led to a very unstable jvm behavior. Also, I am unable to configure all the possible tweaks to the jvm e.g, turning on gc logging or changing to G1GC etc.. etc..
I just wanted to know if running my java springboot app should indeed be packaged into a fat jar and run with standard jvm flags, rather than from maven.
Please let me know your thoughts
Spring boot maven plugin has jvmArguments in order to set jvm settings.
......
<configuration>
<jvmArguments>-Xdebug</jvmArguments>
</configuration>
.......
The second option is to create a self-executable jar/war and use a standard way to run app - java -jar app.jar <jvm properties>
Our teams have been running fat jars similar to how others have stated with the simple java -jar ****.jar commands. However, once in production, you can utilize a container clustering system to construct the many microservices that make up your app. Seems like running maven and deploying source code, rather than artifacts seems dangerous. Hopefully this helps!

tomcat7-maven-plugin to deploy spring boot with appropriate spring profile selected

My goal is to be able to use the tomcat-maven plugin to deploy my spring boot application from the command line where an argument is supplied that tells spring which profile to use like this:
mvn tomcat7:deploy -Dspring.profiles.active="dev"
I've tried several different things such as the solution described here but the default application.properties is still always selected.
The only way that I've been able to get the application-dev.properties selected is by using
mvn spring-boot:run -Dspring.profiles.active="dev"
But we don't want to have tomcat packaged in our war
I'm new to maven and spring boot and I've been spinning my wheels for the better part of a a day now so any advice would be appreciated.
Consider using MAVEN_OPTS environment variable to set VM argument. (Linux/osx) example you would need to execute before your maven goal:
export MAVEN_OPTS="-Dspring.profiles.active=dev"
I found out the issue and I was able to get the correct profile selected using
export SPRING_PROFILES_ACTIVE=dev. The problem that I was having was when I was starting my local tomcat server through the eclipse UI my environment variables were being ignored. When starting tomcat through startup.bat the environment variable gets used and spring uses the correct profile.

Stop Spring standalone application

I have created Spring Standalone application and I have deployed that on Unix box. I have created jar file that I am executing using java -jar JarName.jar command.
Is there any way to stop this standalone process apart from using JMX?
Thanks
Sach

Hot deploy war maven project in embedded tomcat

I have maven war project.
I know inplace. it deploys to a given server. But i want to deploy on embedded tomcat and dont want to restart everytime. just say
for first time run deploy
Then change some java class and say redeploy. All in embedded tomcat.
Is this possible ?
Could the Tomcat Maven Plugin help with this?
You can use it by using the command tomcat:run
This page describes how to set up your POM/settings to make calling the plugin easier (using a prefix vs having to use full groupId/artifactId of plugin on the command line).
Maybe you can have a look at the executable war/jar feature see http://tomcat.apache.org/maven-plugin-2/executable-war-jar.html
So that will produce a simple jar which contains tomcat classes. You will be able to simply run: java -jar pathtofile.jar.

Resources