[jboss-cvs] JBossCache/docs/tutorial-pojo/en ...
Ben Wang
bwang at jboss.com
Wed Sep 27 04:03:33 EDT 2006
User: bwang
Date: 06/09/27 04:03:33
Added: docs/tutorial-pojo/en master.xml
Log:
Separated PojoCache tutorial from Cache one.
Revision Changes Path
1.1 date: 2006/09/27 08:03:33; author: bwang; state: Exp;JBossCache/docs/tutorial-pojo/en/master.xml
Index: master.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<article lang="en">
<articleinfo>
<title>PojoCache Tutorial</title>
<releaseinfo>Release 2.0</releaseinfo>
<pubdate>October 2006</pubdate>
<author>
<firstname>Ben</firstname>
<surname>Wang</surname>
<email>ben.wang at jboss.com</email>
</author>
</articleinfo>
<section>
<title>Introduction</title>
<para>JBoss Cache is an in-memory replicated (synchronous or
asynchronous), transactional, and fine-grained cache. It consists of two subsystems:
Cache (generic cache) and PojoCache (object-oriented POJO cache).
In this tutorial, we will demonstrate the usage of PojoCache feature.
For details of the usage and APIs, please
refer to the user manuals for <ulink
url="http://labs.jboss.org/portal/jbosscache/docs/index.html">PojoCache</ulink>. There is a separate
tutorial on Cache.</para>
</section>
<section>
<title>Scope</title>
<itemizedlist>
<listitem>
<para>PojoCache creation and modification</para>
</listitem>
<listitem>
<para>Replication</para>
</listitem>
<listitem>
<para>Transaction</para>
</listitem>
<listitem>
<para>Cache loader</para>
</listitem>
</itemizedlist>
</section>
<section>
<title>Configuration</title>
<para>First download the standalone JBoss Cache code from <ulink
url="http://labs.jboss.org/portal/jbosscache/download/index.html">here</ulink>.
You will <literal>jboss-cache-pojo-xxx.zip</literal>. Unzip it, and you will get a
root directory (jboss-cache in our
example).</para>
<para>The configuration files are located under the etc directory. You can
modify the behavior of the underyling Cache through editing the various configuration
files.</para>
<itemizedlist>
<listitem>
<para><literal>log4j.xml</literal>. Logging output. You can turn on logging level or
change log file directory (default is
<literal>/tmp/jbosscache.log</literal>).</para>
</listitem>
<listitem>
<para><literal>replSync-service.xml</literal>. Cache configuration file (file name
is not fixed. You specify the file to be read in in
<literal>Configuration</literal>). This settings are for a
replicated, synchronous, and transactional cache. The default
DummyTransactionManager is used with a transaction isolation level of
REPEATABLE_READ. For details of the configuration parameters, please
refer to the <ulink url="http://www.jboss.org/products/jbosscache/docs">Cache</ulink>. Note that
this file is used in the BSH (<ulink
url="http://www.beanshell.org/">BeanShell</ulink>, a lightweight Java
compatible scripting language) script to configure the cache.</para>
</listitem>
<listitem>
<para><literal>pojocache-aop.xml</literal>. PojoCache configuration file that contains definition for the
example POJO classes, Person and Address, respectively. For details of
how to put your own class under AOP, please refer to the <ulink
url="http://www.jboss.org/products/jbosscache/docs">PojoCache</ulink>. This file is read in
when the process is started.</para>
</listitem>
</itemizedlist>
</section>
<section>
<title>Script</title>
<para>The script files that are needed (located under install directory)
in this tutorial are:</para>
<itemizedlist>
<listitem>
<para><literal>build.sh</literal> (or <literal>build.bat</literal> for DOS/Windows).
Simple build script that wraps
around ant. Users can simply type <literal>sh build.sh</literal> for
help. Note from now on, we will only refer to the Unix version with
the understanding that there is a corresponding DOS counterpart. The
same goes for runDemoShell explained next.</para>
</listitem>
<listitem>
<para><literal>runDemoShell.sh</literal>. Simple run script that wraps around BeanShell.
This is used to operate the replicated cache through interactive
command line.</para>
</listitem>
<listitem>
<para><literal>pojocache.bsh</literal>. Java codes that instantiate and configure the aop
cache. In addition, it also sets up the example POJO (plaing old Java
object) classes (e.g., Person and Address).</para>
</listitem>
<listitem>
<para><literal>pojocacheWithTx.bsh</literal>. Same with aop.bsh except it also instantiates a
transaction context.</para>
</listitem>
</itemizedlist>
</section>
<section>
<title>Example POJO</title>
<para>The example POJO classes used for PojoCache demo are:
<literal>Person</literal> and <literal>Address</literal>. They are located
under <literal>tests/org/jboss/cache/pojo</literal>
directory. <literal>Person</literal> has attributes of <literal>String
age, Address addr, List languages</literal>, etc. We will demonstrate that
once you put the POJO instance in the cache, plain get/set POJO methods
will be intercepted by the cache.</para>
<para>Here is the snippet of the class definition for
<literal>Person</literal> and <literal>Address</literal> plus the PojoCache annotation.</para>
<programlisting>
@org.jboss.cache.pojo.annotation.InstanceOfPojoCacheable
public class Person {
String name=null;
int age=0;
Map hobbies=null;
Address address=null;
Set skills;
List languages;
public String getName() { return name; }
public void setName(String name) { this.name=name; }
...
}</programlisting>
<programlisting>
@org.jboss.cache.pojo.annotation.PojoCacheable
public class Address {
String street=null;
String city=null;
int zip=0;
public String getStreet() { return street; }
public void setStreet(String street) { this.street=street; }
...
}</programlisting>
</section>
<section>
<title>Demo</title>
<para>To run the demo, you will need at least two windows: one to peruse
the cache contents and the other to operate the
cache directly. Of course, you can also open more than one GUI window to
see the cache replication at work to multiple members. You will also need
to run the scripts under jboss-cache installation directory after you
unzip the release package (jboss-cache-pojo-dist.zip). Due to the limitation of the
GUI, please note that:</para>
<itemizedlist>
<listitem>For each
demo example, it'd be best if you re-start the whole setup.</listitem>
<listitem>While you can modify the cache content on the GUI window and it will show up on the BSH cache
content, this won't work on PojoCache demo.
That is, you can only modify the cache content on the BSH window.</listitem>
</itemizedlist>
<para>The two demo programs to run are:</para>
<itemizedlist>
<listitem>
<para>On the first window for the GUI, type <literal>sh
build.sh</literal> to see the available commands. To run the GUI, type
<literal>sh build.sh run.demo</literal>. It will startup a
PojoCache GUI. Later on, you can click on a node to view the the
contents. Note that you can also add/modify the node contents for
non-AOP cache entries. Since the GUI entry only accepts String for
now, operation on aop cache from the GUI will not always work (unless
it is a String type).</para>
</listitem>
<listitem>
<para>On the second window for the interactive Java commands, type sh
runShellDemo.sh to fire off the BeanShell interactive command shell
(you can use either ^D or ^Z in Windows and Unix to exit afterward).
You can then read in the Java code scripts to showcase the cache
capabilities (e.g., pojocache.bsh, and pojocacheWithTx.bsh). See the
following for details.</para>
</listitem>
</itemizedlist>
</section>
<section>
<title>PojoCache</title>
<para>Once you are in the shell, type
<literal>sourceRelative("pojocache.bsh");</literal> to execute the shell script.
Basically, pojocache.bsh illustrates the steps to instantiate a cache, configure
it, and then create entries under it. Here are the snippets:</para>
<programlisting>
import org.jboss.cache.pojo.PojoCacheFactory;
import org.jboss.cache.pojo.PojoCache;
import org.jboss.cache.pojo.test.*;
show(); // verbose mode
String configFile = "META-INF/replSync-service.xml";
boolean toStart = false;
cache = PojoCacheFactory.createInstance(configFile, toStart);
joe = new Person();
joe.setName("Joe Black");
joe.setAge(31);
addr = new Address();
addr.setCity("Sunnyvale");
addr.setStreet("123 Albert Ave");
addr.setZip(94086);
joe.setAddress(addr);
cache.start(); // kick start cache
cache.attach("pojo/joe", joe); // add pojocache sanctioned object
// since it is pojocache-sanctioned, use of plain get/set methods will take care of cache content automatically.
joe.setAge(41);
</programlisting>
<para>Note the API needed to put the object (and its dependent ones) into
cache is attach. Once the second window finishes execution, you should
see the first GUI window has been populated with entries of
joe/address. Click on each tree node will display different values
associated with that node.</para>
<para>Like we have explained in the reference documentation, PojoCache uses physical "flat-space" mapping
strategy. As a result, you don't see the sub-POJO stored directly under the user-specified node. Instead,
a <literal>PojoReference</literal> is stored there (Figure 1). You can follow the internal reference and
click on there to inspect the fields there (Figure 2).</para>
<para>For example, to see PojoCache in action, you can do plain get/set methods
without ever worrying about put it in the cache. For example, you can do
in the shell window joe.getAddress().setCity("Taipei"); and see that GUI gets updated with the
age field automatically (if not, click away and back will refresh the GUI content. See Figure 3.)</para>
<figure>
<title>Gui demo: Joe reference</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/demoJoe.png"/>
</imageobject>
</mediaobject>
</figure>
<figure>
<title>Gui demo: Address reference</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/demoAddressInternal.png"/>
</imageobject>
</mediaobject>
</figure>
<figure>
<title>Gui demo: Address</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/demoAddress.png"/>
</imageobject>
</mediaobject>
</figure>
<para>Finally, PojoCache also supports get/set with parameter type of
Collection classes (i.e., List, Map, and Set). For example, type the
following in the shell command line:</para>
<programlisting>ArrayList lang = new ArrayList();
lang.add("Ensligh");
lang.add("Mandarin");
joe.setLanguages(lang);</programlisting>
</section>
<section>
<title>PojoCache with Transaction</title>
<para>To see PojoCache transaction at work, you start with the same setup
with last section except you load the bsh of pojocacheWithTx.bsh (instead of
pojocache.bsh). The additional snippets are:</para>
<programlisting>
import org.jboss.cache.pojo.PojoCache;
import org.jboss.cache.pojo.PojoCacheFactory;
import org.jboss.cache.pojo.test.*;
import javax.transaction.UserTransaction;
import javax.naming.*;
import org.jboss.cache.transaction.DummyTransactionManager;
show(); // verbose mode
// Set up transaction manager
DummyTransactionManager.getInstance();
prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.cache.transaction.DummyContextFactory");
tx = (UserTransaction)new InitialContext(prop).lookup("UserTransaction");
String configFile = "META-INF/replSync-service.xml";
boolean toStart = false;
cache = PojoCacheFactory.createInstance(configFile, toStart);
joe = new Person();
joe.setName("Joe Black");
joe.setAge(31);
addr = new Address();
addr.setCity("Sunnyvale");
addr.setStreet("123 Albert Ave");
addr.setZip(94086);
joe.setAddress(addr);
cache.start(); // kick start tree cache
cache.attach("pojo/joe", joe); // add pojocache sanctioned object
// since it is pojocache-sanctioned, use of plain get/set methods will take care of cache content automatically.
// This is also transacted.
tx.begin();
joe.setAge(41);
tx.commit();
</programlisting>
<para>Note that, in this example, a default dummy transaction manager is used. You can also try out in the
Beanshell window as follows:</para>
<programlisting>tx.begin();
addr.setZip(95131);
tx.rollback();</programlisting>
</section>
</article>
More information about the jboss-cvs-commits
mailing list