-
use spring data rest + mysql(cleardb)
-
the db schema/data is not initialized
-
when access the app for the first time, it will crash (due to step 2)
-
to the rescue! Encapsulate thecf run-task
intask
bean under a specific profile (eg. reset). We also want to make use of the services bound to the app (cleardb). 12 factor: "one off admin task"ApplicationRunner
mvn clean package
cf push demo -p target/task-demo-0.0.1-SNAPSHOT.jar --random-route --no-start
cf create-service cleardb spark mysql1
cf bind-service demo mysql1
cf start demo
curl demo-XXXX-YYYY.cfapps.io/contacts
Expecting to see the error, because the db is not initialized
$ curl demo-XXXX-YYYY.cfapps.io/contacts
{"timestamp":1486745570429,"status":500,"error":"Internal Server Error","exception":"org.springframework.dao.InvalidDataAccessResourceUsageException","message":"could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet","path":"/contacts"}
Open a new terminal to run cf logs demo
Now run the task
cf run-task demo '.java-buildpack/open_jdk_jre/bin/java \
-Dspring.jpa.hibernate.ddl-auto=update \
-Dspring.profiles.active=cloud,reset \
-Dspring.main.web-environment=false \
-cp . org.springframework.boot.loader.JarLauncher'
curl demo-XXXX-YYYY.cfapps.io/contacts
Now it should work, sample output:
{
"_embedded" : {
"contacts" : [ {
"name" : "Alice",
"_links" : {
"self" : {
"href" : "http://demo-spathulate-grainfield.cfapps.io/contacts/2"
},
"contact" : {
"href" : "http://demo-spathulate-grainfield.cfapps.io/contacts/2"
}
}
}, {
"name" : "Bob",
"_links" : {
"self" : {
"href" : "http://demo-spathulate-grainfield.cfapps.io/contacts/12"
},
"contact" : {
"href" : "http://demo-spathulate-grainfield.cfapps.io/contacts/12"
}
}
}, {
"name" : "Eve",
"_links" : {
"self" : {
"href" : "http://demo-spathulate-grainfield.cfapps.io/contacts/22"
},
"contact" : {
"href" : "http://demo-spathulate-grainfield.cfapps.io/contacts/22"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://demo-spathulate-grainfield.cfapps.io/contacts"
},
"profile" : {
"href" : "http://demo-spathulate-grainfield.cfapps.io/profile/contacts"
}
},
"page" : {
"size" : 20,
"totalElements" : 3,
"totalPages" : 1,
"number" : 0
}
}
Use command
to see the historycf tasks demo