This wiki is obsolete, see the NorduGrid web pages for up to date information.

ARC Compute Clients/Java examples

From NorduGrid
Jump to navigationJump to search

The following page contains examples on how to write Java programs interfacing the libarcclient to do submission and management of jobs. The examples are specifically for use with ARC release 11.05.

When compiling and running the classpath should at least contain the path to the JAR archive containing the Java classes used to interface the language bindings, e.g. $ARC_LOCATION/lib/java/arc.jar. When running the programs, the directory containing the language bindings in form of a library should be specified. This can be done by setting either the library path environment variable (Linux: LD_LIBRARY_PATH, Windows: PATH, Mac OS X: DYLD_LIBRARY_PATH), or the java.library.path system property. The library is normally located in the $ARC_LOCATION/lib/java directory.

Job submission

The code example below shows how to submit a "Hello World" job to a specified Computing Element.

import nordugrid.arc.*;

public class arcsub
{
  public static void main(String[] args)
  {
    System.loadLibrary("jarc");

    // Setup logging.
    // Possible log-levels: FATAL, ERROR, WARNING, INFO, VERBOSE and DEBUG.
    new Logger(Logger.getRootLogger(), "arcsub");
    Logger.getRootLogger().addDestination(new LogStream_ostream(arc.getStdout()));
    Logger.getRootLogger().setThreshold(LogLevel.DEBUG);

    // Initialise the UserConfig object.
    // This object holds various attributes, including proxy location and selected services.
    UserConfig usercfg = new UserConfig("");

    // Convert the commandline arguments to a StringList object.
    StringList clusters = new StringList();
    for (int i = 0; i < args.length; i++) {
      clusters.add(args[i]);
    }

    /* 
     * Clear selected services. Default services might have been set from loading the system
     * user configuration file ($ARC_LOCATION/etc/arc/client.conf) or the user configuration
     * file ($HOME/~arc/client.conf).
     */
    usercfg.ClearSelectedServices();
    // Set the selected services, e.g. the arguments specified on the command line.
    // A information service can also be added (ServiceType.INDEX).
    usercfg.AddServices(clusters, ServiceType.COMPUTING);

    // Initialise TargetGenerator object.
    TargetGenerator targen = new TargetGenerator(usercfg);
    // Fetch information about services and create ExecutionTargets from these (CEs).
    targen.GetTargets(0, 1);

    // Create job description.
    // Set the executable.
    ExecutableType exe = new ExecutableType();
    exe.setName("/bin/echo");

    // Set arguments to the executable.
    StringList arguments = new StringList();
    arguments.add("Hello World");
    exe.setArgument(arguments);

    ApplicationType app = new ApplicationType();
    app.setExecutable(exe);
    // Set standard output file.
    app.setOutput("std.out");

    JobDescription job = new JobDescription();
    job.setApplication(app);
    // Job description created.

    // Print job description in XRSL format to screen.
    // Supported formats: XRSL, ARCJSDL, JDL.
    System.out.println(job.UnParse("XRSL"));

    // Get list of collected targets.
    ExecutionTargetList targets = targen.FoundTargets();

    // Loop over targets.
    for (ExecutionTargetListIteratorHandler it = new ExecutionTargetListIteratorHandler(targets.begin());
         !it.equal(targets.end()); it.next()) {
      // Get submitter object from target.
      Submitter submitter = it.pointer().GetSubmitter(usercfg);
      // Try to submitting to target.
      URL submitted = submitter.Submit(job, it.pointer());

      if (submitted.toBool()) {
        // If successful write job id.
        System.out.println(submitted.fullstr());
        break;
      }
      else {
        System.out.println("Unable to submit to " + it.pointer().getUrl());
      }
    }
  }
}

The class should be used as follows:

$ java arcsub ARC0:ldap://lscf.nbi.dk:2138/nordugrid-cluster-name=lscf.nbi.dk,Mds-vo-name=local,o=grid

which submits a hello world job to the Grid-Manager at lscf.nbi.dk.
Note that it is the information endpoint which should be specified. For submission to:

  • Grid-Manager use ARC0 prefix,
  • A-REX use ARC1 prefix,
  • CREAM use CREAM prefix,
  • UNICORE use UNICORE prefix.