Java monitor multi threads outside the class












0














Since I don't have the code here I'll try to be as clear as I can...



I'm developing a rest service in java that will get some params (number of threads, ammount of messages) and will create the threads (via loop) and send this number of messages via MQ (I'm passing the number of mssages when creating the thread).



So for an example if someone sends 50 threads and 5000 msgs it will send 2.5M msgs...



Now my question is how could I create another rest service to monitor all those threads and give me a % of conclusions on the messages sent.



I'm considering calling this service to update a progress bar every 2 secs via ajax.










share|improve this question






















  • 50 * 5000 = 250,000
    – MyStackRunnethOver
    Nov 21 '18 at 22:46
















0














Since I don't have the code here I'll try to be as clear as I can...



I'm developing a rest service in java that will get some params (number of threads, ammount of messages) and will create the threads (via loop) and send this number of messages via MQ (I'm passing the number of mssages when creating the thread).



So for an example if someone sends 50 threads and 5000 msgs it will send 2.5M msgs...



Now my question is how could I create another rest service to monitor all those threads and give me a % of conclusions on the messages sent.



I'm considering calling this service to update a progress bar every 2 secs via ajax.










share|improve this question






















  • 50 * 5000 = 250,000
    – MyStackRunnethOver
    Nov 21 '18 at 22:46














0












0








0







Since I don't have the code here I'll try to be as clear as I can...



I'm developing a rest service in java that will get some params (number of threads, ammount of messages) and will create the threads (via loop) and send this number of messages via MQ (I'm passing the number of mssages when creating the thread).



So for an example if someone sends 50 threads and 5000 msgs it will send 2.5M msgs...



Now my question is how could I create another rest service to monitor all those threads and give me a % of conclusions on the messages sent.



I'm considering calling this service to update a progress bar every 2 secs via ajax.










share|improve this question













Since I don't have the code here I'll try to be as clear as I can...



I'm developing a rest service in java that will get some params (number of threads, ammount of messages) and will create the threads (via loop) and send this number of messages via MQ (I'm passing the number of mssages when creating the thread).



So for an example if someone sends 50 threads and 5000 msgs it will send 2.5M msgs...



Now my question is how could I create another rest service to monitor all those threads and give me a % of conclusions on the messages sent.



I'm considering calling this service to update a progress bar every 2 secs via ajax.







java multithreading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 22:35









Daniel Gontijo LopesDaniel Gontijo Lopes

1




1












  • 50 * 5000 = 250,000
    – MyStackRunnethOver
    Nov 21 '18 at 22:46


















  • 50 * 5000 = 250,000
    – MyStackRunnethOver
    Nov 21 '18 at 22:46
















50 * 5000 = 250,000
– MyStackRunnethOver
Nov 21 '18 at 22:46




50 * 5000 = 250,000
– MyStackRunnethOver
Nov 21 '18 at 22:46












1 Answer
1






active

oldest

votes


















0














A simplified approach is to create a class to keep track of the statistics the status bar will need to display. For example:



public class MessageCreatorProgress {
private final int totalMessagesToBeCreated;
private final AtomicInteger successCount;
private final AtomicInteger failureCount;

// constructor to initialize values
// increment methods
// get methods
}


In the initial request which starts the threads, construct the threads with a shared instance of an MessageCreatorProgress. For example:



// endpoint method to create a bunch of messages
public String startCreatingMessages(CreateMessagesRequest request) {
MessageCreatorProgress progress = new MessageCreatorProgress(requesst.getThreadCount * request.getMessageCountPerThread());

for (...) {
new MyMessageCreator(progress, request.getSomeParameter(), ....).start();
}

String messageProgressId = some unique value...
// Store MessageCreatorProgress in the session or some other shared memory,
// so it can be accessed by subsequent calls.
session.setAttribute(messageProgressId, progress);
return messageProgressId;
}


