MirrorMap "Hello World" Tutorial

This tutorial will take you through all the features of MirrorMap. The tutorial requires running 3 VMs; one will host the server, one a MirrorMap that prints changes to itself and another that will be changing the same named MirrorMap.

This is the source code for running the server VM.

public class MirrorMapServer
{
    public static void main(String[] args) throws Exception
    {
        MirrorMap.server("localhost", 16025);
        while (true)
        {
            synchronized (MirrorMapServer.class)
            {
                MirrorMapServer.class.wait();
            }
        }
    }
}

This is the source code for the MirrorMap that will print changes that occur to itself.

public class MirrorMapConsumer
{
    public static void main(String[] args) throws InterruptedException
    {
        // create the mirror map around a standard hashmap
        final MirrorMap helloWorld =
            MirrorMap.create("helloWorld", new HashMap());

        // connect to the server
        MirrorMap.connect(helloWorld, "localhost", 16025);

        // add a listener to write changes to the console
        helloWorld.addListener(new MapListenerAdapter()
        {
            @Override
            public void didAdd(Object key, Object value)
            {
                System.out.println("didAdd key=" + key + " value=" + value);
            }

            @Override
            public void didRemove(Object key, Object value)
            {
                System.out.println("didRemove key=" + key + " value=" + value);
            }

            @Override
            public void didUpdate(Object key, Object oldValue, Object newValue)
            {
                System.out.println("didUpdate key=" + key + " newValue="
                    + newValue + " oldValue=" + oldValue);
            }
        });
        synchronized (MirrorMapConsumer.class)
        {
            MirrorMapConsumer.class.wait();
        }
    }
}

And finally, this is the source code for the MirrorMap that will change itself, these changes will be sent to the server and then relayed to any other MirrorMap instances of the same name.

public class MirrorMapProducer
{
    public static void main(String[] args) throws InterruptedException
    {
        // create the mirror map around a standard hashmap
        final MirrorMap helloWorld =
            MirrorMap.create("helloWorld", new HashMap());

        // connect to the server
        MirrorMap.connect(helloWorld, "localhost", 16025);

        // now start adding keys
        helloWorld.put("hello world", new Date());
        while (true)
        {
            try
            {
                helloWorld.put("hello world", new Date());
                synchronized (MirrorMapProducer.class)
                {
                    MirrorMapProducer.class.wait(2000);
                }
                helloWorld.put("hello world", new Date());
                synchronized (MirrorMapProducer.class)
                {
                    MirrorMapProducer.class.wait(2000);
                }
                helloWorld.remove("hello world");
                synchronized (MirrorMapProducer.class)
                {
                    MirrorMapProducer.class.wait(2000);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

After running all 3 programs, you should see the following occurring in the MirrorMapConsumer output

...
didAdd key=hello world value=Thu Jun 17 19:39:13 BST 2010
didUpdate key=hello world newValue=Thu Jun 17 19:39:15 BST 2010 oldValue=Thu Jun 17 19:39:13 BST 2010
didRemove key=hello world value=Thu Jun 17 19:39:15 BST 2010
didAdd key=hello world value=Thu Jun 17 19:39:19 BST 2010
didUpdate key=hello world newValue=Thu Jun 17 19:39:21 BST 2010 oldValue=Thu Jun 17 19:39:19 BST 2010
didRemove key=hello world value=Thu Jun 17 19:39:21 BST 2010
didAdd key=hello world value=Thu Jun 17 19:39:25 BST 2010
didUpdate key=hello world newValue=Thu Jun 17 19:39:27 BST 2010 oldValue=Thu Jun 17 19:39:25 BST 2010
didRemove key=hello world value=Thu Jun 17 19:39:27 BST 2010
...