Oracle Coherence

I was trying to setup Oracle Coherence on my Ubuntu machine. I searched for some good examples online but no luck. So i decided to post my own. Here is my experience setting up Oracle Coherence on a linux distro.

1. Download Coherence
You can download the latest Oracle Coherence from their website. At the time of this writing the current version is 3.7 (http://www.oracle.com/technetwork/middleware/coherence/overview/index.html)

2. Setup COHERENCE_HOME
I have JAVA_HOME already setup. If not you would need to set that up first.
After that you would need to set up the following:

$ export COHERENCE_HOME=/home/YOU_LOCATION/coherence
$ export PATH=$PATH:$COHERENCE_HOME/bin

Note: You can put this on your .profile if you want to plan to preserve your variables, else you will have to export the variable on each terminal tabs.

3. Basic setup
Since I am trying this out on 1 machine, I will do a basic setup. For this, I will have a cache server and a cache factory process running. Coherence by default does multicast to find cluster members. It could be set up to be unicast, if you so choose. Learn more from the pdf link below in the notes section. (You can simply ignore the details for now)

4. Touch up the scripts
The coherence directory should have few scripts inside the ‘bin’ folder. I was getting “Bad substitution” error when I tried running the script out of the box, So, I decided to trim it down. Below is the condensed version of the script that made sense to me and cut out some fat.
One of the thing to point out is that I added “-Dtangosol.coherence.cluster=shrestha -Dtangosol.coherence.clusterport=8090” in the JAVA_OPTS which doesnt exist in the canned script

See the scripts below:

cache-server.sh

# specify the JVM heap size
MEMORY=512m

if [ -f $JAVA_HOME/bin/java ]; then
  JAVAEXEC=$JAVA_HOME/bin/java
else
  JAVAEXEC=java
fi

if [[ $1 == '-jmx' ]]; then
    JMXPROPERTIES="-Dcom.sun.management.jmxremote -Dtangosol.coherence.management=all -Dtangosol.coherence.management.remote=true"
    shift
fi

JAVA_OPTS="-Xms$MEMORY -Xmx$MEMORY $JMXPROPERTIES -Dtangosol.coherence.cluster=shrestha -Dtangosol.coherence.clusterport=8090"

$JAVAEXEC -server -showversion $JAVA_OPTS -cp $COHERENCE_HOME/lib/coherence.jar com.tangosol.net.DefaultCacheServer $1

coherence.sh

if [ -f $JAVA_HOME/bin/java ]; then
  JAVAEXEC=$JAVA_HOME/bin/java
else
  JAVAEXEC=java
fi

if [ $STORAGE_ENABLED == "true" ]; then
	echo "** Starting storage enabled console **"
else
	echo "** Starting storage disabled console **"
fi

JAVA_OPTS="-Xms$MEMORY -Xmx$MEMORY -Dtangosol.coherence.distributed.localstorage=$STORAGE_ENABLED $JMXPROPERTIES -Dtangosol.coherence.cluster=shrestha -Dtangosol.coherence.clusterport=8090"

$JAVAEXEC -server -showversion $JAVA_OPTS -cp $COHERENCE_HOME/lib/coherence.jar com.tangosol.net.CacheFactory $1

5. Running Coherence
If you got through here, you are ready to start your coherence.
Now, Lets start the server, simply run the “cache-server.sh”

$ ./cache-server.sh

You should see something like

... ... ...
Services
  (
  ClusterService{Name=Cluster, State=(SERVICE_STARTED, STATE_JOINED), Id=0, Version=3.7.1, OldestMemberId=1}
  InvocationService{Name=Management, State=(SERVICE_STARTED), Id=1, Version=3.1, OldestMemberId=1}
  PartitionedCache{Name=DistributedCache, State=(SERVICE_STARTED), LocalStorage=enabled, PartitionCount=257, BackupCount=1, AssignedPartitions=257, BackupPartitions=0}
  ReplicatedCache{Name=ReplicatedCache, State=(SERVICE_STARTED), Id=3, Version=3.0, OldestMemberId=1}
  Optimistic{Name=OptimisticCache, State=(SERVICE_STARTED), Id=4, Version=3.0, OldestMemberId=1}
  InvocationService{Name=InvocationService, State=(SERVICE_STARTED), Id=5, Version=3.1, OldestMemberId=1}
  )

Started DefaultCacheServer...

Now, lets start a coherence.sh

$ ./coherence.sh

if everything went right, you may see a shell “Map(?) :”

... ... ...
TcpRing{Connections=[1]}
IpMonitor{AddressListSize=0}

2011-10-18 23:14:10.951/2.581 Oracle Coherence GE 3.7.1.0 <D5> (thread=Invocation:Management, member=2): Service Management joined the cluster with senior service member 1

Map (?):

foo bar
Now its time to take it for a spin,
in the pompt type “cache Test” to create cache named “Test”

Map (?): cache Test
Map (Test):

Lets add a key value pair, for this, we do “foo” “bar”

Map Test): put foo bar

