Terracotta Catalog Example
The catalog example is something I cooked up for an article on InfoQ. It's an idealized retail system with a catalog of products that can be added to a customer's shopping cart. At any time, the set of active shopping carts can be viewed by, for example, an administration or reporting console. At any time, the set of active shopping carts can be viewed by, for example, an administration or reporting console.
The sample code is written using simple Java data structures. Some of the problem domain is idealized for simplicity. For example, the business data encapsulated in the in the product, catalog, customer, and order classes in a real system would likely be backed by a relational database, perhaps fronted by an object-relational system of some kind. The transient shopping cart data, though, might very well be best expressed purely as simple Java objects with no backing system of record.
Running the Example
Check out the code from the svn repository:
Import it into Eclipse (make sure you have the Terracotta plugin installed)

Choose "Terracotta DSO Project"

Select "Create project from existing source" and browse to where you checked out the terracotta-catalog-example. Give the project a name (e.g., "Terracotta Catalog Example"). Then select "Finish."

Right-click on the Main class in the Package Explorer and choose "Run..." from the context menu.

Configure the program arguments: the first argument is the number of threads, the second argument ("run-locally") instructs the Main class to set up the execution for a single JVM.

You should see output that looks like this:
 |
Step 1: Waiting for everyone to arrive. I'm expecting 1 other thread(s)...
Step 1: Waiting for everyone to arrive. I'm expecting 1 other thread(s)...
We're all here!
We're all here!
Step 2: Set Up
Step 2: Set Up
I'm the first thread, so I'm going to populate the catalog...
Step 3: Let's do a little shopping...
Step 3: Let's do a little shopping...
I'm adding "Price: $12.00; Name: 14 blade super razor" to my cart...
I'm adding "Price: $5.00; Name: Super-smooth shaving cream" to my cart...
Step 4: Let's look at all shopping carts in all JVMs...
Step 4: Let's look at all shopping carts in all JVMs...
==========================
Shopping Cart
item 1: Price: $5.00; Name: Super-smooth shaving cream
==========================
Shopping Cart
item 1: Price: $12.00; Name: 14 blade super razor
Step 5: Let's wait for the other JVM to make a price change...
==========================
Shopping Cart
item 1: Price: $5.00; Name: Super-smooth shaving cream
==========================
Shopping Cart
item 1: Price: $12.00; Name: 14 blade super razor
Step 5: Let's make a 10% price increase...
Step 6: Let's look at the shopping carts with the new prices...
Step 6: Let's look at the shopping carts with the new prices...
==========================
Shopping Cart
item 1: Price: $5.50; Name: Super-smooth shaving cream
==========================
Shopping Cart
item 1: Price: $13.20; Name: 14 blade super razor
==========================
Shopping Cart
item 1: Price: $5.50; Name: Super-smooth shaving cream
==========================
Shopping Cart
item 1: Price: $13.20; Name: 14 blade super razor |
Runing the Example in Multiple JVMs
Next, try running the same code with the two execution threads running in separate JVMs.
Start the Terracotta server

Start the Two JVMs
Start the first JVM:

You should see output like this:
 |
2007-01-22 19:09:06,310 INFO - Terracotta, version 2.2 as of 20061201-071248.
2007-01-22 19:09:06,890 INFO - Configuration loaded from the file at '/Users/orion/tmp/terracotta-catalog-example/tc-config.xml'.
2007-01-22 19:09:06,907 INFO - Log file: '/Users/orion/tmp/terracotta-catalog-example/terracotta/client-logs/terracotta-client.log'.
Step 1: Waiting for everyone to arrive. I'm expecting 1 other thread(s)... |
Then, start the second JVM the same way.
In the console of the first JVM, you should see output like this:
 |
2007-01-22 19:09:06,310 INFO - Terracotta, version 2.2 as of 20061201-071248.
2007-01-22 19:09:06,890 INFO - Configuration loaded from the file at '/Users/orion/tmp/terracotta-catalog-example/tc-config.xml'.
2007-01-22 19:09:06,907 INFO - Log file: '/Users/orion/tmp/terracotta-catalog-example/terracotta/client-logs/terracotta-client.log'.
Step 1: Waiting for everyone to arrive. I'm expecting 1 other thread(s)...
We're all here!
Step 2: Set Up
I'm the first thread, so I'm going to populate the catalog...
Step 3: Let's do a little shopping...
I'm adding "Price: $12.00; Name: 14 blade super razor" to my cart...
Step 4: Let's look at all shopping carts in all JVMs...
==========================
Shopping Cart
item 1: Price: $12.00; Name: 14 blade super razor
==========================
Shopping Cart
item 1: Price: $5.00; Name: Super-smooth shaving cream
Step 5: Let's make a 10% price increase...
Step 6: Let's look at the shopping carts with the new prices...
==========================
Shopping Cart
item 1: Price: $13.20; Name: 14 blade super razor
==========================
Shopping Cart
item 1: Price: $5.50; Name: Super-smooth shaving cream
|
In the console of the second JVM, you should see output like this:
 |
2007-01-22 19:10:49,193 INFO - Terracotta, version 2.2 as of 20061201-071248.
2007-01-22 19:10:49,983 INFO - Configuration loaded from the file at '/Users/orion/tmp/terracotta-catalog-example/tc-config.xml'.
WARN: The log directory, '/Users/orion/tmp/terracotta-catalog-example/terracotta/client-logs', is already in use by another Terracotta process. Logging will proceed to the console only.
Step 1: Waiting for everyone to arrive. I'm expecting 1 other thread(s)...
We're all here!
Step 2: Set Up
Step 3: Let's do a little shopping...
I'm adding "Price: $5.00; Name: Super-smooth shaving cream" to my cart...
Step 4: Let's look at all shopping carts in all JVMs...
==========================
Shopping Cart
item 1: Price: $12.00; Name: 14 blade super razor
==========================
Shopping Cart
item 1: Price: $5.00; Name: Super-smooth shaving cream
Step 5: Let's wait for the other JVM to make a price change...
Step 6: Let's look at the shopping carts with the new prices...
==========================
Shopping Cart
item 1: Price: $13.20; Name: 14 blade super razor
==========================
Shopping Cart
item 1: Price: $5.50; Name: Super-smooth shaving cream
|