public static void main(String[] args) throws URISyntaxException, IOException {

    // Create and configure a new instance of RallyRestApi
    // (Server, username, password and proxy settings configured in Factory)
    RallyRestApi restApi = RestApiFactory.getRestApi();

    try {

      // Create a defect
      System.out.println("Creating defect...");
      JsonObject newDefect = new JsonObject();
      newDefect.addProperty("Name", "Test Defect");
      CreateRequest createRequest = new CreateRequest("defect", newDefect);
      CreateResponse createResponse = restApi.create(createRequest);
      System.out.println(
          String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));

      // Read defect
      String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
      System.out.println(String.format("\nReading defect %s...", ref));
      GetRequest getRequest = new GetRequest(ref);
      GetResponse getResponse = restApi.get(getRequest);
      JsonObject obj = getResponse.getObject();
      System.out.println(
          String.format(
              "Read defect. Name = %s, State = %s",
              obj.get("Name").getAsString(), obj.get("State").getAsString()));

      // Update defect
      System.out.println("\nUpdating defect state...");
      JsonObject updatedDefect = new JsonObject();
      updatedDefect.addProperty("State", "Fixed");
      UpdateRequest updateRequest = new UpdateRequest(ref, updatedDefect);
      UpdateResponse updateResponse = restApi.update(updateRequest);
      obj = updateResponse.getObject();
      System.out.println(
          String.format("Updated defect. State = %s", obj.get("State").getAsString()));

      // Delete defect
      System.out.println("\nDeleting defect...");
      DeleteRequest deleteRequest = new DeleteRequest(ref);
      DeleteResponse deleteResponse = restApi.delete(deleteRequest);
      if (deleteResponse.wasSuccessful()) {
        System.out.println("Deleted defect.");
      }

    } finally {
      // Release all resources
      restApi.close();
    }
  }
  public static void main(String[] args) throws URISyntaxException, IOException {

    // Create and configure a new instance of RallyRestApi
    // (Server, username, password and proxy settings configured in Factory)
    RallyRestApi restApi = RestApiFactory.getRestApi();

    try {
      String ref = "/hierarchicalrequirement/12544729477";
      GetRequest getRequest = new GetRequest(ref);
      getRequest.setFetch(new Fetch("Defects"));

      System.out.println(String.format("\nReading defect info for story %s...", ref));
      GetResponse getResponse = restApi.get(getRequest);
      JsonObject story = getResponse.getObject();

      JsonObject defectInfo = story.getAsJsonObject("Defects");
      int defectCount = defectInfo.get("Count").getAsInt();
      System.out.println(String.format("\nTotal defects: %d", defectCount));

      if (defectCount > 0) {
        QueryRequest defectRequest = new QueryRequest(defectInfo);
        defectRequest.setFetch(new Fetch("FormattedID", "Name", "State", "Priority"));

        QueryResponse queryResponse = restApi.query(defectRequest);
        if (queryResponse.wasSuccessful()) {
          for (JsonElement result : queryResponse.getResults()) {
            JsonObject defect = result.getAsJsonObject();
            System.out.println(
                String.format(
                    "\t%s - %s: Priority=%s, State=%s",
                    defect.get("FormattedID").getAsString(),
                    defect.get("Name").getAsString(),
                    defect.get("Priority").getAsString(),
                    defect.get("State").getAsString()));
          }
        }
      }
    } finally {
      // Release resources
      restApi.close();
    }
  }
  public static void main(String[] args)
      throws URISyntaxException, IOException, InterruptedException {

    // start main thread
    String host = "https://rally1.rallydev.com";
    String apiKey = "_abc123";
    String applicationName = "Nick multi thread example";
    RallyRestApi restApi = new RallyRestApi(new URI(host), apiKey);
    restApi.setApplicationName(applicationName);

    // get current user
    GetRequest getRequest = new GetRequest("/user");
    GetResponse getResponse = restApi.get(getRequest);
    JsonObject currentUser = getResponse.getObject();
    String currentUserRef = currentUser.get("_ref").getAsString();
    System.out.println("current user " + currentUserRef);

    // get workspaces where the current user has permission
    List<String> workspaces = new ArrayList<String>();
    QueryRequest workspacePermissionsRequest = new QueryRequest("WorkspacePermissions");
    workspacePermissionsRequest.setQueryFilter(new QueryFilter("User", "=", currentUserRef));
    workspacePermissionsRequest.setFetch(new Fetch("Workspace", "ObjectID"));

    QueryResponse workspacePermissionsResponse = restApi.query(workspacePermissionsRequest);
    int numberOfPermissoins = workspacePermissionsResponse.getTotalResultCount();
    System.out.println(numberOfPermissoins);
    if (numberOfPermissoins > 0) {
      for (int i = 0; i < numberOfPermissoins; i++) {
        JsonObject workspacePermissionObject =
            workspacePermissionsResponse.getResults().get(i).getAsJsonObject();
        String workspaceRef =
            workspacePermissionObject
                .get("Workspace")
                .getAsJsonObject()
                .get("ObjectID")
                .getAsString();
        workspaces.add(workspaceRef);
      }
    }
    System.out.println("workspaces " + workspaces);

    class MyRunnable implements Runnable {
      private String oid;

      public MyRunnable(String oid) {
        this.oid = oid;
      }

      public void run() {
        try {
          getRally(this.oid);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    Thread[] threads = new Thread[numberOfPermissoins];
    for (int i = 0; i < threads.length; i++) {
      threads[i] = new Thread(new MyRunnable(workspaces.get(i)));
      threads[i].start();
      threads[i].join();
    }
    if (restApi != null) {
      restApi.close();
    }
  }