Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Demo application on OpenShift | A simple Java application using the JDK HttpServer

branch: master
README.md

This is a simple DIY cartridge demo that illustrates the use of java in the DIY application repo. The application consists of a single test.MyHttpServer main class that is located in the repository bin directory. The src for the class is in the src directory. To update the class run by the DIY cartridge you would simple recompile it using:

javac -g -d bin src/test/MyHttpServer.java

First, create the DIY java application

Assuming you have an OpenShift account and domain setup, and client tools installed (if you don't, see https://www.openshift.com/get-started), you create your DIY application using:

[8](ironmaiden:tmp) > rhc app create diy diy-0.1
Creating application: diy
Now your new domain name is being propagated worldwide (this might take a minute)...
    retry # 5 - Waiting for DNS: diy-jbossdev.rhcloud.com
Confirming application 'diy' is available:  Success!

diy published:  http://diy-jbossdev.rhcloud.com/
git url:  ssh://0baa01674cac4a96a8ef8a1096867680@diy-jbossdev.rhcloud.com/~/git/diy.git/
Disclaimer: This is an experimental cartridge that provides a way to try unsupported languages, frameworks, and middleware on OpenShift.

Second, pull replace your app repository with the java-demo contents

[11](ironmaiden:diy) > git remote add upstream git@github.com:openshift-quickstart/openshift-diy-java-demo.git
[12](ironmaiden:diy) > git pull -s recursive -X theirs upstream master
warning: no common commits
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 19 (delta 2), reused 19 (delta 2)
Unpacking objects: 100% (19/19), done.
From github.com:openshift/openshift-diy-java-demo
 * branch            master     -> FETCH_HEAD
Auto-merging .openshift/action_hooks/stop
Auto-merging .openshift/action_hooks/start
Merge made by the 'recursive' strategy.
 .openshift/action_hooks/start           |    4 +
 .openshift/action_hooks/stop            |    1 +
 README.md                               |   22 +++++
 bin/test/MyHttpServer$HtmlHandler.class |  Bin 0 -> 5645 bytes
 bin/test/MyHttpServer.class             |  Bin 0 -> 2441 bytes
 html/index.html                         |    5 +
 src/test/MyHttpServer.java              |  150 +++++++++++++++++++++++++++++++
 7 files changed, 182 insertions(+), 0 deletions(-)
 create mode 100644 README.md
 create mode 100644 bin/test/MyHttpServer$HtmlHandler.class
 create mode 100644 bin/test/MyHttpServer.class
 create mode 100644 html/index.html
 create mode 100644 src/test/MyHttpServer.java
[13](ironmaiden:diy) > git push
Counting objects: 29, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (24/24), 8.61 KiB, done.
Total 24 (delta 4), reused 0 (delta 0)
remote: Stopping application...
remote: Done
remote: ~/git/diy.git ~/git/diy.git
remote: ~/git/diy.git
remote: Running .openshift/action_hooks/pre_build
remote: Running .openshift/action_hooks/build
remote: Running .openshift/action_hooks/deploy
remote: Starting application...
remote: Done
remote: Running .openshift/action_hooks/post_deploy
To ssh://0baa01674cac4a96a8ef8a1096867680@diy-jbossdev.rhcloud.com/~/git/diy.git/
   b90f85b..48699e2  master -> master

Now load your DIY java app page

Load http://diy-jbossdev.rhcloud.com/index.html in your browser to see the contents of the html/index.html file from your application repository. The little http java server will serve any files found in your application's html directory, so you can add files or make changes to them, push the contents and see those reflected in your browser.

Take a look at your DIY java application log on the server

You can ssh into the server sing your application uuid and dns name to look at the log generated by the java application as follows:

[21](ironmaiden:diy) > ssh 0baa01674cac4a96a8ef8a1096867680@diy-jbossdev.rhcloud.com

[diy-jbossdev.rhcloud.com ~]\> cd diy/logs/
[diy-jbossdev.rhcloud.com logs]\> ls
MyHttpServer.log
[diy-jbossdev.rhcloud.com logs]\> cat MyHttpServer.log 
Starting HttpServer for root context:/var/lib/stickshift/0baa01674cac4a96a8ef8a1096867680/diy/runtime/repo/html
Begin ------ Tue Apr 10 15:06:58 EDT 2012
RequestURI: /index.html
RequestURI.Path: /index.html
RequestURI.Query: null
RequestMethod: GET
Protocol: HTTP/1.0
RemoteAddress: /127.1.244.129:24405
LocalAddress: /127.1.244.129:8080
ContextPath: /
ContextAttributes: {}
Header(X-forwarded-proto): http
Header(Host): diy-jbossdev.rhcloud.com
Header(X-forwarded-server): diy-jbossdev.rhcloud.com
Header(Accept-charset): ISO-8859-1,utf-8;q=0.7,*;q=0.3
Header(Accept-encoding): gzip,deflate,sdch
Header(X-forwarded-for): 24.19.131.231
Header(Accept-language): en-US,en;q=0.8
Header(X-forwarded-host): diy-jbossdev.rhcloud.com
Header(User-agent): Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19
Header(Accept): text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Reading file: /var/lib/stickshift/0baa01674cac4a96a8ef8a1096867680/diy/repo/html/index.html
Sending file: /var/lib/stickshift/0baa01674cac4a96a8ef8a1096867680/diy/runtime/repo/html/index.html,56
End ------ Tue Apr 10 15:06:58 EDT 2012
Begin ------ Tue Apr 10 15:06:58 EDT 2012
RequestURI: /favicon.ico
RequestURI.Path: /favicon.ico
RequestURI.Query: null
RequestMethod: GET
Protocol: HTTP/1.0
RemoteAddress: /127.1.244.129:24406
LocalAddress: /127.1.244.129:8080
ContextPath: /
ContextAttributes: {}
Header(X-forwarded-proto): http
Header(Host): diy-jbossdev.rhcloud.com
Header(X-forwarded-server): diy-jbossdev.rhcloud.com
Header(Accept-charset): ISO-8859-1,utf-8;q=0.7,*;q=0.3
Header(Accept-encoding): gzip,deflate,sdch
Header(X-forwarded-for): 24.19.131.231
Header(Accept-language): en-US,en;q=0.8
Header(X-forwarded-host): diy-jbossdev.rhcloud.com
Header(User-agent): Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19
Header(Accept): */*
Reading file: /var/lib/stickshift/0baa01674cac4a96a8ef8a1096867680/diy/repo/html/favicon.ico
File not found: /var/lib/stickshift/0baa01674cac4a96a8ef8a1096867680/diy/runtime/repo/html/favicon.ico
End ------ Tue Apr 10 15:06:58 EDT 2012

Under the covers

The DIY cartridge provides a number of hooks that are called out during the lifecycle actions of the application. The hooks available to you for customization are found in the .openshift/action_hooks directory of your application repository.

[22](ironmaiden:diy) > ls .openshift/action_hooks/
build       post_deploy start
deploy      pre_build   stop

For this application, all that has been customized are the start/stop scripts. They simply launch the test.MyHttpServer class using java, and perform a wget call to have the test.MyHttpServer stop itself:

[23](ironmaiden:diy) > cat .openshift/action_hooks/start 
#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_INTERNAL_IP:8080

cd $OPENSHIFT_REPO_DIR
nohup java -cp bin test.MyHttpServer >${OPENSHIFT_DIY_LOG_DIR}/MyHttpServer.log 2>&1 &

[24](ironmaiden:diy) > cat .openshift/action_hooks/stop
#!/bin/bash
# The logic to stop your application should be put in this script.
wget http://${OPENSHIFT_INTERNAL_IP}:${OPENSHIFT_INTERNAL_PORT}?action=stop

See the src/test/MyHttpServer.java source to understand how the java application is making use of the OpenShift environment variables to interact with the server environment.

Licensed under ASL V2, http://www.apache.org/licenses/LICENSE-2.0

Something went wrong with that request. Please try again.