How to implement hot swapping (not automatic restart) in spring boot? - java

Hot reloading can be achieved by using spring dev tools
example https://dzone.com/articles/spring-boot-application-live-reload-hot-swap-with
but is there any way to do achieve true hot swap in spring boot. i know we can do automatic restart with spring boot dev tools . but i do not want to restart(i.e automatically/manually) the server every changes i make. i just need to update the byte code of java class i made change..

Hot reloading can be achieved by using spring dev tools
To an extent yes. As stated in the docs (emphasis mine)
Since Spring Boot applications are just plain Java applications, JVM hot-swapping should work out of the box. JVM hot swapping is somewhat limited with the bytecode that it can replace. For a more complete solution, JRebel can be used.
Ref: https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-hot-swapping
So you'll want to configure JRebel with Spring Boot for more advanced hot swapping. There are a plethora of tutorials out there.

Related

What is the difference between Quarkus and Thorntail?

Could you please provide more detailed difference between Quarkus and Thorntail?
This would help when considering between these two when building a new "Java cloud native app".
What Quarkus has over Thorntail:
Capability to create native executables, which gives you massive improvements in startup time and memory footprint. This can be critical for cloud deployments.
Even with the usual executable JAR, Quarkus is still going to have significantly smaller memory footprint (thanks to the dead code elimination), and faster startup time (because lot's of initialization work that's normally done on run-time was moved to built-time).
Live coding (akka hot swapping code) - best I've seen so far.
Better integration testing capabilities.
Advantages of Thorntail over Quarkus:
Thorntail is mature, while Quarkus only just released the first Beta. This is only issue in very short term obviously.
Compatibility with other Java libraries is going to be less of a problem in Thorntail, in a sense that:
there's a higher chance that a library will work out-of-the-box in Thorntail, than there's a chance that it will work out-of-the-box in Quarkus,
if it's not working out-of-the-box, it's gonna be easier to make it work with Thorntail than to make it work with Quarkus.
It should be said however that Quarkus team made an amazing job of preparing an impressive list of extensions which covers probably everything one could wish for(?)
Conceptually Quarkus may choose to stay "lighter" in some cases. E.g. the CDI implementation in Quarkus is a subset of the CDI spec, in order to remain leaner and faster.
see https://thorntail.io/posts/thorntail-community-announcement-on-quarkus/ (from Mar 11, 2019)
particularly in Conclusion:
Quarkus is the latest evolution of our efforts in developing a framework for microservices, and serverless, that began back in 2015 with 2.x.
and
During the next 18 months we will ensure existing Thorntail users have a smooth transition over to either Quarkus or WildFly.
that is, if you are evaluating thorntail vs quarkus for a new microservice project, you should choose quarkus, ihmo
Quarkus is a putting-all-together framework (resteasy, hibernate, and more) (comparable with spring boot and dropwizard).
Thorntail (known previously as wildly-swarm) is :
Enable deconstructing the WildFly AS and pasting just enough of it
back together with your application to create a self-contained
executable jar.

JHipster Java web app development and its hot reload capability

I currently use the Play framework using Java (actually the 1.x series) and by far the best part of using Play, what makes it hugely productive is it's inherent, transparent, comprehensive, quick, seamless 'hot reload' capability. I simply could not return to doing Java development where I don't have this.
I do however have issues with Play in general including, but not limited to, the general lack of emphasis on Java as opposed to Scala development on the platform.
So doing a bit of research on current alternatives (I previously used to do Spring J2EE development) I stumbled across JHipster which on the surface looks promising.
So can people tell me the options of using JHipster with an 'easy' hot reload setup. Are we talking about using JRebel (which is expensive) or are there other alternatives? Is it straightforward to setup and does it work consistency? Anyone who has experience of this, I would be grateful for their comments.
JHipster gives you 2 types of "hot reload" features. They are both free and Open Source, and work consistently. I know lots of people (including me) using them daily.
The Spring Boot devtools allow you to have hot reload of your Java code. This is quite close to what Play! or JRebel provide. It has also been improved by JHipster to include database updates using Liquibase, something that JRebel or Play do not provide.
BrowserSync, which gives you hot reload of your front end (AngularJS) code. This is a very different focus: this means your Web browser gets automatically refreshed when code is changed.
It is the consistent usage of both those "hot reload" features that makes JHipster powerful. I often generate big parts of my application, and I have the database, Java code, and browser all refreshed automatically on-the-fly.
I'm in the same corner than you, looking for something better than play one in the java space, it has not been an easy task, we continue using play 1.4.1.
But going to your question, like you, we finally get to Spring Boot and to JHipster; Mat Raible presentation in Devoxx is very convincing.
For hot reload of Thymeleaf Templates, it's neccesary to add next two lines in application.properties:
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5
Hot Reload Java
To have hot reloading in spring boot it's necessary to use the spring loaded project, download it and and let it somewhere in your disk.
To activate spring loaded, at least in Spring Tool Suite (eclipse) you need to add the jar as an agent in section VM Arguments of run configuration:
-Djava.security.egd=file:/dev/./urandom -javaagent:/home/hans/Descargas/springloaded-1.2.5.RELEASE.jar -noverify
It's not as cool as play one, it's necessary to restart the server from time to time, but it works.

