Monday, July 16, 2007

Distributed Groovy in 5 minutes

Ok Groovy is cool, but can your Groovy do this?



This took me about 20 minutes to do with Terracotta.  If you're not new to Terracotta, this is pretty old hat.  Distributing a LinkedBlockingQueue is really easy (as seen above).  But then again, being able to use Groovy to distribute anything in Java is ... well pretty powerful.

What if you wanted to synchronize two scripts?  Just use a java.util.concurrent.CyclicBarrier:



I could build a distributed test harness in ... oh I don't know another 10 minutes.  But I'll leave that up to you :).

Here's how to get going with Clustered Groovy:

1) Download and unpack Groovy.
2) Set GROOVY_HOME (per their instructions)
3) Download and unpack Terracotta. (Hint: get Version 2.4.  If it's not already final, it will be soon)
4) set TC_HOME (this makes your life easier)
5) You need my Groovy Startup Script - I saved it as groovyConsoleTC.  Sorry, I use a Mac so it is not Windows friendly, but at 5 lines, hopefully you can figure it out:

DIRNAME=`dirname "$0"`

TC_INSTALL_DIR=${TC_HOME}
. "${TC_INSTALL_DIR}/bin/dso-env.sh" -q $DIRNAME/groovy-tc.xml
JAVA_OPTS=$TC_JAVA_OPTS
echo $JAVA_OPTS

. "${GROOVY_HOME}/bin/startGroovy"

startGroovy groovy.ui.Console "$@"


6) You need my RootMap class.  It's not that pretty, it could be improved, but it works:


import java.util.*;
import java.util.concurrent.*;

public class RootMap
{
public final Map root = new ConcurrentHashMap();
}


7) You need my Terracotta config (save it to a file called groovy-tc.xml)


<?xml version="1.0" encoding="UTF-8"?>
<!--

All content copyright (c) 2003-2007 Terracotta, Inc.,
except as may otherwise be noted in a separate copyright notice.
All rights reserved.

-->
<tc:tc-config xmlns:tc="http://www.terracotta.org/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-4.xsd">
<servers>
<server host="%i" name="sample"/>
</servers>
<system>
<configuration-model>development</configuration-model>
</system>

<application>
<dso>
<instrumented-classes>
<include>
<class-expression>Root*</class-expression>
<honor-transient>true</honor-transient>
</include>
</instrumented-classes>
<locks>
<autolock>
<method-expression>* *..*.*(..)</method-expression>
</autolock>
</locks>
<roots>
<root>
<field-name>RootMap.root</field-name>
</root>
</roots>
</dso>
</application>
</tc:tc-config>


If you saved all that to some directory, now:

8) Compile RootMap.java : > javac RootMap.java
9) Make sure your classpath has . in it: > export CLASSPATH=.
10) start up the TC Server in $TC_HOME/bin/start-tc-server.sh
11) start one ore more Groovy Consoles using groovyConsoleTC

Validate you can see a root by executing a Groovy script:


root = new RootMap()
root.root.put("message", "Hello World")


If you run the admin console, you can see your root:

> $TC_HOME/bin/admin.sh

Now try out more advanced stuff like LinkedBlockingQueue!

Update: I've added a tar.gz file with the relevant code: distributedGroovy.tgz

6 comments:

Guillaume Laforge said...

This is pretty cool!

Guillaume Laforge said...

The tgz link doesn't seem to work.

Anonymous said...

head junior tennis racketwilson tennis racquet
wilson tennis racket
head tennis racketbabolat tennis racket
Lacoste polo shirtschaussure nike Ugg Bootspuma CATblack ugg boots
nike max ltd
ed hardy womens ed hardy sunglasses ed hardy mens

Anonymous said...

fhm I like your blog. Thank you. They are really great . Ermunterung ++ .
Some new style Puma Speed is in fashion this year.
chaussure puma is Puma shoes in french . Many Franzose like seach “chaussure sport” by the internet when they need buy the Puma Shoes Or nike max shoes. The information age is really convenient .
By the way ,the nike max ltd is really good NIKE air shoes ,don’t forget buy the puma mens shoes and nike air max ltd by the internet when you need them . Do you know Nike Air Shoes is a best Air Shoes . another kinds of Nike shoes is better . For example , Nike Air Rift is good and Cheap Nike Shoes .the nike shox shoes is fitting to running.
Spring is coming, Do you think this season is not for Ugg Boots? maybe yes .but this season is best time that can buy the cheap ugg boots. Many sellers are selling discounted. Do not miss . Please view my fc2 blog and hair straighteners blog.
.thank you .

I like orange converse shoes ,I like to buy the cheap converse shoes by the internet shop . the puma shoes and the adidas shoes (or addidas shoes) are more on internet shop .i can buy the cheap nike shoes and cheap puma shoes online. It’s really convenient.
Many persons more like Puma basket shoes than nike air rift shoes . the Puma Cat shoes is a kind of Cheap Puma Shoes .
If you want to buy the Cheap Nike Air shoes ,you can buy them online. They are same as the Nike Air shoes authorized shop. Very high-caliber Air shoes and puma cat shoes . the cheap puma shoes as same as other.

Anonymous said...

池袋 風俗
渋谷 風俗
新宿 風俗
アダルトDVD
av 写真
アダルト ブルーレイ
アダルトショップ
ペニス増大
電マ
TENGA
SM 通販
メンズセクシー下着
男性用下着
メンズTバック
大規模修繕
決済代行
SEO
SEO
Grid geat grop
カード決済
FX 外国為替
クレジットカード 比較
仔犬
子ウサギ
仔ウサギ
アダルトショップ
アダルトグッツ
ゴールドカード年会費
オナホール

rscredits said...

RSorder Offers cheapest ever rs gold
Get free gold rs by joining the 21th RSorder drop party!
runescape gold and items can be totally free on this drop party !
Time for Rsorder drop party: At 03:00 AM September 21, 2012 GMT/At 20:00 PM September 20, 2012 PST.
More detailed info at; http://www.rsorder.com/profile/news/read/id/386
You can buy cheap runescape gold through phone, paypal,moneybooker,western union and so on.
buy items for runescape at rsorder is really amazing!
Buy Cheap rsgold, RS Money, RS Power Leveling and RS Items - Pay by Phone | RSorder.com.

buy rs gold and RS products with lowest price and fastest speed by phone onebip from RSorder.com.