Child pages
  • HashMap Recipe
Skip to end of metadata
Go to start of metadata

About Terracotta Documentation

This documentation is about Terracotta DSO, an advanced distributed-computing technology aimed at meeting special clustering requirements.

Terracotta products without the overhead and complexity of DSO meet the needs of almost all use cases and clustering requirements. To learn how to migrate from Terracotta DSO to standard Terracotta products, see Migrating From Terracotta DSO. To find documentation on non-DSO (standard) Terracotta products, see Terracotta Documentation. Terracotta release information, such as release notes and platform compatibility, is found in Product Information.

Release: 3.6
Publish Date: November, 2011

Documentation Archive »

This example shows how to cluster a Map using Terracotta. Note that the map used in this example is a java.util.HashMap. As in
standard Java, the HashMap structure in Terracotta is not automatically locked, and requires external synchronization.

For an automatically locked Map, use for example java.util.concurrent.ConcurrentHashMa. For production code, the Terracotta Toolkit provides a fully locked, highly concurrent, automatically distributed (shared) map.

The following items are required to run the HashMap example:

How to run

A Terracotta Server instance is always required to run Terracotta. dso-java(.sh|.bat) is a wrapper script that adds Terracotta to standard java. After the Terracotta server is running, start the two nodes:

Node 1:

$ javac *.java
$ start-tc-server &
$ dso-java Main
Enter name> Fred
$ dso-java Main
Enter name> Jane

Node 2:

$ dso-java Main list
Jane : Sun Dec 09 15:20:21 PST 2007
Fred : Sun Dec 09 15:19:50 PST 2007

The Main Class

/**
  All content copyright (c) 2003-2011 Terracotta, Inc.,
  except as may otherwise be noted in a separate copyright notice.
  All rights reserved.
*/
import java.io.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;

public class Main
{
    public static final Main instance = new Main();

    private Map<String, Date> map = new HashMap<String, Date>();

    private Date get(String name)
    {
        synchronized (map) {
            return map.get(name);
        }
    } 

    private void put(String name, Date d)
    {
        synchronized (map) {
            map.put(name, d);
        }
    } 

    public void run() throws IOException
    {
        System.out.print("Enter name> "); System.out.flush();
        String name = new BufferedReader(new InputStreamReader(System.in)).readLine();

        Date d = get(name);
        if (d != null) {
            System.out.println(name + " was added on " + d);
            return;  // note the return here
        }
  
        put(name, d = new Date());
        System.out.println("Added " + name + " on " + d);
    }

    public void list()
    {
        // note that it is usually not a good idea to do IO inside of a 
        // synchronized block; this is only for demo purposes
        synchronized (map) {
            for (Map.Entry<String, Date> entry : map.entrySet()) {
                System.out.println(entry.getKey()  + " : " + entry.getValue());
            }
        }
    }

    public static void main(String[] args) throws IOException
    {
        if (args.length > 0 && args[0].equals("list")) {
            instance.list();
        } else {
            instance.run();
        }
    }

tc-config.xml

<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">

  <application>
    <dso>
      <locks>
        <autolock>
           <method-expression>* Main.get(..) </method-expression>
           <lock-level>read</lock-level>
        </autolock>
        <autolock>
           <method-expression>void Main.put(..) </method-expression>
        </autolock>
        <autolock>
           <method-expression>void Main.list(..) </method-expression>
           <lock-level>read</lock-level>
        </autolock>
      </locks>
      <roots>
        <root>
          <field-name>Main.map</field-name>
        </root>
      </roots>
    </dso>
  </application>
</tc:tc-config>
  • No labels