Cancelling a search

The cancel method in the ODServer class cancels a search in progress.

About this task

The following example uses the ODServer.cancel method to cancel a search in progress.

This example uses ODServer, ODFolder, and ODCriteria methods to log on to a server, open a folder, and set the Date criteria to 1970-2001. A second thread is then initiated to do a search. When the second thread completes, the number of hits is displayed. A second thread is again initiated, to do a search. The process is put to sleep for 0.5 seconds and then the search is canceled. When the second thread completes, the number of hits is displayed.

This example demonstrates the following ODServer methods:
  • initialize
  • logon
  • openFolder
  • logoff
  • terminate
This example demonstrates the following ODFolder methods:
  • getName
  • getCriteria
  • search
  • close
This example demonstrates the following ODCriteria methods:
  • setOperator
  • setSearchValues
This example uses the following runtime parameters:
  • Server name
  • Port
  • User Id
  • Password
  • Folder name
Example of canceling a search:
import java.util.*;
import java.io.*;
import com.ibm.edms.od.*;

class TestThread extends Thread
{
    ODFolder odFolder;

    TestThread( ODFolder fld )
    {
        odFolder = fld;
    }

    public void run( )
    {
        Vector hits;

        try
        {
            System.out.println( "  Second thread Searching..." );
            hits = odFolder.search( );
            System.out.println( "  Search completed - Number of hits: " + hits.size( ) );
        }

        catch ( ODException e )
        {
            System.out.println( "ODException: " + e );
            System.out.println( "   id = " + e.getErrorId( ) );
            System.out.println( "  msg = " + e.getErrorMsg( ) );
            e.printStackTrace( );
        }

        catch ( Exception e2 )
        {
            System.out.println( "exception: " + e2 );
            e2.printStackTrace( );
        }
    }
}

public class TcCancelSearch
{
    public static void main ( String argv[] )
    {
        ODServer odServer;
        ODFolder odFolder;
        ODCriteria odCrit;
        TestThread search_thread;
        int j;

        //----------
        // If too few parameters, display syntax and get out
        //----------
        if ( argv.length < 8 )
        {
            System.out.println( "usage: java TcCancelSearch <server> <port> <userid> <password> <folder> " +
                                " <date field name> " +
                                " <from date mm/dd/yy or mm/dd/yyyy> " +
                                " <to date mm/dd/yy or mm/dd/yyyy> " );
            return;
        }

        try
        {
            //----------
            // Set the stage
            //----------
            System.out.println( "Testcase TcCancelSearch started." );
            System.out.println( "This testcase should:" );
            System.out.println( "  Logon to the specified server" );
            System.out.println( "  Open the specified folder" );
            System.out.println( "  Set '" + argv[5] + "' criteria to " + argv[6] + " to " + argv[7] );
            System.out.println( "  Initiate a second thread to perform the search" );
            System.out.println( "  When second thread completes, display the number of hits" );
            System.out.println( "  Initiate a second thread to perform the search" );
            System.out.println( "  Sleep for .02 seconds" );
            System.out.println( "  Cancel the search" );
            System.out.println( "  When second thread completes, display the number of hits" );
            System.out.println( "" );
            System.out.println( "Ensure that a folder is chosen that includes a criteria named Date." );
            System.out.println( "Ensure that the folder contains many hits and that arswww.props is" );
            System.out.println( "not overly restricting the number of hits which can be returned." );
            System.out.println( "" );
            System.out.println( "---------------------------------------------------" );
            System.out.println( "" );

            //----------
            // Logon to specified server
            //----------
            ODConfig odConfig = new ODConfig();
            if(odConfig != null)
            {
                odServer = new ODServer(odConfig );
                odServer.initialize( "TcCancelSearch.java" );
                System.out.println( "Logging on to " + argv[0]  + " server with user " + argv[2] + "..." );
                odServer.setPort(Integer.parseInt(argv[1]));
                odServer.logon( argv[0], argv[2], argv[3]);

                //----------
                // Open the specified folder and display its name and description
                //----------
                System.out.println( "Opening " + argv[4] + "..." );
                odFolder = odServer.openFolder( argv[4] );
                odCrit = odFolder.getCriteria( argv[5] );
                odCrit.setOperator( ODConstant.OPBetween );
                odCrit.setSearchValues( argv[6], argv[7] );

                //----------
                // Start a search on a different thread, sleep briefly, awake and cancel search
                //----------
                System.out.println( "Main thread initiating search (will not attempt to cancel)..." );
                System.out.println( "  Searching " + odFolder.getName( ) + "..." );
                search_thread = new TestThread( odFolder );
                search_thread.start( );
                search_thread.join( );

                System.out.println( "Main thread initiating search (will attempt to cancel)..." );
                search_thread = new TestThread( odFolder );
                search_thread.start( );
                System.out.println( "Main thread sleeping for .02 seconds..." );
                ( Thread.currentThread( ) ).sleep( 20 );
                System.out.println( "Main thread attempting to cancel search..." );
                odServer.cancel( );
                System.out.println( "Main thread returned from attempt to cancel" );
                search_thread.join( );

                //----------
                // Cleanup
                //----------
                odFolder.close( );
                odServer.logoff( );
                odServer.terminate( );
                System.out.println( "" );
                System.out.println( "---------------------------------------------------" );
                System.out.println( "" );
                System.out.println( "Testcase TcCancelSearch completed. - Ensure that the second search," );
                System.out.println( "  which was cancelled, yielded fewer hits than the first" );
                System.out.println( "" );
            }
        }


        catch ( ODException e )
        {
            System.out.println( "ODException: " + e );
            System.out.println( "   id = " + e.getErrorId( ) );
            System.out.println( "  msg = " + e.getErrorMsg( ) );
            e.printStackTrace( );
        }

        catch ( Exception e2 )
        {
            System.out.println( "exception: " + e2 );
            e2.printStackTrace( );
        }
    }
}