0

It was my first post on the community. Any comments and suggestions are welcome. I have a web service on end point http://ip:port/report. This service execute a Runnable class. After executing the Runnable, is it possible to check / monitor the thread with a separate service call?

I am still working and searching for some stuff like ExecutorService, but still no luck. Is there any other way to do this? Please suggest some probable solution that I can check. Sorry for my grammar. I hope I can explain it clear with my sample code below.

My code is something like this.

Running the thread on : http://ip:port/report

public String runReport {

    // Run the the runnable
    Report report = new Report();
    String threadName = "REPORT1";
    Thread t = new Thread(report, threadName);
    t.start();

    // return thread some details
    return t.getId() + "|" + t.hashCode();
}

My runnable class

public class Report {

    private String status;

    @Override
    public void run() {
        //Update status
        setStatus("Running");

        //... do stuff

        //Update status
        setStatus("End")
    }

    // Getter and Setter
}

My checker class on http://ip:port/report/check/some_param

public String check( int threadId ) {
    // Search the thread by threadId and check the current status
    //Report.getStatus();
}
  • `Thread.currentThread()`? – Lino Mar 26 '19 at 07:46
  • Apparently I cannot use the current thread in checking, since it was in different call. What I need is like get the thread with a specific ID that I pass during the call and then check the status of that thread. – YouDontKnowAboutBear Mar 26 '19 at 07:51
  • 2
    You need to maintain a `Map` that maps a thread id to the thread. But be careful: Once a thread is terminated, the id may be reused! There might be other (better) solutions, though. But then this question is a bit too broad. – Seelenvirtuose Mar 26 '19 at 07:57
  • Maybe this helps: https://stackoverflow.com/questions/1323408/get-a-list-of-all-threads-currently-running-in-java – Thilo Mar 26 '19 at 07:59
  • Thanks @Seelenvirtuose, now I get a new idea from your answer on maintaining a map. I can use also a table in DB for reports details. But still I am finding some ways to do it. – YouDontKnowAboutBear Mar 26 '19 at 08:06

2 Answers2

1

Using thread IDs may not be the best idea, especially because you're likely to use a pool that reuses threads. A simple solution is to generate IDs for your jobs and maintain a map that you can use to read the status.

As an example, you can use unique IDs for your task IDs:

Map<String, Report> jobs = new ConcurrentHashMap<>();
ExecutorService executorService = Executors.newFixedThreadPool(10); //just an example

public String runReport {

    // Run the the runnable
    Report report = new Report();

    //For a numeric sequence, you can use something like AtomicLong
    String jobId = UUID.randomUUID().toString();
    jobs.put(jobId, report);

    //using a thread pool may be a better idea.
    executorService.submit(report);

    // return the job ID
    return jobId;
}

And to check the status, you just read the map:

public String check(String jobId) {
    return jobs.get(jobId).getStatus(); //remember null checks
}

You would then just need to know when to remove entries from the map based on how you expect the check method to be called.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
1

Maintain the map in the class of your job id. Whenever that thread is initialized, pass the id in the constructor and when it starts processing put the status as running and when it gets completed, just before ending execution in run method, put the status as end. Something like below

public void run(){
     try{
     jobsMap.put(this.jobId, "Running");
     // your business logic here
     jobsMap.put(this.jobId,"Completed");
     } catch(Exception e){
       jobsMap.put(this.jobId,"Failed. Reason -"+e.getMessage);
       // exception handling
     }
}
Somil Aseeja
  • 150
  • 8