/** * Synchronous version of {@link #create(String, ScheduledExecutorService)}. * * @param connectionString The connection string to be used. See {@link ConnectionStringBuilder} to construct a connectionString. * @param executor An {@link ScheduledExecutorService} to run all tasks performed by {@link EventHubClient}. * @return EventHubClient which can be used to create Senders and Receivers to EventHub * @throws EventHubException If Service Bus service encountered problems during connection creation. * @throws IOException If the underlying Proton-J layer encounter network errors. */ static EventHubClient createSync(final String connectionString, final ScheduledExecutorService executor) throws EventHubException, IOException { return EventHubClient.createSync(connectionString, null, executor); }
public void startup() throws EventHubException, IOException { this.eventHubSender = EventHubClient.createSync(this.eventHubConnectionString, EXECUTOR_SERVICE); } }
private EventHubClient createEventHubClient(String eventHubName) { try { return EventHubClient.createSync(this.connectionStringProvider.getConnectionString(eventHubName), Executors.newSingleThreadScheduledExecutor()); } catch (EventHubException | IOException e) { throw new EventHubRuntimeException("Error when creating event hub client", e); } }
private EventHubClient createEventHubClient(String eventHubName) { try { return EventHubClient.createSync(this.connectionStringProvider.getConnectionString(eventHubName), Executors.newSingleThreadExecutor()); } catch (EventHubException | IOException e) { throw new EventHubRuntimeException("Error when creating event hub client", e); } }
private static EventHubClient receiveEventsAsync(final String partitionId) { EventHubClient client = null; try { client = EventHubClient.createSync(EVENTHUB_CONN_STR, Para.getExecutorService()); client.createReceiver(EventHubClient.DEFAULT_CONSUMER_GROUP_NAME, partitionId, EventPosition.fromEnqueuedTime(Instant.now())). thenAccept(new Receiver(partitionId)); } catch (Exception e) { logger.warn("Couldn't start receiving messages from Azure cloud: {}", e.getMessage()); } return client; }
private static EventHubClient receiveEventsAsync(final String partitionId) { EventHubClient client = null; try { client = EventHubClient.createSync(EVENTHUB_CONN_STR, Para.getExecutorService()); client.createReceiver(EventHubClient.DEFAULT_CONSUMER_GROUP_NAME, partitionId, EventPosition.fromEnqueuedTime(Instant.now())). thenAccept(new Receiver(partitionId)); } catch (Exception e) { logger.warn("Couldn't start receiving messages from Azure cloud: {}", e.getMessage()); } return client; }
@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); }
@Test public void doAzureEventSourceSendTest() throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(); final ConnectionStringBuilder connStr = new ConnectionStringBuilder().setNamespaceName("sitewhere") .setEventHubName("events").setSasKeyName("RootManageSharedAccessKey").setSasKey("xxx"); byte[] payloadBytes = EventsHelper.generateJsonMeasurementsMessage(DEVICE_TOKEN); EventData sendEvent = EventData.create(payloadBytes); final EventHubClient ehClient = EventHubClient.createSync(connStr.toString(), executor); ehClient.sendSync(sendEvent); ehClient.closeSync(); executor.shutdown(); }