Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to Excecute Logic Before SpringBoot Application Starts?

 
Bartender
Posts: 1679
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to just run some code in the Spring ApplicationClass before Spring starts up to make sure a TCP/IP server is running. If it's not running, the code will load it.

However, just putting that Java code in the Spring Application main() method does not work at all.

So, what is required to run some pre-application start code in SpringBoot?

(or is it recommended to just JAR up that separate server start logic and run it on computer restart?)

The sites I looked at online were not clear.

Thanks in advance,

- mike
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean with "does not work at all"? What exactly did you try and what problems exactly did you have with it? Any error messages?

You can execute whatever you want in the main() method before you call SpringApplication.run(), there's no special reason why this wouldn't work.
 
Mike London
Bartender
Posts: 1679
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote:What do you mean with "does not work at all"? What exactly did you try and what problems exactly did you have with it? Any error messages?

You can execute whatever you want in the main() method before you call SpringApplication.run(), there's no special reason why this wouldn't work.



That is what I thought as well.

I have logging statements before and after the static method I'm calling (to start the TCP/IP server). But the logging isn't logging those strings and, similarly, the logging statements in the static method aren't being logged either.

How is that possible?

Thanks,

- mike
 
Rancher
Posts: 4188
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has the logging system started up at that point?
Isn't that one of the things Spring Boot loads up?
 
Mike London
Bartender
Posts: 1679
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Has the logging system started up at that point?
Isn't that one of the things Spring Boot loads up?



Yeah, I thought of that too.

But, in any case, the TCP/IP server isn't getting started from the static call in main() so I guess I'll create a standalone executable JAR and auto-start that at the OS level. That approach may be better since as the application grows, they might not always want to start that TCP/IP server.

Thanks,

- mike
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!