Search in sources :

Example 1 with InvalidCredentialsException

use of streams.components.exceptions.InvalidCredentialsException in project compss by bsc-wdc.

the class Producer method sendMessages.

/**
 * Sends numMessages messages trough the given stream and closes it
 *
 * @param stream
 * @param numMessages
 * @return
 */
public static Integer sendMessages(COMPSsStream stream, int numMessages) {
    // Announce on stream
    if (DEBUG) {
        System.out.println("[LOG] Announcing stream write");
    }
    RegistrationId id = null;
    try {
        id = stream.announce();
    } catch (AnnounceException ae) {
        System.err.println("ERROR: Cannot announce publication");
        ae.printStackTrace();
        return -1;
    }
    // Send messages
    if (DEBUG) {
        System.out.println("[LOG] Sending " + numMessages + " messages");
    }
    for (int i = 0; i < numMessages; ++i) {
        // Send lots of regular messages with type message
        // Message of the form {"type":"message", t=12.234, k=1}
        String msg = String.format(Locale.ROOT, "{\"" + Messages.FIELD_TYPE + "\":\"" + Messages.TYPE_MESSAGE + "\", \"" + Messages.FIELD_T + "\":%.3f, \"" + Messages.FIELD_K + "\":%d}", System.nanoTime() * 1e-9, i);
        try {
            stream.publish(id, Topics.TOPIC_REGULAR_MESSAGES, msg);
        } catch (InvalidCredentialsException ice) {
            System.err.println("ERROR: Cannot send regular message");
            ice.printStackTrace();
            return -1;
        }
        // Every so often send a stats message
        if (i % FREQ_STATS_MSG == 0) {
            String statMsg = String.format(Locale.ROOT, "{\"" + Messages.FIELD_TYPE + "\":\"" + Messages.TYPE_STATS + "\", \"" + Messages.FIELD_T + "\":%.3f, \"" + Messages.FIELD_K + "\":%d}", System.nanoTime() * 1e-9, i);
            try {
                stream.publish(id, Topics.TOPIC_REGULAR_MESSAGES, statMsg);
            } catch (InvalidCredentialsException ice) {
                System.err.println("ERROR: Cannot send stat message");
                ice.printStackTrace();
                return -1;
            }
        }
        // Every so often send a sleep for a while
        if (i % FREQ_WAIT_TIME == 0) {
            try {
                Thread.sleep(WAIT_TIME);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
    // Send end message
    if (DEBUG) {
        System.out.println("[LOG] Sending end messge");
    }
    String endMsg = String.format(Locale.ROOT, "{\"" + Messages.FIELD_TYPE + "\":\"" + Messages.TYPE_END + "\", \"" + Messages.FIELD_T + "\":%.3f, \"" + Messages.FIELD_K + "\":%d}", System.nanoTime() * 1e-9, 0);
    try {
        stream.publish(id, Topics.TOPIC_SYSTEM_MESSAGES, endMsg);
    } catch (InvalidCredentialsException ice) {
        System.err.println("ERROR: Cannot send end message");
        ice.printStackTrace();
        return -1;
    }
    // Close stream
    if (DEBUG) {
        System.out.println("[LOG] Closing stream");
    }
    try {
        stream.close(id);
    } catch (InvalidCredentialsException ice) {
        System.err.println("ERROR: Cannot close stream");
        ice.printStackTrace();
        return -1;
    }
    // All ok
    if (DEBUG) {
        System.out.println("[LOG] DONE");
    }
    return 0;
}
Also used : AnnounceException(streams.components.exceptions.AnnounceException) InvalidCredentialsException(streams.components.exceptions.InvalidCredentialsException) RegistrationId(streams.components.utils.RegistrationId)

Example 2 with InvalidCredentialsException

use of streams.components.exceptions.InvalidCredentialsException 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

InvalidCredentialsException (streams.components.exceptions.InvalidCredentialsException)2 RegistrationId (streams.components.utils.RegistrationId)2 AnnounceException (streams.components.exceptions.AnnounceException)1 SubscribeException (streams.components.exceptions.SubscribeException)1 ConsumerException (streams.exceptions.ConsumerException)1