Running a Spring Boot App on Azure WebJob with a Schedule - java

I have a regular Java/Spring Batch job that runs every night to get data from one database and insert/update in my project's database. This is working fine in the current setup where it is deployed on Tomcat.
Now I need to separate it out and run it on an Azure WebJob. What will be a good approach?
Can I use Spring Boot for this purpose?
But I am not sure how it will work. I mean, I can create a JAR of my project (Job written using Spring Boot) and copy it on a Azure WebJob. Then have a batch file with "java -jar..." but:
wouldn't it be like running and deploying the Spring Boot App with it's inbuilt web-server that will continue to run once I run it?
secondly, the next time the batch file is executed by Azure WebJob as per the schedule I set it will try to run the Spring Boot App again and I will probably get bind exception since the port is already in use from the first run.
Would appreciate if somebody can help me in doing this.
Thank you.

wouldn't it be like running and deploying the Spring Boot App with it's inbuilt web-server that will continue to run once I run it?
A Spring Boot app can be a non web app, and a good example is a Spring Boot batch app without a dependency to a servlet container.
You can create a Spring Boot app that runs your Spring Batch job and then stops when the job is finished without the need to deploy it in a (embedded) Tomcat. You can find an example here: https://spring.io/guides/gs/batch-processing/
secondly, the next time the batch file is executed by Azure WebJob as per the schedule I set it will try to run the Spring Boot App again and I will probably get bind exception since the port is already in use from the first run.
Once you have a script to run your app with java -jar mybatchapp.jar, you can use Azure Scheduler to run your job when you want. Since your batch app does not contain/start an embedded servlet container, you won't get a port conflict.

Related

Azure webjobs with Spring Boot

I need to run a scheduler for every 1hour, and it has to read DB and send emails. I have deployed my Spring Boot app into Azure webjobs as a scheduled trigger. The app is deployed and the scheduler is working fine, but I don't see the trigger calling my app. Am I doing it correctly?
Also if my understanding is correct , when the trigger starts won't it deploy my Spring boot app again? Please let me know
Since the data from DB is less, I have not preferred to work with Spring Batch
Also if my understanding is correct , when the trigger starts won't it deploy my Spring boot app again?
yes, you can't deploy spring boot application once the trigger is started.
The app is deployed and the scheduler is working fine, but I don't see the trigger calling my app. Am I doing it correctly?
You can use Maven application Plugin to deploy the spring boot application a you are not using the spring boot batch
Also here is the Microsoft Official document where you can have a look on connecting the DB with the application and then running the scheduler.
You can even check this Spring Boot deployment documentation and this SO with related discussions.

Running Spring boot locally with a MySQL container as dependency?

After some radical changes to our schema and reading some posts on why you should avoid in memory databases.
We have decided to use MySQL locally for testing and developing. Using a MySQL docker container with a volume for persistence.
This is fairly straightforward however the issues we are having are the following:
Requires the container to be executed separate from the spring boot application (a manual task docker run
Same goes for stopping the container, its a independant process
My question is essentially, is it possible to have spring boot (when using a dev config profile) to manage this docker container.
i.e. I start development work in IntelliJ and run the service, the service checks if the container is running, if not starts it up.
If this idea is bad, then please let me know.
For testing its not issue, because we are using a maven docker plugin to create the container during the maven lifecycle.
Its more for devs working locally, and getting the service running locally with ease.
Any suggestions welcomed!
Bonus for Intellij setup!

Spring Boot : How to get Application name in Spring Boot Executable?

I have created a Spring boot application and executing the same as a an init.d service.
Tutorial Followed : https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-initd-service
I have multiple symlink created in the same machine like worker-node1, worker-node2, worker-node3, worker-node4 etc so that i can run multiple instances of the same application in the same machine as independent processes. Is there any way to get the symlink name in the Spring boot application so that i can send which worker-node picked up a task to the job management server?
Any help on this regard would be helpful.

Can I run long tasks in a Spring Boot application?

I have a Spring Boot application, which shows some data from MongoDB on an AngularJS page and allows the user to change it.
Now I need to create a mechanism, which allows me to
run a long (1-3 hours) Java method,
which produces some files and
observe its state via web (does it run, is it finished, did it crash?).
Can I implement this in scope of the Spring Boot application? If yes, what parts of Spring can I use to do that?
I would argue that it's not good idea to embed batch processing into your service exposing MongoDB data store.
I would create separate batch application. Spring Batch would be natural choice if you are using Spring stack. You would need to figure out how you want to host Spring Batch job and how you want to trigger and schedule it. Spring Batch needs SQL storage for it's metadata.
Status of the batch processing could be monitored by one other application with Spring Batch Admin module running on Servlet container. If you point this application to SQL storage of Spring Batch job application, you get monitoring of status via web UI out of the box.
Of course it could run each app with Spring Boot.
If you don't want to handle this operational complexity it brings to host there three applications, yous still can all three embed into one and it would work fine with Spring Boot. You could even execute jobs with parameters manually or restart them via Spring Batch Admin configured to have access to Spring Batch Job beans.
You could also explore using MongoDb as storage for Spring Batch metadata. E.g. this project may help. But such mechanism would need to be used by Spring Batch application and also by Spring Batch Admin module visualizing status of processing.

Spring boot application shutting down on AWS

I have a Spring boot web application running on Production deployed on Amazon Web Servers. I have create two instances of my Web applications. But sometimes one/both instance(s) automatically stops. I can't understand how the process are killed automatically.
This issue is affecting many users experience. I am using Spring Boots default properties for tomcat.
Check your application.properties for endpoints.shutdown.enabled=true.
Perhaps the shutdown endpoint is getting called by someone.
Also, scan your code for any 'System.exit'
Also, the jvm may be crashing...
Is it stopping gracefully? Are there any logs?
Found the problem:
springBoot {
mainClass = ''
executable = true
buildInfo()
}
executable needs to be changed to false

Categories

Resources