In my previous video I took you through a tour of the BlueMix UI, but we didn’t actually go through pushing an application up to BlueMix and seeing the results.  Well no fear, I have created videos for that as well.  I actually split this up into two videos.  In the first one we create a Java app in the BlueMix UI, download the code for the app, make some changes, and then push it back to BlueMix.  In part two we learn about scaling our Java app and how to take advantage of the services in BlueMix to deal with the elasticity of the cloud.  Below are the videos AND the code I wrote during the demonstration.

Part 1

Here is the code.

package com.ibm.cloudoe.samples;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/counter")
public class Counter {

	private static int counter = 0;

	@GET
	public String getCounter() {
		counter++;
		return "This servlet was called " + counter + " times.";
	}
}

 

Part 2

Here is the code we wrote in part 2.

package com.ibm.cloudoe.samples;

import java.util.Iterator;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.apache.wink.json4j.JSONArray;
import org.apache.wink.json4j.JSONException;
import org.apache.wink.json4j.JSONObject;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

@Path("/counter")
public class Counter {

	private static Jedis jedis;

	public Counter() throws JSONException {
		jedis = getJedis();
	}

	@GET
	public String getCounter() {
		String value = jedis.get("counter");
		int counter = 1;
		if(value != null) {
			counter = Integer.parseInt(value);
			counter++;
		}
		jedis.set("counter", Integer.toString(counter));
		return "This servlet was called " + counter + " times.";
	}

	private Jedis getJedis() throws JSONException {
		JSONObject service = getServiceByName("redis-java-demo");
		JSONObject creds = service.getJSONObject("credentials");
		String host = creds.getString("host");
		int port = creds.getInt("port");
		String pwd = creds.getString("password");
		JedisPool pool = new JedisPool(new JedisPoolConfig(), host, port, Protocol.DEFAULT_TIMEOUT, pwd);
		return pool.getResource();
	}

	private JSONObject getServiceByName(String name) throws JSONException {
		JSONObject service = null;
		JSONObject envServices = new JSONObject(System.getenv("VCAP_SERVICES"));
		Iterator iter = envServices.keys();
		while(iter.hasNext()) {
			String key = iter.next();
			JSONArray servArray = envServices.getJSONArray(key);
			Iterator servIter = servArray.iterator();
			while(servIter.hasNext()) {
				JSONObject tempService = servIter.next();
				if(name.equals(tempService.getString("name"))) {
					service = tempService;
					break;
				}
			}
			if(service != null) {
				break;
			}
		}
		return service;
	}

}

Remeber for this code to compile you need to add the Jedis jar and the Apache Commons Pool jar (you must download version 1.6) to WebContent/WEB-INF/libs directory and modify the path element of the build.xml to include these jars.

<path id="classpathDir">
        <pathelement location="bin"/>
        
    	<fileset dir="WebContent/WEB-INF/lib">
    		<include name="**/*.jar"/>
    	</fileset>
    </path>

 


Ryan J Baxter

Husband, Father, Software Engineer