Search in sources :

Example 1 with SubscribeException

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

the class COMPSsStream method subscribe.

/*
     * ****************************************************************************************************************
     * CONSUMER METHODS
     * ****************************************************************************************************************
     */
public RegistrationId subscribe(List<String> topics) throws SubscribeException {
    // Create subscription id
    RegistrationId registrationId = new RegistrationId();
    // Create internal consumer
    Properties properties = new Properties();
    try (FileInputStream fis = new FileInputStream(new File(PROPERTIES_FILE_PATH_CONSUMER))) {
        properties.load(fis);
    } catch (IOException ioe) {
        throw new SubscribeException("ERROR: Cannot open properties file for subscription", ioe);
    }
    if (properties.getProperty("group.id") == null) {
        properties.setProperty("group.id", "group-" + new Random().nextInt(100_000));
    }
    KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
    consumer.subscribe(topics);
    // Update consumerId - consumer relation
    this.consumerIds2consumers.put(registrationId.getConsumerId(), consumer);
    // Update consumerId - topics relation
    this.consumerIds2topics.put(registrationId.getConsumerId(), topics);
    // Update subscriber - consumer relation
    List<String> consumers = this.subscriberIds2consumerIds.get(registrationId.getSubscriberId());
    if (consumers == null) {
        consumers = new LinkedList<>();
    }
    consumers.add(registrationId.getConsumerId());
    this.subscriberIds2consumerIds.put(registrationId.getSubscriberId(), consumers);
    this.consumerIds2subscriberIds.put(registrationId.getConsumerId(), registrationId.getSubscriberId());
    // Return the registration id
    return registrationId;
}
Also used : Random(java.util.Random) RegistrationId(streams.components.utils.RegistrationId) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) IOException(java.io.IOException) Properties(java.util.Properties) SubscribeException(streams.components.exceptions.SubscribeException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with SubscribeException

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

SubscribeException (streams.components.exceptions.SubscribeException)2 RegistrationId (streams.components.utils.RegistrationId)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 Random (java.util.Random)1 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)1 InvalidCredentialsException (streams.components.exceptions.InvalidCredentialsException)1 ConsumerException (streams.exceptions.ConsumerException)1