Search in sources :

Example 1 with EventHubException

use of com.microsoft.azure.eventhubs.EventHubException in project samza by apache.

the class EventHubSystemProducer method init.

private void init() {
    LOG.info("Initializing EventHubSystemProducer");
    // Fetches the stream ids
    List<String> streamIds = config.getStreams(systemName);
    // partition count)
    for (String streamId : streamIds) {
        EventHubClientManager ehClient = eventHubClientManagerFactory.getEventHubClientManager(systemName, streamId, config);
        perStreamEventHubClientManagers.put(streamId, ehClient);
        ehClient.init();
    }
    // Create partition senders if required
    if (PartitioningMethod.PARTITION_KEY_AS_PARTITION.equals(partitioningMethod)) {
        // Create all partition senders
        perStreamEventHubClientManagers.forEach((streamId, samzaEventHubClient) -> {
            EventHubClient ehClient = samzaEventHubClient.getEventHubClient();
            try {
                Map<Integer, PartitionSender> partitionSenders = new HashMap<>();
                long timeoutMs = config.getRuntimeInfoWaitTimeMS(systemName);
                Integer numPartitions = ehClient.getRuntimeInformation().get(timeoutMs, TimeUnit.MILLISECONDS).getPartitionCount();
                for (int i = 0; i < numPartitions; i++) {
                    String partitionId = String.valueOf(i);
                    EventHubClientManager perPartitionClientManager = createOrGetEventHubClientManagerForPartition(streamId, i);
                    PartitionSender partitionSender = perPartitionClientManager.getEventHubClient().createPartitionSender(partitionId).get(DEFAULT_CREATE_PARTITION_SENDER_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
                    partitionSenders.put(i, partitionSender);
                }
                streamPartitionSenders.put(streamId, partitionSenders);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                String msg = "Failed to fetch number of Event Hub partitions for partition sender creation";
                throw new SamzaException(msg, e);
            } catch (EventHubException | IllegalArgumentException e) {
                String msg = "Creation of partition sender failed with exception";
                throw new SamzaException(msg, e);
            }
        });
    }
    isInitialized = true;
    LOG.info("EventHubSystemProducer initialized.");
}
Also used : HashMap(java.util.HashMap) EventHubException(com.microsoft.azure.eventhubs.EventHubException) SamzaException(org.apache.samza.SamzaException) PartitionSender(com.microsoft.azure.eventhubs.PartitionSender) EventHubClient(com.microsoft.azure.eventhubs.EventHubClient) EventHubClientManager(org.apache.samza.system.eventhub.EventHubClientManager) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with EventHubException

use of com.microsoft.azure.eventhubs.EventHubException in project samza by apache.

the class SamzaEventHubClientManager method init.

@Override
public void init() {
    String remoteHost = String.format(EVENTHUB_REMOTE_HOST_FORMAT, eventHubNamespace);
    LOG.info("Initializing SamzaEventHubClientManager for namespace: " + eventHubNamespace);
    try {
        ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder().setNamespaceName(eventHubNamespace).setEventHubName(entityPath).setSasKeyName(sasKeyName).setSasKey(sasKey);
        ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder().setNameFormat("Samza EventHubClient Thread-%d").setDaemon(true);
        eventHubClientExecutor = Executors.newFixedThreadPool(numClientThreads, threadFactoryBuilder.build());
        eventHubClient = EventHubClient.createSync(connectionStringBuilder.toString(), retryPolicy, eventHubClientExecutor);
    } catch (IOException | EventHubException e) {
        String msg = String.format("Creation of EventHub client failed for eventHub EntityPath: %s on remote host %s:%d", entityPath, remoteHost, ClientConstants.AMQPS_PORT);
        LOG.error(msg, e);
        throw new SamzaException(msg, e);
    }
    LOG.info("SamzaEventHubClientManager initialized for namespace: " + eventHubNamespace);
}
Also used : ConnectionStringBuilder(com.microsoft.azure.eventhubs.ConnectionStringBuilder) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) EventHubException(com.microsoft.azure.eventhubs.EventHubException) IOException(java.io.IOException) SamzaException(org.apache.samza.SamzaException)

Example 3 with EventHubException

use of com.microsoft.azure.eventhubs.EventHubException in project samza by apache.

the class EventHubConsoleConsumer method main.

public static void main(String[] args) throws EventHubException, IOException, ExecutionException, InterruptedException {
    Options options = new Options();
    options.addOption(CommandLineHelper.createOption(OPT_SHORT_EVENTHUB_NAME, OPT_LONG_EVENTHUB_NAME, OPT_ARG_EVENTHUB_NAME, true, OPT_DESC_EVENTHUB_NAME));
    options.addOption(CommandLineHelper.createOption(OPT_SHORT_NAMESPACE, OPT_LONG_NAMESPACE, OPT_ARG_NAMESPACE, true, OPT_DESC_NAMESPACE));
    options.addOption(CommandLineHelper.createOption(OPT_SHORT_KEY_NAME, OPT_LONG_KEY_NAME, OPT_ARG_KEY_NAME, true, OPT_DESC_KEY_NAME));
    options.addOption(CommandLineHelper.createOption(OPT_SHORT_TOKEN, OPT_LONG_TOKEN, OPT_ARG_TOKEN, true, OPT_DESC_TOKEN));
    CommandLineParser parser = new BasicParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
    } catch (Exception e) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(String.format("Error: %s%neh-console-consumer.sh", e.getMessage()), options);
        return;
    }
    String ehName = cmd.getOptionValue(OPT_SHORT_EVENTHUB_NAME);
    String namespace = cmd.getOptionValue(OPT_SHORT_NAMESPACE);
    String keyName = cmd.getOptionValue(OPT_SHORT_KEY_NAME);
    String token = cmd.getOptionValue(OPT_SHORT_TOKEN);
    consumeEvents(ehName, namespace, keyName, token);
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) EventHubException(com.microsoft.azure.eventhubs.EventHubException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

EventHubException (com.microsoft.azure.eventhubs.EventHubException)3 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 SamzaException (org.apache.samza.SamzaException)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ConnectionStringBuilder (com.microsoft.azure.eventhubs.ConnectionStringBuilder)1 EventHubClient (com.microsoft.azure.eventhubs.EventHubClient)1 PartitionSender (com.microsoft.azure.eventhubs.PartitionSender)1 HashMap (java.util.HashMap)1 TimeoutException (java.util.concurrent.TimeoutException)1 BasicParser (org.apache.commons.cli.BasicParser)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 HelpFormatter (org.apache.commons.cli.HelpFormatter)1 Options (org.apache.commons.cli.Options)1 EventHubClientManager (org.apache.samza.system.eventhub.EventHubClientManager)1