Search in sources :

Example 1 with ConsumerException

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());
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ConsumerException(streams.exceptions.ConsumerException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ConsumerException

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());
}
Also used : ProducerException(streams.exceptions.ProducerException) ConsumerException(streams.exceptions.ConsumerException) COMPSsStream(streams.components.COMPSsStream) Result(streams.types.Result)

Example 3 with ConsumerException

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);
}
Also used : InvalidCredentialsException(streams.components.exceptions.InvalidCredentialsException) RegistrationId(streams.components.utils.RegistrationId) ConsumerException(streams.exceptions.ConsumerException) SubscribeException(streams.components.exceptions.SubscribeException)

Aggregations

ConsumerException (streams.exceptions.ConsumerException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 COMPSsStream (streams.components.COMPSsStream)1 InvalidCredentialsException (streams.components.exceptions.InvalidCredentialsException)1 SubscribeException (streams.components.exceptions.SubscribeException)1 RegistrationId (streams.components.utils.RegistrationId)1 ProducerException (streams.exceptions.ProducerException)1 Result (streams.types.Result)1