I have created restfull web application using spring boot web starter which works well. I am able to access it through urls.
But I have requirement to create console command which can compute and store some values at the backend. I want to be able to run console command manually or through bash script.
I could not find any documentation on how to integrate spring-shell project in spring boot web application.
Also there is no option to choose spring-shell dependency in spring boot starter https://start.spring.io/
1) Do webapp and console need to be two separate applications and I need to deploy them separately ?
2) Is it possible to deploy web app and run console commands in the same app ?
3) What is the best approach to share common code (model, services, entities, business logic) between shell and web applications ?
Can anyone please help on this ?
Here's 2 options:
You could create a Spring
@RestController
, which you then call from the command line ?You can easily embed this in a nice shell script.
Though it is mainly for monitoring/administration purposes, you may use the spring-boot-remote-shell for that.
Dependencies
You need the following dependencies to enable the remote-shell:
Groovy script:
Add the following script in
src/main/resources/custom.groovy
:To get a hold of a Spring bean from this groovy script (source: https://stackoverflow.com/a/24300534/641627):
Launch your SpringBootApp
With spring-boot-remote-shell on the classpath, the Spring Boot Application listens on port 5000 (by default). You can now do this:
Help
You can type
help
to see the list of available commands:Call our custom command
Our custom command is listed (the fourth from the top), you can call it:
So, essentially, your crontab would do a
telnet 5000
and executecustom
Arguments
To use arguments, you can take a look at the documentation:
Sub-command (or options)
Still from their documentation:
The last command executes:
jdbc
connect
jdbc:derby:memory:EmbeddedDB;create=true
A Complete example
The following contains:
The code:
It sounds like you have two distinct use cases here: running scheduled tasks, and running commands manually. From my understanding, Spring Shell isn't part of the Boot ecosystem. You can write a Spring Shell application that is external to your Boot Web app, but it's not going to embed within it. At least from my experience.
For the first case of scheduled tasks, you should look at Spring's Scheduler. You should be able to configure a Spring application (Boot or normal) that has a Task Scheduler within it. Then you can configure your Tasks that can be scheduled, letting the scheduler do the work.
For manually executing the commands, you do have a few options here. If you use Spring Shell, assume it's running in its own process, external to the Spring Boot process. You would need to have the Shell app call into the Boot application (presuming that's where you want to work to occur) using remote method invocation technologies (eg, RMI, REST, etc).
An alternative to Spring Shell is to embed a remote shell into your Boot application. Essentially, you can use SSH to connect to your Boot application and define commands in a similar fashion as Spring Shell. The benefit is that this is in the same process as the Boot application. So you can inject the Task Scheduler and run the same Tasks manually as are scheduled. This might be a good option if you want to manually kick of the same Tasks are being scheduled. Doco for the remote console is here.
Hope this helps