<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Cancel search using java sdk in Reporting</title>
    <link>https://community.splunk.com/t5/Reporting/Cancel-search-using-java-sdk/m-p/98424#M2293</link>
    <description>&lt;P&gt;Hi,&lt;BR /&gt;
I am trying to write a java wrapper which deals with retries(x) with a given timeout(n) value. The wrapper is using java Concurrent APIs (ExecutorService, FutureTask, etc).  Here is at a high level whats written:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;Create a thread which connects to splunk, fetch results and export/store to a file&lt;/LI&gt;
&lt;LI&gt;Step#1 has a timeout after which the operation should fail and retry.&lt;/LI&gt;
&lt;LI&gt;Step#2 times out but partial splunk results still shows up which I am trying to avoid. My results are stored in MultiResultsReaderXml. &lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;What I am looking for is when my thread times out, search should get aborted and returned from there on.&lt;/P&gt;

&lt;P&gt;Any pointers would be appreciated.&lt;/P&gt;

&lt;P&gt;Below is snippet of code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;public class SplunkSearch {
 public static void main(String[] args) {
   SplunkThread callable = new SplunkThread();
   FutureTask&amp;lt;String&amp;gt; futureTask = new FutureTask&amp;lt;String&amp;gt;(callable);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.execute(futureTask);
    long startTime = System.currentTimeMillis();
    int timeoutInSeconds = 5;
    while (true) {
        try {
            final long timeElapsed = (System.currentTimeMillis() - startTime) / 1000;

            if (timeElapsed &amp;gt; timeoutInSeconds) {
                System.out.println("\nTime limit Exceeded. Aborting!!!");
                futureTask.cancel(true);
                executor.shutdown();
                return;
            }
            futureTask.get(1000L, TimeUnit.MILLISECONDS);

        }  catch (TimeoutException e3) {
            System.out.print(".");
        } catch (Exception e) {
        }
    }
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;}&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;class SplunkThread implements Callable&amp;lt;String&amp;gt; {

@Override
public String call() throws Exception {
    pullLogs();
    return "";
}


public void pullLogs() {
    long startTime = System.currentTimeMillis();
    StringBuffer response = new StringBuffer();
    Map&amp;lt;String, Object&amp;gt; map = new HashMap&amp;lt;String, Object&amp;gt;();

    map.put("port", getPort());
    map.put("username", getUsername());
    map.put("password", getPassword());
    map.put("host", getHost());
    map.put("scheme", getScheme());
    map.put("output_mode", "json");
    map.put("output", "summary");

    Service service = Service.connect(map);

    OutputStream outputStream = null;
    InputStream inputStream = null;
    MultiResultsReaderXml multiResultsReader = null;

    try {
        outputStream = new FileOutputStream(instance.getOutputFile());
        inputStream = service.export(instance.getSearchQuery());

        multiResultsReader = new MultiResultsReaderXml(inputStream);
        int countEvents = 0;

        for (final SearchResults searchResults : multiResultsReader) {
            for (Event event : searchResults) {
                for (String key : event.keySet()) {
                    if (key.contains("_raw")) {
                        String data = event.get(key) + "\n";
                        outputStream.write(data.getBytes());

                    }
                }
                countEvents++;
            }
        }
        System.out.println("Total rows fetched: " + countEvents);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (multiResultsReader != null) {
                multiResultsReader.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    System.out.println("Total time taken: " + (System.currentTimeMillis() - startTime) / 1000 + " secs");
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;}&lt;/P&gt;</description>
    <pubDate>Tue, 15 Oct 2013 00:02:01 GMT</pubDate>
    <dc:creator>ashishrathore</dc:creator>
    <dc:date>2013-10-15T00:02:01Z</dc:date>
    <item>
      <title>Cancel search using java sdk</title>
      <link>https://community.splunk.com/t5/Reporting/Cancel-search-using-java-sdk/m-p/98424#M2293</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;
I am trying to write a java wrapper which deals with retries(x) with a given timeout(n) value. The wrapper is using java Concurrent APIs (ExecutorService, FutureTask, etc).  Here is at a high level whats written:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;Create a thread which connects to splunk, fetch results and export/store to a file&lt;/LI&gt;
&lt;LI&gt;Step#1 has a timeout after which the operation should fail and retry.&lt;/LI&gt;
&lt;LI&gt;Step#2 times out but partial splunk results still shows up which I am trying to avoid. My results are stored in MultiResultsReaderXml. &lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;What I am looking for is when my thread times out, search should get aborted and returned from there on.&lt;/P&gt;

&lt;P&gt;Any pointers would be appreciated.&lt;/P&gt;

&lt;P&gt;Below is snippet of code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;public class SplunkSearch {
 public static void main(String[] args) {
   SplunkThread callable = new SplunkThread();
   FutureTask&amp;lt;String&amp;gt; futureTask = new FutureTask&amp;lt;String&amp;gt;(callable);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.execute(futureTask);
    long startTime = System.currentTimeMillis();
    int timeoutInSeconds = 5;
    while (true) {
        try {
            final long timeElapsed = (System.currentTimeMillis() - startTime) / 1000;

            if (timeElapsed &amp;gt; timeoutInSeconds) {
                System.out.println("\nTime limit Exceeded. Aborting!!!");
                futureTask.cancel(true);
                executor.shutdown();
                return;
            }
            futureTask.get(1000L, TimeUnit.MILLISECONDS);

        }  catch (TimeoutException e3) {
            System.out.print(".");
        } catch (Exception e) {
        }
    }
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;}&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;class SplunkThread implements Callable&amp;lt;String&amp;gt; {

@Override
public String call() throws Exception {
    pullLogs();
    return "";
}


public void pullLogs() {
    long startTime = System.currentTimeMillis();
    StringBuffer response = new StringBuffer();
    Map&amp;lt;String, Object&amp;gt; map = new HashMap&amp;lt;String, Object&amp;gt;();

    map.put("port", getPort());
    map.put("username", getUsername());
    map.put("password", getPassword());
    map.put("host", getHost());
    map.put("scheme", getScheme());
    map.put("output_mode", "json");
    map.put("output", "summary");

    Service service = Service.connect(map);

    OutputStream outputStream = null;
    InputStream inputStream = null;
    MultiResultsReaderXml multiResultsReader = null;

    try {
        outputStream = new FileOutputStream(instance.getOutputFile());
        inputStream = service.export(instance.getSearchQuery());

        multiResultsReader = new MultiResultsReaderXml(inputStream);
        int countEvents = 0;

        for (final SearchResults searchResults : multiResultsReader) {
            for (Event event : searchResults) {
                for (String key : event.keySet()) {
                    if (key.contains("_raw")) {
                        String data = event.get(key) + "\n";
                        outputStream.write(data.getBytes());

                    }
                }
                countEvents++;
            }
        }
        System.out.println("Total rows fetched: " + countEvents);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (multiResultsReader != null) {
                multiResultsReader.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    System.out.println("Total time taken: " + (System.currentTimeMillis() - startTime) / 1000 + " secs");
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;}&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2013 00:02:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Cancel-search-using-java-sdk/m-p/98424#M2293</guid>
      <dc:creator>ashishrathore</dc:creator>
      <dc:date>2013-10-15T00:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: Cancel search using java sdk</title>
      <link>https://community.splunk.com/t5/Reporting/Cancel-search-using-java-sdk/m-p/98425#M2294</link>
      <description>&lt;P&gt;NVM. I got it working by putting a timer along the loop where results were getting extracted. If timer runs out, I return at that point.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2013 16:48:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Cancel-search-using-java-sdk/m-p/98425#M2294</guid>
      <dc:creator>ashishrathore</dc:creator>
      <dc:date>2013-10-15T16:48:10Z</dc:date>
    </item>
  </channel>
</rss>

