use of streams.exceptions.ConsumerException in project compss by bsc-wdc.
the class Consumer method processRegularRecord.
private static void processRegularRecord(String value) throws ConsumerException {
// Read the encoded information in the message
JsonNode msg = null;
try {
msg = new ObjectMapper().readTree(value);
} catch (JsonProcessingException jpe) {
jpe.printStackTrace();
throw new ConsumerException("ERROR: Cannot read regular message", jpe);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new ConsumerException("ERROR: Cannot read regular message", ioe);
}
// Retrieve the message type
switch(msg.get(Messages.FIELD_TYPE).asText()) {
case Messages.TYPE_MESSAGE:
long latency = (long) ((System.nanoTime() * 1e-9 - msg.get(Messages.FIELD_T).asDouble()) * 1_000);
Result.addValue(latency);
break;
case Messages.TYPE_STATS:
// Whenever we receive a stats message we log its retrieval
if (DEBUG) {
System.out.println("[LOG] Stats message received");
}
break;
default:
throw new ConsumerException("ERROR: Unrecognised message type: " + msg.get(Messages.FIELD_TYPE).asText());
}
}
use of streams.exceptions.ConsumerException in project compss by bsc-wdc.
the class Main method main.
public static void main(String[] args) throws ProducerException, ConsumerException {
// Check and get parameters
if (args.length != 1) {
System.out.println("[ERROR] Bad number of parameters");
System.out.println(" Usage: streams.Main <numMessages>");
System.exit(-1);
}
int numMessages = Integer.parseInt(args[0]);
// Add a sleep to wait for both workers to be ready
try {
Thread.sleep(WAIT_FOR_RUNTIME);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// ------------------------------------------------------------------------
// Create stream
COMPSsStream stream = new COMPSsStream();
// Launch producer task
Integer exitP = Producer.sendMessages(stream, numMessages);
// Launch consumer task
Result resultC = Consumer.receiveMessages(stream);
// Synchronize
if (exitP != 0) {
throw new ProducerException("ERROR: Producer ended with exitValue " + exitP);
}
Integer exitC = resultC.getExitValue();
if (exitC != 0) {
throw new ConsumerException("ERROR: Consumer ended with exitValue " + exitC);
}
System.out.println("Consumer has returned:");
System.out.println("- NumMessages = " + resultC.getNumMessages());
System.out.println("- AcumValue = " + resultC.getAccumValue());
}
use of streams.exceptions.ConsumerException in project compss by bsc-wdc.
the class Consumer method receiveMessages.
/**
* Process the stream messages until and EOS is received
*
* @param stream
* @return
*/
public static Result receiveMessages(COMPSsStream stream) {
// Subscribe
if (DEBUG) {
System.out.println("[LOG] Subscribing to stream");
}
RegistrationId id;
try {
id = stream.subscribe(Topics.ALL_TOPICS);
} catch (SubscribeException se) {
System.err.println("ERROR: Cannot subscribe to stream");
se.printStackTrace();
return Result.generate(-1);
}
// Process stream
if (DEBUG) {
System.out.println("[LOG] Processing stream records");
}
try {
boolean end = false;
while (!end) {
// Read all the records within the timeout
// We don't care about timeout because we will receive an end message
ConsumerRecords<String, String> records = stream.poll(id, MESSAGE_TIMEOUT);
// Process the received records
end = processRecords(records);
}
} catch (InvalidCredentialsException ice) {
System.err.println("ERROR: Cannot receive messages from stream");
ice.printStackTrace();
return Result.generate(-2);
} catch (ConsumerException ce) {
System.err.println("ERROR: Cannot process received records");
ce.printStackTrace();
return Result.generate(-3);
} finally {
// Close stream
if (DEBUG) {
System.out.println("[LOG] Closing stream");
}
try {
stream.unsubscribe(id);
} catch (InvalidCredentialsException ice) {
System.err.println("ERROR: Cannot unsubscribe from stream");
ice.printStackTrace();
return Result.generate(-4);
}
}
// Set all ok on result
if (DEBUG) {
System.out.println("[LOG] DONE");
}
// Return result
return Result.generate(0);
}
Aggregations