Each MyMessageCreator instance would for example call progress.incrementSuccess() as a last step, or progress.incrementFailure() for an exception.



The AJAX call passes the messageProgressId to the status endpoint which knows how to access the MessageCreatorProgress:



// endpoint method to get the message creation progress
// transform to JSON or whatever
public MessageCreatorProgress getMessageCreationProgress(String messageProgressId) {
return session.getAttribute(messageProgressId);
}


A more complex approach is to use a database - for example when the AJAX call will not hit the same server running the threads which are creating the messages. When a thread is successful or has an exception it can update a record associated with messageProgressId, and the AJAX endpoint checks the database and constructs a MessageCreatorProgress to return to the client.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421382%2fjava-monitor-multi-threads-outside-the-class%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    A simplified approach is to create a class to keep track of the statistics the status bar will need to display. For example:



    public class MessageCreatorProgress {
    private final int totalMessagesToBeCreated;
    private final AtomicInteger successCount;
    private final AtomicInteger failureCount;

    // constructor to initialize values
    // increment methods
    // get methods
    }


    In the initial request which starts the threads, construct the threads with a shared instance of an MessageCreatorProgress. For example:



    // endpoint method to create a bunch of messages
    public String startCreatingMessages(CreateMessagesRequest request) {
    MessageCreatorProgress progress = new MessageCreatorProgress(requesst.getThreadCount * request.getMessageCountPerThread());

    for (...) {
    new MyMessageCreator(progress, request.getSomeParameter(), ....).start();
    }

    String messageProgressId = some unique value...
    // Store MessageCreatorProgress in the session or some other shared memory,
    // so it can be accessed by subsequent calls.
    session.setAttribute(messageProgressId, progress);
    return messageProgressId;
    }


    Each MyMessageCreator instance would for example call progress.incrementSuccess() as a last step, or progress.incrementFailure() for an exception.



    The AJAX call passes the messageProgressId to the status endpoint which knows how to access the MessageCreatorProgress:



    // endpoint method to get the message creation progress
    // transform to JSON or whatever
    public MessageCreatorProgress getMessageCreationProgress(String messageProgressId) {
    return session.getAttribute(messageProgressId);
    }


    A more complex approach is to use a database - for example when the AJAX call will not hit the same server running the threads which are creating the messages. When a thread is successful or has an exception it can update a record associated with messageProgressId, and the AJAX endpoint checks the database and constructs a MessageCreatorProgress to return to the client.






    share|improve this answer


























      0














      A simplified approach is to create a class to keep track of the statistics the status bar will need to display. For example:



      public class MessageCreatorProgress {
      private final int totalMessagesToBeCreated;
      private final AtomicInteger successCount;
      private final AtomicInteger failureCount;

      // constructor to initialize values
      // increment methods
      // get methods
      }


      In the initial request which starts the threads, construct the threads with a shared instance of an MessageCreatorProgress. For example:



      // endpoint method to create a bunch of messages
      public String startCreatingMessages(CreateMessagesRequest request) {
      MessageCreatorProgress progress = new MessageCreatorProgress(requesst.getThreadCount * request.getMessageCountPerThread());

      for (...) {
      new MyMessageCreator(progress, request.getSomeParameter(), ....).start();
      }

      String messageProgressId = some unique value...
      // Store MessageCreatorProgress in the session or some other shared memory,
      // so it can be accessed by subsequent calls.
      session.setAttribute(messageProgressId, progress);
      return messageProgressId;
      }


      Each MyMessageCreator instance would for example call progress.incrementSuccess() as a last step, or progress.incrementFailure() for an exception.



      The AJAX call passes the messageProgressId to the status endpoint which knows how to access the MessageCreatorProgress:



      // endpoint method to get the message creation progress
      // transform to JSON or whatever
      public MessageCreatorProgress getMessageCreationProgress(String messageProgressId) {
      return session.getAttribute(messageProgressId);
      }


      A more complex approach is to use a database - for example when the AJAX call will not hit the same server running the threads which are creating the messages. When a thread is successful or has an exception it can update a record associated with messageProgressId, and the AJAX endpoint checks the database and constructs a MessageCreatorProgress to return to the client.






      share|improve this answer
























        0












        0








        0






        A simplified approach is to create a class to keep track of the statistics the status bar will need to display. For example:



        public class MessageCreatorProgress {
        private final int totalMessagesToBeCreated;
        private final AtomicInteger successCount;
        private final AtomicInteger failureCount;

        // constructor to initialize values
        // increment methods
        // get methods
        }


        In the initial request which starts the threads, construct the threads with a shared instance of an MessageCreatorProgress. For example:



        // endpoint method to create a bunch of messages
        public String startCreatingMessages(CreateMessagesRequest request) {
        MessageCreatorProgress progress = new MessageCreatorProgress(requesst.getThreadCount * request.getMessageCountPerThread());

        for (...) {
        new MyMessageCreator(progress, request.getSomeParameter(), ....).start();
        }

        String messageProgressId = some unique value...
        // Store MessageCreatorProgress in the session or some other shared memory,
        // so it can be accessed by subsequent calls.
        session.setAttribute(messageProgressId, progress);
        return messageProgressId;
        }


        Each MyMessageCreator instance would for example call progress.incrementSuccess() as a last step, or progress.incrementFailure() for an exception.



        The AJAX call passes the messageProgressId to the status endpoint which knows how to access the MessageCreatorProgress:



        // endpoint method to get the message creation progress
        // transform to JSON or whatever
        public MessageCreatorProgress getMessageCreationProgress(String messageProgressId) {
        return session.getAttribute(messageProgressId);
        }


        A more complex approach is to use a database - for example when the AJAX call will not hit the same server running the threads which are creating the messages. When a thread is successful or has an exception it can update a record associated with messageProgressId, and the AJAX endpoint checks the database and constructs a MessageCreatorProgress to return to the client.






        share|improve this answer












        A simplified approach is to create a class to keep track of the statistics the status bar will need to display. For example:



        public class MessageCreatorProgress {
        private final int totalMessagesToBeCreated;
        private final AtomicInteger successCount;
        private final AtomicInteger failureCount;

        // constructor to initialize values
        // increment methods
        // get methods
        }


        In the initial request which starts the threads, construct the threads with a shared instance of an MessageCreatorProgress. For example:



        // endpoint method to create a bunch of messages
        public String startCreatingMessages(CreateMessagesRequest request) {
        MessageCreatorProgress progress = new MessageCreatorProgress(requesst.getThreadCount * request.getMessageCountPerThread());

        for (...) {
        new MyMessageCreator(progress, request.getSomeParameter(), ....).start();
        }

        String messageProgressId = some unique value...
        // Store MessageCreatorProgress in the session or some other shared memory,
        // so it can be accessed by subsequent calls.
        session.setAttribute(messageProgressId, progress);
        return messageProgressId;
        }


        Each MyMessageCreator instance would for example call progress.incrementSuccess() as a last step, or progress.incrementFailure() for an exception.



        The AJAX call passes the messageProgressId to the status endpoint which knows how to access the MessageCreatorProgress:



        // endpoint method to get the message creation progress
        // transform to JSON or whatever
        public MessageCreatorProgress getMessageCreationProgress(String messageProgressId) {
        return session.getAttribute(messageProgressId);
        }


        A more complex approach is to use a database - for example when the AJAX call will not hit the same server running the threads which are creating the messages. When a thread is successful or has an exception it can update a record associated with messageProgressId, and the AJAX endpoint checks the database and constructs a MessageCreatorProgress to return to the client.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 23:21









        Andrew SAndrew S

        1,5451510




        1,5451510






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421382%2fjava-monitor-multi-threads-outside-the-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python