Web application development and deployment onto Bluemix

Download Report

Transcript Web application development and deployment onto Bluemix

Web Application Development
and Deployment onto Bluemix
By Chase Blank
AGENDA
 Create an App
 Create a database
 Setup a JDBC Connection in App
 Push Application into Bluemix
Create an App
After logging into your Bluemix account you should be on the Dashboard
page. If not navigate to it and click on “Create an App”.
Create an App
Here you select what kind of app
you want to create. We will choose
WEB merely for convince.
This will be the name of you app in
Bluemix. It will also be the Domain
name of the application with
.mybluemix.net appended to the
end, so make sure this name is
unique and relevant to your
applcation.
Create an App
Bluemix provides a Liberty for Java Starter Application as a template so
that you can add your code and push the changes back to the Bluemix
environment. We will see how to do this later. Choose Liberty for Java and
then continue.
Create an App
Download the Cloud Foundry (CF)
installer zip file if you don’t already
have CF installed on your machine.
Also, download the starter code zip
file.
Don’t worry about the commands in
the black boxes at the bottom of the
screen. We will come back to this
later.
Create an App
Once you have
downloaded the
files, extract
them into a
location where
you can find
them.
I have everything
in my
Documents
folder for this
tutorial.
Note: I have completed two
applications thus far. We
will use ConcertFinder as
the example in this tutorial.
The extracted files are in
yellow.
Create an App
In Eclipse go to FileImport, and click on
General-Existing Projects
into Workspace.
Create an App
Browse for the
file you just
extracted.
Again, we will
use
ConcertFinder
for this
example.
Create an App
After importing the file, return to eclipse and copy all of your source
code into this project or code normally as you would in a new java
project. This includes all servlets, classes, html, jsp, css, etc..
If you have errors then right click on the project, go to properties-build a
path and add any jar files you may be missing. For example I needed to
add javax.servlet-api-3.0.1.jar.
These file don’t need to be here, but if you want
to copy and paste them here we will build a
path to this library in the project properties
later.
These files are why you downloaded and imported
the project into eclipse. We will modify them slightly
in just a minute.
Ant build will compile your program and manifest
will tell Bluemix what to do with your App.
Create an App
Open the build file.
For my program I had to
change this from 1.5 to 1.7 so I
can compile multi-thread
exception handling.
If you have servlets, you may need
to add the path to the javax.servletapi-3.0.1.jar.
Note: you may not have
to make any changes
Once you are done,
right click and run
as ant build.
Create an App
Now go to the properties
of the project folder and
click on Java Build Path.
Add the Web App Libraries
with Add external Class
Folder (The folder I
mentioned earlier that the
files were not needed there.
This is the path.)
Use the Add External
JARs button to browse
for the two db2jcc jar
files, and add them. If
you don’t have them
then Google it and
download them. You will
need these drivers to
connect to the Bluemix
database you create.
Note: If you added the path to
the library like I mentioned 2
slides before then this is
redundant and it should work
either way you do it.
Create a Database
Back on the Bluemix dashboard, we are ready to create a database for
our application. Click on “Add a Service or API”
Create a Database
Click on Data Management in the far left bar of the screen and
choose SQL database.
Create a Database
Use the dropdown to select the application you want to add the service to
i.e. the app we just created. Also take note of the service name. We will
need it in the manifest file.
This is auto generated by
Bluemix. I would
recommend NOT
changing it since it is
unique.
Click on
this box
Then
Launch.
Once your
database is
created, you can
add tables and
data to it. From
the dashboard,
click on your
app.
If you have data in
an excel file then you
can use this option to
import the data into
a table in your
database.
Here is where you
can create tables
and execute insert,
update and delete
statements to
manage your data.
You can only run
Select queries here.
This will not support
insert or update
statements.
Setup JDBC Connection
Now in Eclipse,
open the manifest
file and add your
service name
from the previous
slide.
If you didn’t
change the name
of the service then
it should look like
this with a new
combination at
the end of the
SQL Databaseline.
Be sure to make
the file look
exactly like this.
The format DOES
matter here!
Right click and run
as ant build again
after you edit the
manifest file.
Note: Anytime you make a
change to your code you
must run this before you
push your app to Bluemix
Setup JDBC Connection
Now lets go to
the app in
Bluemix. Click
on your app in
the side bar.
Click the “Show Credentials”
FYI: On the left bar there is a “Files and Logs”.
After you push it to Bluemix, this will help you
debug if your application is not working
properly.
Setup JDBC Connection
Here are the
credentials for your
database. Protect
this information.
This will allow you
to connect to the
database and
execute Select
queries, Update,
Insert, etc..
Copy down the
username, jdbcurl,
and password.
These will go in your
java code.
Setup JDBC Connection
Any time you
make a
connection to
the Bluemix
database you
created, you
will use the
credentials in
the java code
like this.
Remember the drivers we created a path to earlier in
properties? Be sure to call the db2 drivers in your code
before you try to make a connection to the database (the
line of code highlighted in yellow). Also you must surround
the connection and the query in a try/catch.
FYI: The multi-catch here is the reason I had to update the ant build file.
This is a good skeleton to follow if you aren’t sure how to make a DB
connection. Ignore the “if” and “while” pieces of code.
Note: The last system print in the catch helps with any debugging.
Push to Bluemix
Open CMD in Windows
• Move to the
location of the
file we
imported into
eclipse earlier
• Type the command
“cf login”, then enter your
Bluemix email and
password.
• Once logged in,
you are ready to
push your
application to
Bluemix.
•Type the
command “cf
push” to publish
the current
directory you are
in to Bluemix.
Push to Bluemix
You should
see this
during the
push
If it fails to push to Bluemix at this point then go back and check the manifest
file. Remember that the format matters.
You
should see
this after
Conclusion
If you still have errors that wont allow your application to start on bluemix
or it starts but doesn’t work correctly, here are some simple things you can
check.
• Does the manifest file contain your Bluemix database information?
• Are the paths to the db2 Drivers set? Do this in Properties.
• Is the Ant version you are using correct?
• Did you run Ant Build again after updating any code?
• Is the driver called in your java code before trying to connect to DB?
• Are your Query statements compatible with the Bluemix database?
Once you have completed these steps, you can now open a web browser and
view/interact with your web application using the URL
(your app name).mybluemix.net
I hope you found this tutorial helpful. Thanks, and Happy Coding!