Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen1.5-4b-chat· Alibaba (Qwen)Hugging Face Arena Human Preference 55K

I have event hub, topic and subscription created in azure. I want to write java 17…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Metallama-2-70b-chat’s answer.
  1. User

    I have event hub, topic and subscription created in azure. I want to write java 17 application to consume messages from topic. Can you write example code please

  2. Qwen Assistant

    Sure! Here is an example of a Java 17 application that consumes messages from an Azure Event Hub topic:

    import reactor.core.publisher.Flux;
    import reactor.core.publisher.Mono;
    import reactor.core.publisher.MonoSource;
    import reactor.core.publisher.BroadcastEmitter;
    import reactor.core.publisher.FluxSink;
    
    import java.time.Duration;
    import java.time.Instant;
    import java.util.concurrent.CompletionStage;
    
    public class EventHubConsumer {
        private final String connectionString;
        private final String topicName;
        private final String subscriptionName;
    
        public EventHubConsumer(String connectionString, String topicName, String subscriptionName) {
            this.connectionString = connectionString;
            this.topicName = topicName;
            this.subscriptionName = subscriptionName;
        }
    
        public Mono<Void> consumeMessages() {
            returnMonoSource()
                    .fromCallable(() -> {
                        EventHubClient client = EventHubClientBuilder.fromConnectionString(connectionString).build();
    
                        return client.createConsumer(topicName, subscriptionName)
                                .map(event -> {
                                    System.out.println("Received event: " + event);
                                    return null;
                                })
                                .doOnCancel(() -> client.close())
                                .doOnComplete(() -> {
                                    System.out.println("Finished consuming messages");
                                })
                                .subscribe();
                    })
                    .then(Mono::empty);
        }
    }
    

    This code uses the Reactor library to create a Mono that consumes messages from the specified Event Hub topic. The fromCallable method creates a Mono that calls a lambda function to create an EventHubConsumer and consume messages from the topic. The map method then processes each message received and returns it. The doOnCancel and doOnComplete methods handle the cancellation and completion of the operation. Note that this code assumes that you have already configured a connection string for your Event Hub, as well as the topic and subscription names. You can obtain these values by navigating to the Azure portal and looking at the Event Hub connection string.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.