Is it possible to restart a springboot application? - java

I know that by sending a http post request to http://host:port/shutdown, we can shutdown a Springboot application. Is it possible to restart the whole springboot application by sending a http request in a production environment? So we don't need to login in the server to do that. Thank you.

I don't think such a thing exists, I'll be glad to be proven otherwise:
Spring boot doesn't do any assumptions about the environment it runs in. So when spring boot process gets shut down, re-starting it again is "out of competence" of spring boot infrastructure which is just a bunch of java classes running inside a JVM process.
You can find Here a list of endpoints exposed by the spring boot. There is a "shutdown" method that you've mentioned there, but there is no "restart" functionality exposed.
Now there are other techniques that probably can help:
If the application gets shut down because of some illegal state of some spring bean, maybe it makes sense to expose some endpoint that will "clean up" the state and make application operational again. If the application has to be restarted due to changes in configuration files or something, then you might want to consider using spring cloud's Refresh Scope for Beans. It's kind of hard to provide more information here, because you haven't mentioned the reason for shutting down the application, but I guess you've got the direction.
Having said that, there are probably some different ways to achieve what you want depending on the environment your application runs in:
If you're running in AWS for example, you can take advantage of their autoscaling policies, shut down the application remotely and AWS will run another instance for you. I'm not an expert in AWS, but I saw this working in ECS for example.
If you're running "java -jar" just on some server and want to make sure that when your process ends (by using 'shutdown') it should be started again, its possible to use some kind of wrapper that would wrap the process in service and track the service availability. There are even ready solutions for this, like Tanuki wrapper (I'm not affiliated with this product but used once its free version and it served us great)
If you're using Docker infrastructure you can change the policy and restart the container automatically when it gets shut down, I haven't used this by myself, but according to This excellent blog post is perfectly doable.

You should look at Spring boot jenkins You will also find a small article explaining how to configure the project on jenkins.

Related

Spring Batch Processing using Spring Cloud data flow

I have spring batch app and I want an admin portal to manage failed jobs and see other job related activities. I saw there was some Spring batch Admin portal package in spring, but it has been deprecated in 2017 and I have to use Spring cloud data flow as mentioned here. I want to know for Spring cloud data flow, is this some dependency we need to add to project as an artifact or is this some separate standalone service that needs to be set up?
My batch has dozen of cron jobs, can I just give my jar to Cloud Data Flow and it will take care of rest or Do I need to configure each and every job there? Any sample for the same are appreciated, as I want to know how big will be an effort to set up all this.
On the side note : My app is a combination of some REST controllers and some batch jobs.So does it make sense to use cloud data flow? If not, then is there better console manager for batch jobs(like restart ,cancel jobs portal) etc.?
Spring Data Flow requires a server to be running, where you can deploy your jars (register tasks as wrapper over spring batch). This server will be responsible for orchestration and deploying to runtime. If you have massive work load you probably need to go with cluster and Kubernetes, which supports scheduling via cron, but if now you have a single server that handles all together and you don't have performance issues, you may simplify it by using Local mode. But with Local mode you have to manage scheduling anyway by yourself with Quartz for example.
https://dataflow.spring.io/docs/feature-guides/batch/scheduling/
So just having SCDF for monitoring may be complicated and probably requires re-think your application design. Also SCDF as I see is good when you have some dependencies between tasks.
Maybe it would be easier for you to write couple of REST endpoint to fetch failed jobs and re-run them - everything depends on what you need and how big your app is.
PS:
I'm currently also thinking between having just spring batch monolith or cloud ready tasks :)

How do you use cron jobs using Elastic Beanstalk and Java?

I want to run cron jobs and use the same code base. I found a few solutions, but they don't appear ideal. For example, with Heroku, you can add a Scheduler element and fill in the commands to run in a web page.
http://blog.rotaready.com/scheduled-tasks-elastic-beanstalk-cron/
It seems overly complicated for load-balanced instances.
It makes use of require('async') in Node, but what would be a Java Spring Boot equivalent?
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features-managing-env-tiers.html
There doesn't appear to be any security. Any one the net could access the /path to POST and execute the job, causing a denial-of-service attack.
it mentions cron.yaml which doesn't make sense as the app is deployed via a WAR/ZIP file to a Tomcat instance (Spring Boot).
It mentions Amazon DynamoDB, which we don't use. We use MySQL.
It doesn't specify whether the load balancer connection draining timeout is in effect for these jobs (10s).
It mentions "Worker Configuration card on the Configuration page in the environment management console" but there is no Worker Configuration card under Configuration page.
Running a cron job in Elastic Beanstalk
For Python/Django - uses cron.yaml.
I thought of just having a dedicated EC2 instance, but how can I deploy the latest code changes there?
This may also belong on SoftwareEngineering.StackExchange.
There is an easy way to do this using other AWS systems.
You can use CloudWatch to set scheduled events (https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html). You can set a rule to set the event on a set schedule.
You then have at least two options:
set the event to publish an SNS message and use that SNS to call a web hook on your server. Many examples on how to do this but you will have to make sure you check the signature to ensure the web API is called from the signed SNS. But this would use a public API and may not be something you are comfortable with.
set the event to publish an SQS message. Then set an elastic beanstalk worker to process the SQS message or just run a background script on your main server, which is basically on an infinite loop polling SQS for work to do.
Not sure how familiar you are with these systems so not sure if it will be clear what I am talking about, but there is no way to give a detail solution so hope this is enough to give you ideas.

Why is Spring Boot Monitoring and management using a remote shell is deprecated?