Verify the value is stored property

Map (Test): get foo
bar

If you want to try more, you can spin up another “coherence” process by running the “coherence.sh” in another terminal. You can work with same cache, you can add values in that and go back to previous shell and see the value is globally replicated.

PS: http://download.oracle.com/docs/cd/E18686_01/coh.37/e18692.pdf

Advertisements

Testing with Spock – quick start

I heard about “Spock” the testing framework earlier this year. When I watched a online screencast at the time, I was impressed! Few things that I liked about it was the “Simplicity” and “intuitiveness” it brings to the table.
I see it as “groovy” to groovy, as to groovy to java.(i.e. life made simpler)
To be honest, when I learnt about it earlier, I was little skeptic if this was going to be adopted well and gonna be there for a while. Recently I visited their portal and I was glad to see that it was “Highly Active”. So, here I am trying to give it a try.
Complete Source code could be found at my github, https://github.com/manijshrestha/SpockExample

I have a simple project set up using gradle.
In this example I am going to try to create a “InventoryManager” that manages inventory for a online store.

Here is my Manager code:

package com.manijshrestha.spockexample

import com.manijshrestha.spockexample.exception.OutOfStockException
import com.manijshrestha.spockexample.exception.NotEnoughInventoryException

class InventoryManager{
	
    private def items =[:]
	
	def getCountOfInventory(item){
		items.get(item)
		}
	
	def gotTotalInventoryCount(){
		def sum = 0
		items.each{sum += it.value}
		return sum
	}
	
	def checkout(String item, int qty){
		def availQty = items.get(item)
		if(availQty <= 0)
			throw new OutOfStockException()
		if(qty > availQty)
			throw new NotEnoughInventoryException()
		
			items.put(item, availQty - qty)	
	}
}

And here is a simple test class used to test this code:

package com.manijshrestha.spockexample

import spock.lang.Specification
import com.manijshrestha.spockexample.InventoryManager
import com.manijshrestha.spockexample.exception.OutOfStockException
import com.manijshrestha.spockexample.exception.NotEnoughInventoryException

/**
 * Created by IntelliJ IDEA.
 * User: manijshrestha
 * Date: 10/12/11
 * Time: 11:10 PM
 * To change this template use File | Settings | File Templates.
 */
class InventoryManagerTest extends Specification{
	
	def inventoryManager = new InventoryManager()
	
	def "count of items in inventory"(){
		when:
			inventoryManager.items = ["cd": 3]
		then:
		    inventoryManager.getCountOfInventory("cd") == 3
	}
	
	def "total count of all inventories"(){
		when:
			inventoryManager.items = ["laptop": 10, "cellphone": 5, "tablet":5]
		
		then:
			inventoryManager.gotTotalInventoryCount() == 20
	}
	
	def "throw exception when trying to checkout item out of stock"(){
		when:
			inventoryManager.items = ["droidx": 0]
			inventoryManager.checkout("droidx", 1) // trying to buy droidx
		then:
			thrown(OutOfStockException)
	}
	
	def "throw exception when trying to checkout more qty than avail"(){
		when:
			inventoryManager.items = ["led tv": 2]
			inventoryManager.checkout("led tv", 3)
		then:
			thrown(NotEnoughInventoryException)
	}
	
	def "reduces correct number of items"(){
		when:
			inventoryManager.items=["dvd" : 10]
			inventoryManager.checkout("dvd", 7)
		then:
			inventoryManager.items.dvd == 3
	}

}

Few things I would like to point out here, the method names are Strings, which makes it a nice and readable test case rather than a camel cased or underscore delimited method names.
These tests I think are more readable and simple to understand. Well, I do have a very simple example here but with its power, I think writing complex test will still be simpler compared to regular jUnit tests.

Installing Nepali/Devnagari Font in Android

Update: If you do not want to mess with ADB Follow the steps from my new post about Yet another way to Install Nepali/Devnagari font on Android devices.

One of the issues with Android is that it doesn’t come with Nepali / Devanagari font by default. When I visit websites with Nepali font or have posting on Facebook with Nepali text, it shows up as boxes. Similar to image below.

It started to be more annoying as I would get emails in Nepali font, and I would have to check it on a computer.
So after tinkering around, I am happy to report that I have a solution.
This is how I solved it:
Download the Font (DroidSansFallback.ttf) on your SDCard:
DroidSansFallback.ttf

Using ADB (You may be able to use some “Explorer app” with root access. adb is simple if you have Android SDK installed)

go to the device console:

$ ./adb shell
#

Mount your phone drive so you can have write access to system folder

# mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system

Now, Simply copy over the font from your sdcard to “/system/fonts”.
(font might be already existing on the device, you can just overwrite it.)

# cp /mnt/sdcard/DroidSansFallback.ttf /system/fonts

(Note: DroidSansFallback.ttf is stored on the root of the sdcard.)
Thats it, now reboot your device and your device should render Nepali font with no issues ­čÖé

Hope this helps!