Simple Example Using Datastore Identity

This example shows how to create a factory, a session and a map instance. The map instance is used to store, retrieve and remove a JobCapable instance using datastore identity.

The example is contained in file Example2.java .

package org.job.examples;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import org.job.JobCapable;
import org.job.JobFactory;
import org.job.JobKey;
import org.job.JobKeyMap;
import org.job.JobSession;

/**
 * This class shows how to create a factory, session and map instance.
 * The map instance is used to store, retrieve and remove
 * a JobCapable
 instance using datastore identity.
 */
public class Example2
{
    /**
     * This method is invoked with an argument array containing an URL.
     * @param args the command line arguments containing an URL.
     */
    public static void main(String[] args)
    {
        // create a factory
        JobFactory jobFactory = org.job.impl.JobFactory.newInstance();
        try {
            // create a session using a built in user
            JobSession session =
                    jobFactory.getSession(args[0], "rio", "dejaneiro");

            try {
                // start a transaction
                session.begin();

                // create JOB key map
                JobKeyMap jobKeyMap = session.newKeyMap( "myJobKeyMap" );

                // create a JOB capable
                JobCapable jobCapable = new MyJobCapable( "Instance 4711" );
    
                // store the JOB capable instance
                JobKey jobKey = jobKeyMap.put( jobCapable );
    
                // retrieve the JOB capable instance
                Object value = jobKeyMap.get( jobKey );
                
                // check the retrieved instance
                if( !jobCapable.equals(value) )
                    throw new RuntimeException( "Retrieved: "+value+", expected: \""+jobCapable+'\"' );
    
                // remove JOB capable instance
                Object removedValue = jobKeyMap.remove( jobKey );
                
                // check the removed instance
                if( !value.equals(removedValue) )
                    throw new RuntimeException( "Retrieved: "+value+", expected: \""+removedValue+'\"' );
    
                // commit the transaction
                session.end();
            } finally {
                // close the session
                session.close();
            }
        } finally {
            // close the factory
            jobFactory.close();
        }
    }

    /**
     * A simple JOB capable class.
     */
    public static class MyJobCapable implements JobCapable
    {
        /** */
        String name;

        /**
         * 
         */
        public MyJobCapable()
        {}

        /**
         * @param name
         */
        MyJobCapable( String name )
        {
           this.name = name;
        }

        /*
         * @see java.lang.Object#equals(java.lang.Object)
         */
        public boolean equals(Object object)
        {
            if( !(object instanceof MyJobCapable) )
                return false;
            return  this.name.equals( ((MyJobCapable)object).name );
        }

        /*
         * @see java.lang.Object#hashCode()
         */
        public int hashCode()
        {
            return this.name.hashCode();
        }

        /*
         * @see java.lang.Object#toString()
         */
        public String toString()
        {
           return this.name;
        }

        /**
         * Required method by JOB to write this instance to the given output. 
         * @param out
         * @throws IOException
         */
        private void writeObject(ObjectOutput out) throws IOException
        {
            out.writeObject( this.name );
        }

        /**
         * Required method by JOB to read from the given input into this instance.
         * @param in
         * @throws IOException
         * @throws ClassNotFoundException
         */
        private void readObject(ObjectInput in) throws IOException, ClassNotFoundException
        {
            this.name = (String) in.readObject();
        }
    }
}