FileDocCategorySizeDatePackage
AsyncRequest.javaAPI DocExample3561Mon Dec 02 13:34:04 GMT 2002ora.jwsnut.chapter7.jaxr

AsyncRequest.java

package ora.jwsnut.chapter7.jaxr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import javax.xml.registry.BulkResponse;
import javax.xml.registry.BusinessQueryManager;
import javax.xml.registry.Connection;
import javax.xml.registry.ConnectionFactory;
import javax.xml.registry.JAXRException;
import javax.xml.registry.RegistryService;
import javax.xml.registry.infomodel.Organization;
import ora.jwsnut.chapter7.util.Utils;

public class AsyncRequest {

    // The BusinessQueryManager
    private static BusinessQueryManager bqm;
    
    public static void main(String[] args) {
        
        // Validate arguments
        if (args.length != 1) {
            usage();
        }
        
        // Process the common arguments
        String queryURL = args[0];
        
        try {
            // Get the ConnectionFactory
            Properties props = new Properties();
            props.put("javax.xml.registry.queryManagerURL", queryURL);
            
            ConnectionFactory cf = ConnectionFactory.newInstance();
            cf.setProperties(props);
            
            // Get and initialize the connection
            Connection conn = cf.createConnection();
            conn.setSynchronous(false);
            
            // Get the RegistryService and the BusinessQueryManager
            RegistryService registry = conn.getRegistryService();
            bqm = registry.getBusinessQueryManager();            

            // Run the example code
            doAsyncRequest();
            
            // Close connection
            conn.close();           
            
        } catch (Throwable t) {
            System.out.println(t);
            t.printStackTrace(System.out);
            System.exit(1);
        }    
        
        System.exit(0);
    }
    
    private static void doAsyncRequest() throws JAXRException {

        // Request all organizations
        ArrayList namePatterns = new ArrayList();
        namePatterns.add("%");
        BulkResponse res = bqm.findOrganizations(null, namePatterns, null, null, null, null);

	  // Wait until the request has completed
        System.out.println("Request submitted - id = " + res.getRequestId());
        while (!res.isAvailable()) {
            System.out.println("Request status: " + res.getStatus());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
        }
        System.out.println("Request completed"); 
        
        // Process results (if any)
        Collection coll = res.getCollection();
        System.out.println("Request found " + coll.size());
        Iterator iter = coll.iterator();
        while (iter.hasNext()) {
            Organization org = (Organization)iter.next();
            System.out.println("\t" + org.getName().getValue());
        }
        
        // Process exceptions (if any)
        Collection exceptions = res.getExceptions();
        if (exceptions != null && !exceptions.isEmpty()){
            System.out.println("Error while searching");
            iter = exceptions.iterator();
            while (iter.hasNext()) {
                ((Exception)iter.next()).printStackTrace(System.out);
            }
        }
    }
    
    public static void usage() {
        System.out.println("Usage:\tAsyncRequest queryURL");
        System.exit(1);
    }
}