September 21, 2006
Groovy as a command line console for ObjectGrid
We've had customers ask for a command line utility that can interact with the ObjectGrid or just an easy way to experiment with the APIs. My own point of view is that the last thing the world needs is another command line utility. The good news is that there are plenty of Java scripting languages that can be used as an excellent command line utility for the ObjectGrid.
Groovy is a popular and standardized (JCP) Java scripting languages with many advanced features only seen on languages from 20 years ago which are now making a comeback such as closures. This blog will show how we can very easily set up groovy to act as a command line console for the ObjectGrid.
Visit the IBM web site at the link at the end of the article. Unzip it and find the location of the ObjectGrid directory. It has bin/javadoc/lib directories in it. The evaluation copy is a fully functional version of the standalone ObjectGrid. It has a 60 minute shutdown feature because it's a developer only version, when it shuts down, simply start it again. Otherwise, it's fully functional.
Visit the groovy web site at the link at the end of the article. I used the JSR06 version. Unzip it. We need to edit the groovyconsole.bat to add the objectgrid jars to its classpath. This isn't strictly necessary as command line arguments could also be used but I wanted it always there. Locate the startgroovy.bat file, find the following section and add these lines:
@rem Setup the command line
# add these two lines
# add the %OG_JARS% segment in this line
This just adds the objectgrid jar to the classpath.
Have a JDK
I'm using the IBM 1.5 JDK
Setup your environment
SET JAVA_HOME to where your jdk is.
Start it up
Goto the groovy/bin directory and run the groovyconsole.bat file. This starts a swing based console which allows you to type groovy script in to the top half of the window, hit CTRL-R which runs the script and lets you view the output on the bottom half of the console.
Step 1. Create an ObjectGrid
Enter the following script and run it:
og = ObjectGridManagerFactory.getObjectGridManager().createObjectGrid("test")
We now have a reference to an ObjectGrid in the og variable. Now lets define a Map so we can use it. Remove the old script and add this instead and run it (CTRL-R).
Now we're ready to show the APIs. Each thread should have it's own connection or Session to the ObjectGrid runtime. The ObjectGrid.getSession() method returns a new Session for the calling thread to use. The following script stores such a Session in a variable sess. We then get a reference to the Map we defined earlier. Each Session should get its own reference to the Map for use on that thread. Again, remove the old one and run the following script:
sess = og.getSession()
map = sess.getMap("map")
Any output from the ObjectGrid runtime is also displayed in the output window so you can see warnings, errors or informational messages.
Inserting data in the map
We now have a reference to a session 'sess' and a reference to our Map 'map'. We can now insert some data. For example, enter the following script
This inserts three entries in the Map. If the Map was a replicated one then it would now be replicated and made fault tolerant. In this article, we'll just be using an embedded ObjectGrid, it's actually embedded right in to the Groovy Swing console! Now, lets retrieve an entry:
and you'll see "A" output on the console. Now remove that entry with the following script:
and do the following
and you'll see false.
The purpose of this was to show how to kick the tires of the ObjectGrid using groovy and also to show how groovy makes a great scriptable front end to ObjectGrid. This is why we don't ship one with the product. I'll follow up this to show how you can use groovy to interact with a clustered ObjectGrid.
- ObjectGrid evaluation copy
- Groovy download
September 21, 2006 | Permalink
The step 1 has a typo:
og = ObjectGridManagerFactory.getObjectGridManager().createObject
og = ObjectGridManagerFactory.getObjectGridManager().createObjectGrid()
Posted by: Brian Martin | Sep 25, 2006 12:47:30 PM
Did you mean createObject() ?
Posted by: Randy Schnier | Sep 26, 2006 10:18:52 AM
I see even my comment got truncated. Apparently Billy's blog software truncates lines after X characters. Let me try again with some space in there:
og = ObjectGridManagerFactory.getObjectGridManager().
Posted by: Brian Martin | Sep 27, 2006 1:18:01 PM
Click on the pictures and you can see whats missing.
Posted by: Billy | Oct 9, 2006 7:32:32 PM