Java monitor multi threads outside the class
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
50 * 5000 = 250,000
– MyStackRunnethOver
Nov 21 at 22:46
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
java multithreading
asked Nov 21 at 22:35
Daniel Gontijo Lopes
1
1
50 * 5000 = 250,000
– MyStackRunnethOver
Nov 21 at 22:46
add a comment |
50 * 5000 = 250,000
– MyStackRunnethOver
Nov 21 at 22:46
50 * 5000 = 250,000
– MyStackRunnethOver
Nov 21 at 22:46
50 * 5000 = 250,000
– MyStackRunnethOver
Nov 21 at 22:46
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 21 at 23:21
Andrew S
1,4721510
1,4721510
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
50 * 5000 = 250,000
– MyStackRunnethOver
Nov 21 at 22:46