Starting Spring Boot App after deploying it to CF


Qing Gong
 

I built a Spring Boot App and using java -jar SpringBootApp.jar to run it, the code works as expected. The System.out printed as expected.

public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
System.out.println("Spring Boot Test Message");
}

However, when deployed in CF using cf push myApp -p SpringBootApp.jar, the main() was not executed. I have tried using META-INF/MANIFEST.MF to include the Main-Class, or using config/java-main.yml, or manifest.yml to include java_main_class, none worked. The app just would not start. Do I need to do anything else to trigger the app to start its main method?

Thanks!


James Bayer
 

i tried this simple getting-started guide [1] and it worked easily for me
[2].

[1] http://spring.io/guides/gs/spring-boot/
[2] https://gist.github.com/jbayer/ecacb25822dddd44ba13

On Thu, Sep 10, 2015 at 12:33 PM, Qing Gong <qinggong(a)gmail.com> wrote:

I built a Spring Boot App and using java -jar SpringBootApp.jar to run it,
the code works as expected. The System.out printed as expected.

public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
System.out.println("Spring Boot Test Message");
}

However, when deployed in CF using cf push myApp -p SpringBootApp.jar, the
main() was not executed. I have tried using META-INF/MANIFEST.MF to include
the Main-Class, or using config/java-main.yml, or manifest.yml to include
java_main_class, none worked. The app just would not start. Do I need to do
anything else to trigger the app to start its main method?

Thanks!
--
Thank you,

James Bayer


Naga Rakesh
 

Did you make your jar/war executable? if not that would help.

Just add the following below the dependency in pom

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins></build>

spring-boot-maven-plugin will help make the jar/war executable


Thanks,
Venkata

On Thu, Sep 10, 2015 at 12:33 PM, Qing Gong <qinggong(a)gmail.com> wrote:

I built a Spring Boot App and using java -jar SpringBootApp.jar to run it,
the code works as expected. The System.out printed as expected.

public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
System.out.println("Spring Boot Test Message");
}

However, when deployed in CF using cf push myApp -p SpringBootApp.jar, the
main() was not executed. I have tried using META-INF/MANIFEST.MF to include
the Main-Class, or using config/java-main.yml, or manifest.yml to include
java_main_class, none worked. The app just would not start. Do I need to do
anything else to trigger the app to start its main method?

Thanks!


Qing Gong
 

Thanks Naga. The sample project had this plugin in the pom file already.


Qing Gong
 

Thanks James! You are right that it is pretty straight forward to build and deploy this sample project. However, I got the exact same behavior. The System.out worked when I ran the jar as a standalone app (java -jar xxx.jar). However, when deployed in CF (cf push appname -p abc.jar), nothing happens in stdout. I know a static block will execute and output System.out messages. The problem is that the Spring Boot app doesn't start after deployment.


James Bayer
 

the sample app i referenced with the "initial" director works for me:

$ curl http://jamesboot.cfapps.io
Greetings from Spring Boot!

here's the corresponding logs to the sample app once it's restarted:
https://gist.github.com/jbayer/eead23ded28bf1bada47

what behavior do you get when you deploy the sample app?

On Sat, Sep 12, 2015 at 10:24 AM, Qing Gong <qinggong(a)gmail.com> wrote:

Thanks James! You are right that it is pretty straight forward to build
and deploy this sample project. However, I got the exact same behavior. The
System.out worked when I ran the jar as a standalone app (java -jar
xxx.jar). However, when deployed in CF (cf push appname -p abc.jar),
nothing happens in stdout. I know a static block will execute and output
System.out messages. The problem is that the Spring Boot app doesn't start
after deployment.
--
Thank you,

James Bayer


Qing Gong
 

Hi James,

It does work. I am not sure why it didn't last time. I will need to figure out what's difference between this sample app and my testing SB app.

One more thing. In my previous apps, I have to specifically listen on the ${PORT} so that the app will not be killed by CF. I thought CF needs this port to open for the health monitor? With this sample app, I don't have to do that. Is there some code that does that automatically?

Thanks again!


James Bayer
 

the java buildpack takes care of setting the port for you with spring boot
apps.

run a command like this where you replace "jamesboot" with whatever your
app is called:
cf curl /v2/apps/`cf app jamesboot --guid`/summary | grep start_command

and you'll see how the java buildpack assembles the detected_start_command
with the SERVER_PORT being set to $PORT

On Sun, Sep 13, 2015 at 10:50 AM, Qing Gong <qinggong(a)gmail.com> wrote:

Hi James,

It does work. I am not sure why it didn't last time. I will need to figure
out what's difference between this sample app and my testing SB app.

One more thing. In my previous apps, I have to specifically listen on the
${PORT} so that the app will not be killed by CF. I thought CF needs this
port to open for the health monitor? With this sample app, I don't have to
do that. Is there some code that does that automatically?

Thanks again!
--
Thank you,

James Bayer


Qing Gong
 

Thanks James. I did find the SERVER_PORT=$PORT in the command. Does that mean the build-pack also takes care of listening on that port so CF will not kill and restart the app? Last time when I tried to deploy my spring boot app, I had to add java code to listen on $PORT to avoid being shutdown (I thought it was strange that I had to do that, but then thought maybe CF app is different :-)). So I am a little confused why it works with this sample app but not the previous one I built. Could it be the way the spring app jar was built? I used a different script to build it. I am guessing the $SERVER_PORT variable will be used somehow, but not in the spring app itself.


Scott Frederick <scottyfred@...>
 

When using an embedded web container, Spring Boot will look for a port to
listen on as documented here:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-change-the-http-port.
The SERVER_PORT environment variable should be picked up by Boot. The only
thing the Java buildpack does is set SERVER_PORT=$PORT.

Scott

On Mon, Sep 14, 2015 at 7:59 AM, Qing Gong <qinggong(a)gmail.com> wrote:

Thanks James. I did find the SERVER_PORT=$PORT in the command. Does that
mean the build-pack also takes care of listening on that port so CF will
not kill and restart the app? Last time when I tried to deploy my spring
boot app, I had to add java code to listen on $PORT to avoid being shutdown
(I thought it was strange that I had to do that, but then thought maybe CF
app is different :-)). So I am a little confused why it works with this
sample app but not the previous one I built. Could it be the way the spring
app jar was built? I used a different script to build it. I am guessing the
$SERVER_PORT variable will be used somehow, but not in the spring app
itself.


Qing Gong
 

Perfect! Thanks.