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;
}
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);
}
Aggregations