Azure / azure-event-hubs-java

☁️ Java client library for Azure Event Hubs
https://azure.microsoft.com/services/event-hubs
MIT License
51 stars 60 forks source link

How to get the params in the samples? #461

Closed dahaibao closed 3 years ago

dahaibao commented 4 years ago

String consumerGroupName = "$Default"; String namespaceName = "----ServiceBusNamespaceName----"; String eventHubName = "----EventHubName----"; String sasKeyName = "----SharedAccessSignatureKeyName----"; String sasKey = "----SharedAccessSignatureKey----"; String storageConnectionString = "----AzureStorageConnectionString----"; String storageContainerName = "----StorageContainerName----"; String hostNamePrefix = "----HostNamePrefix----";

for example: final ConnectionStringBuilder connStr = new ConnectionStringBuilder() // .setEndpoint(new URI("Endpoint=sb://aaaa-event-hub.servicebus.chinacloudapi.cn/")) .setNamespaceName("aaaaa-event-hub") // to target National clouds - use .setEndpoint(URI) .setEventHubName("event-center-test") .setSasKeyName("RootManageSharedAccessKey") .setSasKey("OWoynSd50kpo32m8eoBiW6PIWdKQ/6Sx6M5oNjLSixxx");

but it doesn't work exception throws, need I use the method .setEndpoint(new URI("Endpoint=sb://aaaa.eventhub.servicebus.chinacloudapi.cn/")) to set the connections?

dahaibao commented 4 years ago

I mean what's the params mean and where to get them

JamesBirdsall commented 4 years ago

1) You are correct that you need to use setEndpoint. setNamespaceName assumes the event hubs namespace is in the Azure public cloud. For the event hub name and SAS key stuff, it looks like you have already figured it out. 2) storageConnectionString: EventProcessorHost requires a Storage account because it uses Storage blobs to handle partition leases and checkpointing. When you create a Storage account, the portal gives you a connection string for the account -- use that here. 3) storageContainerName: Azure Storage groups blobs into containers. Any valid container name is fine here. If the container does not exist, it will be created when EventProcessorHost starts. 4) hostNamePrefix: each instance of EventProcessorHost must have a unique name. The sample uses the utility method EventProcessorHost.createHostName, which takes the string you have supplied here and appends a GUID to make sure it's unique.

dahaibao commented 4 years ago

OK thanks for your reply. Firstly I want to run through the SimpleSend case. but it doesn't work, could you please tell me where I am wrong? thank you.

final ConnectionStringBuilder connStr = new ConnectionStringBuilder() .setEndpoint(new URI("Endpoint=sb://xxx-event-hub.servicebus.chinacloudapi.cn/")) .setNamespaceName("xxx-event-hub") .setEventHubName("event-center-test") .setSasKeyName("RootManageSharedAccessKey") // name space key .setSasKey("OWoynSd50kpo32m8eoBiW6PIWdKQ/6Sx6M5oNjLSiVxx");

    final Gson gson = new GsonBuilder().create();

    // The Executor handles all asynchronous tasks and this is passed to the EventHubClient instance.
    // This enables the user to segregate their thread pool based on the work load.
    // This pool can then be shared across multiple EventHubClient instances.
    // The following sample uses a single thread executor, as there is only one EventHubClient instance,
    // handling different flavors of ingestion to Event Hubs here.
    final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(4);

    // Each EventHubClient instance spins up a new TCP/SSL connection, which is expensive.
    // It is always a best practice to reuse these instances. The following sample shows this.
    final EventHubClient ehClient = EventHubClient.createFromConnectionStringSync(connStr.toString(), executorService);

    try {
        for (int i = 0; i < 100; i++) {

            String payload = "Message " + Integer.toString(i);
            //PayloadEvent payload = new PayloadEvent(i);
            byte[] payloadBytes = gson.toJson(payload).getBytes(Charset.defaultCharset());
            EventData sendEvent = EventData.create(payloadBytes);

            // Send - not tied to any partition
            // Event Hubs service will round-robin the events across all Event Hubs partitions.
            // This is the recommended & most reliable way to send to Event Hubs.
            ehClient.sendSync(sendEvent);
        }

        System.out.println(Instant.now() + ": Send Complete...");
        System.out.println("Press Enter to stop.");
        System.in.read();
    } finally {
        ehClient.closeSync();
        executorService.shutdown();
    }
JamesBirdsall commented 4 years ago

1) You only need to use setEndpoint. setNamespaceName sets the same information, it just assumes that the domain name is servicebus.windows.net, which only works for public Azure. 2) When creating the URI, don't put "Endpoint=" in the string.

dahaibao commented 4 years ago

Hi James, I changed the URI and still can not get the connection with event hub. I feel confused and below I pasted my real params.

    final ConnectionStringBuilder connStr = new ConnectionStringBuilder()
            .setEndpoint(new URI("sb://xxxxx-event-hub.servicebus.chinacloudapi.cn/"))
            .setNamespaceName("xxxx-event-hub") 
            .setEventHubName("event-center-test")
            .setSasKeyName("RootManageSharedAccessKey")
            .setSasKey("OWoynSd50kpo32m8eoBiW6PIWdKQ/6Sx6M5oNjLSiVxx");

Exception in thread "main" com.microsoft.azure.eventhubs.CommunicationException: A communication error has occurred. This may be due to an incorrect host name in your connection string or a problem with your network connection. at com.microsoft.azure.eventhubs.impl.ExceptionUtil.toException(ExceptionUtil.java:71)

JamesBirdsall commented 4 years ago

Let me reiterate (1) from the previous comment: remove the call to setNamespaceName! In the code as shown, it will overwrite the correct information from setEndpoint with bad information, and cause the failure you are seeing.

dahaibao commented 4 years ago

ok, thanks buddy. it's running.

dahaibao commented 4 years ago

ok, thanks buddy. it's running.