Azure / azure-event-hubs-go

Golang client library for Azure Event Hubs https://azure.microsoft.com/services/event-hubs
MIT License
89 stars 69 forks source link

Quick Start only works when sending and receiving Events, but doesn't work when just receiving #187

Open lumichael24 opened 4 years ago

lumichael24 commented 4 years ago

Expected Behavior

From the quick start code, if we remove the lines: signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, os.Interrupt, os.Kill) <-signalChan

the event hub handler should still receive events from the EventHub since the Receive function is blocking until there is a connection to the EventHub.

Actual Behavior

Removing those lines of code AND the line that sends a new string to the EventHub results in the EventHub being closed early and nothing being printed in the handler function. Is it the expected behavior/required to have the OS signal channel or something like time.Sleep for the connection to persist as well as the events be returned from the EventHub?

Full code attached below for reference

`

hub, err := eventhub.NewHubFromConnectionString(connStr)

if err != nil {
    fmt.Println(err)
    return
}

ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()

//hub.Send(ctx, eventhub.NewEventFromString("hello a different sdfsdf"))
handler := func(c context.Context, event *eventhub.Event) error {
    fmt.Println(string(event.Data))
    return nil
}

// listen to each partition of the Event Hub
runtimeInfo, err := hub.GetRuntimeInformation(ctx)
if err != nil {
    fmt.Println(err)
    return
}

for _, partitionID := range runtimeInfo.PartitionIDs {
    //eventhub.ReceiveWithLatestOffset()
    listenerHandle, err := hub.Receive(ctx, partitionID, handler, eventhub.ReceiveWithConsumerGroup("$Default"))
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(listenerHandle.Err())
}

//time.Sleep(10 * time.Second)
/*
    // Wait for a signal to quit:
    signalChan := make(chan os.Signal, 1)
    signal.Notify(signalChan, os.Interrupt, os.Kill)
    <-signalChan*/`

Environment

catalinaperalta commented 4 years ago

Hi! That's interesting, I'll look into this and get back to you. In the meantime, we have this other example for a consumer that only receives events: https://github.com/Azure/azure-event-hubs-go/blob/master/_examples/helloworld/consumer/main.go.

This is the link to the full project that demonstrates how to a producer sends messages and how the consumer receives them: https://github.com/Azure/azure-event-hubs-go/tree/master/_examples/helloworld.

Hopefully that helps for a quick start while I look into this.