I've made a small Spring Boot application and I was looking into the "put on production phase" and I thought that it would be a good idea to have the application run as a daemon on the server.
When writing the systemd service script the ExecStart was pretty straightforward, but it came to me that I had no idea how to stop it gracefully (ie not doing a kill). So after searching for a bit I came upon this : Monitoring and management using a remote shell, and I thought that it was pretty neat, that I would connect to my application using ssh inside my systemd script and I would stop my application this way. But then I saw the dreadful "(deprecated)" next to the title.
So my question is : Why is it deprecated ?
I find it a bit overkill to have to call a HTTP service to do the "simple" thing of stopping application.
Thanks for your insight on the matter.

Integrating weekly e-mail delivery/newsletter with Spring Framework

For my Spring-based web application, I now have the requirement to send out weekly e-mails to my application's users.
What are elegant solutions to this requirements?
Up until now, I have come up with the following possible solutions:
a dedicated cron job that I schedule to run once a week, running independently from my web application JVM process and outside of the web application Servlet container. This process takes care of sending out those weekly e-mails. To accomplish sending personalized e-mails, it reuses domain classes (such as my User class) that I have already developed for my web application. This dedicated process accesses my application's MySQL database concurrently to the running Spring Web MVC servlet?
a scheduled mechanism inside my Spring Web MVC servlet or inside my Servlet container.
In this setup, the e-mail sending happens inside the same JVM and the same servlet container as my web-serving Spring Web MVC servlet. Maybe this setup has (irrelevant?) advantages such as "database connection pool sharing" and "transaction sharing" "class sharing" with the servlet hosted inside the same environment.
Using or not using Spring Batch, for any of the above conceived setups. I have no experience right now with Spring Batch as to judge whether Spring Batch is or isn't an adequate tool for my requirement.
Maybe there are other solutions as well?
I am especially interested in answers that can give insights and guide in making an educated decision.
It is irrelevant for this particular question whether e-mails get sent with my own infrastructure or with a third party e-mail SaaS service.
From your description, the code for generating newsleters must share common code base with your main application. So the natural solution is to develop this code withing your main application. The open case is how this code is triggered:
From CRON. You start a script from CRON that would trigger the function within you application somehow. This somehow may be a process listening on specific port, or, what is quite natural for web application, a dedicated URL that would trigger newsletter. Just make sure that URL can't be run from outside, only from localhost (check caller IP, for example). You must, however, deal with the situation, that your app is down (restarting for example) when CRON launches the script.
From within the application. For example, using Quartz. The minus is that you need to include new library, create database tables for Quartz. The plus - Quartz will handle situation, when the task was scheduled on the moment when the application was down, because it stores the information about what was launched in DB.
We always use cron to fire a JMS message to a queue and have a dedicated process which consumes these messages. You can add the email contents to the message or just use the message as a trigger. The nice thing about this approach is you can fire in a JMS message from anywhere and have multiple handlers lots of different email scenarios. The only downside is installing a JMS broker, if you don't already have one...
I am building a Spring-MVC based web application which is required to send a weekly newsletter to a small group of people. I am using Spring's built-in scheduling mechanism. http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html‎
Yes, in this setup, the e-mail sending happens inside the same JVM and the same servlet container and it is quite easy and handy to implement the solution. I am observing the stability and reliability of this mechanism and cannot feedback more about it now.

Distributed components lifecycle management

My company has plans to implement a clustered system with a lot of services that will be deployed automatically in different machines and will interconnect with each other (SOA style). Sometimes the services will have interdependencies.
For example:
Service B (application) can be started up only when the Service A (Database) is up and running.
Each service is planned to be run as a different java process, possibly deployed as a WAR (inside dedicated tomcat) or even without web at all.
For now we have all the services in the same WAR and only single tomcat that deploys the WAR.
All the services are defined via Spring and Spring manages dependencies for us.
So I'm asking myself whether exist some frameworks that will help to manage the services in a distributed environment as I've described above?
Thanks in advance
Use ZooKeeper.
Correction, use Netflix's Curator, a framework on top of zookeeper which simplifies the work with it.
Where I work I recently implemented a Coordinator class which has two methods:
waitForDependencies - a synchronic method that checks for the liveness of the current service's dependnecies and blocks the current thread until notified that all dependency services are alive. The liveness check is done by verifying the existence of nodes which are created by the depenedency services, at the end of their initializing process, by calling notifyUp
notifyUp - a synchronic method that notifies the world that the current service that calls that method is alive. The notification is done by creating an ephemeral (temporal, stays alive just as long as the connection in which it was created is alive) node in the zk cluster, which is looked for by other services which depend on it, using waitForDependencies
Netflix released their open source tool - Asgard that manages and deploys instances to a cloud. It is tightly coupled with EC2 (the last time I checked). Depending on whether you deploy to the amazon cloud you might find that useful. I'm unaware if it supports dependencies but it does manage deployments on a distributed environment. Netflix does talk about service dependencies a lot on their blog, so the deployment solution might have a feature to solve for that.
I'm not aware of any other service / framework that does this. If you were to write this on your own I guess you could configure a couple of Jenkins tasks that deploy services. One task can depend on another to simulate the service dependency. Pinging URL endpoints can check if Service A exists before B is deployed.
There's another way to look at this. You would not need to check for dependency if you ensure your services are all running properly. Monitoring tools like Nagios can help here. Troubleshooting faulty services immediately can help you focus on deploying Service A instead of checking your dependencies on each deployment.

Categories

Resources