Thrifty Java EE setup for REST services and database

I want to offer some REST services which collect data from a database and store passed data in this database. Because I work often with Java it is my prefered language instead of PHP, nodeJs or some other lightweight language/framework.
AS prefered database I would use mySQL or PostgreSQL.
To save some resources I want to use Jetty in combination with Jersey and Hibernate. For the frontend angularJS should help with its asynchronous behavior.
Could this setup run with 500MB of RAM on a small virtual server (VServer) or would it be not enough? Is these a more frugal setup for my prefered language Java?
Many thanks ;)
Going with Java for what you are suggesting is obviously going to take more resources than other obvious choices you mentioned. I'm like you though and like to use Java for this type of thing.
I have done a Jersey, MySql, AngularJS setup just you like you mentioned many times. 500 mb should be enough for you although it really depends on how large your project ends up being.
In my opinion Spring Boot is a nice way to have all your desired components up and running quickly and with minimal configuration (lots of convention-over-configuration and autoconfig).
If you're not familiar with Spring Boot, the spring.io website and its guides should get you up and running. Here is a simple guide to create your REST web service: http://spring.io/guides/gs/rest-service/
(or search for tutorials and talks on youtube/vimeo).
Here you can see an memory footprint analysis of a Spring Boot app: https://spring.io/blog/2015/12/10/spring-boot-memory-performance
As you can see there, 512 MB should be plenty for a simple app.
Spring Boot uses Tomcat and Spring MVC by default with good Hibernate support. You could swap in Jetty (search for appropriate howtos), but Tomcat works really well.
Important (whichever framework you end up using) is to set the memory parameters of the JVM manually (see the memory performance post linked above).

Containers for a Spring Project

I would like to take a doubt about the container to run a "Spring Application", in my opinion one of many features who Spring Framework offer is a possibility to create application without container EE. Is right use this type of container ("WildFly", "GlassFish") with a spring project ? In my option is wrong but i'm open to listening more people.
i saw a article where the author are showing the features of Spring 4 with the WildFly, but i didn't agree, so i would like to take more opinions about this topic here.
Considering Spring Web and Web MVC you will typically use a lightweight container like Tomcat, Jetty or any other servlet container - maybe embedded like Spring Boot does.
Spring Web is designed to work without a full JEE container but will also work deployed inside one.
With Spring without JEE you have much more control about the features you use. A full appserver has a bundled feature list, you get all or none. Spring offers you to pick what you need resulting in a more slim application.
Well, first of all it is a little tricky to say that "something" is wrong or right in software development, mainly in this type of subjective question. As always, that really depends on what you need and on the EE Container that that you are using.
Let's get Widlfly as example. Since JBoss 7 (and now Wildfly) I do not think we can say that JBoss is a "heavy" Server, as it starts in just a few seconds, much faster than in the earlier versions. While it is true that Wildfly comes with a lot of services that you may not use, it is not correct to say that you get "all or none", for at least 2 reasons:
You can disable the services you do not need.
Wildfly works with the concept of lazy-loading for its services, which means that it will only load the services that are required to run a given application.
Having said that, we can not forget that Spring also uses some EE services, if you want to. So, if you need to use Spring with JMS, for example, you still need a JMS container. In this case, you can use an EE Server without needing to install additional services or you can start a JMS container in your Tomcat.
Besides the EE features, there is also the question of features related to the server, such as configurability/manageability (Web interface, CLI), HA, scalability... that really varies from server to server and I think Jboss 7/Widfly does a good work on these points.

Advises about Spring Framework Implementation

I have a Windows server with SQL Server /IIS running on it, and the for the next application we're planning to create a Java Web App. I've found Spring Framework / Hibernate combo as a compelling option, but I can see that only in my development phase it consumes a lot of memory and processor and I got many PerGem Exceptions, Of course that I've tuned the Java Opts for the increase of Pergem Space,etc but what I can see is that in my dev environment (NetBeans / Maven) it gets really slow, to wait for Tomcat recognize XMLs, redeploy the app, it is more slow than the classic servlet MVC aproach,
My question is: ¿In a server with modest hardware/memory config, with services already running is the Spring / Hibernate combo play well or better I go for other modest way(JSF or Servlets JSPs? ¿Opinions, experiences?, For now there's no budget to buy a new server.
Spring MVC vs JSF should not make a big difference in memory consumption. Replacing hibernate by spring-jdbc might safe you a bit, but how going out of PermGem with a normal Spring/Hibernate app seems strange. Which version of tomcat are you using?
And are you only redeploying/ hot swapping, or sometimes also restarting?
Tomcat has had memory issues with hot-swapping for a long time.

Categories