Unravel โบ Java concepts โบ Messaging and Kafka
Azure Service Bus, Event Hubs and Google Pub/Sub
Azure and Google offer managed brokers (Service Bus and Event Hubs; Pub/Sub).
At a glance
Different post companies with the same service.
- 1Service Bus has queues and topics with subscriptions; the processor client calls your handler for each message, and you complete, abandon or dead-letter itLocks on a message expire like the SQS visibility timeout.
- 2Event Hubs is closer to Kafka: partitions, a consumer group and checkpoints stored in blob storage that track progress per partition
- 3Pub/Sub: Publisher.publish(PubsubMessage) returns a future of the message idA Subscriber calls your MessageReceiver with an AckReplyConsumer; ack() removes the message, nack() redelivers it.
- 4Spring Cloud GCP and Spring Cloud Azure provide PubSubTemplate and Spring Integration adapters so you can use them like other messaging in Spring
- All of them deliver at least once
- Ordering is optional and scoped: Pub/Sub ordering keys, Service Bus sessions, Event Hubs partition keys
- Clients keep long-lived streaming connections (AMQP or gRPC) and receive messages pushed with flow control; unacknowledged messages are tracked by the service with a lease
ServiceBusProcessorClient processor = new ServiceBusClientBuilder() .credential(namespace, new DefaultAzureCredentialBuilder().build()) .processor() .queueName("orders") .processMessage(context -> handle(context.getMessage().getBody().toString())) .processError(context -> log.error("Service Bus error", context.getException())) .buildProcessorClient();processor.start();- line 1 uses ServiceBusProcessorClient, the core of Azure Service Bus, Event Hubs and Google Pub/Sub
Read the full explanation (about 1 min)
Azure and Google offer managed brokers (Service Bus and Event Hubs; Pub/Sub). The pattern is familiar: publish to a topic, subscribers receive and must acknowledge, and unacknowledged messages come back.
A mental model
Different post companies with the same service. You label a parcel and hand it over, and the recipient signs for it. If nobody signs, the courier tries again later.
How it works
Service Bus has queues and topics with subscriptions; the processor client calls your handler for each message, and you complete, abandon or dead-letter it. Locks on a message expire like the SQS visibility timeout.
Event Hubs is closer to Kafka: partitions, a consumer group and checkpoints stored in blob storage that track progress per partition.
Pub/Sub: Publisher.publish(PubsubMessage) returns a future of the message id. A Subscriber calls your MessageReceiver with an AckReplyConsumer; ack() removes the message, nack() redelivers it.
Spring Cloud GCP and Spring Cloud Azure provide PubSubTemplate and Spring Integration adapters so you can use them like other messaging in Spring.
Going deeper
All of them deliver at least once. Handlers must be idempotent, and the ack deadline (lock duration) must outlast processing, or work is duplicated.
Ordering is optional and scoped: Pub/Sub ordering keys, Service Bus sessions, Event Hubs partition keys. Without them, do not assume order.
Authenticate with managed identity or application default credentials, not connection strings with embedded keys, so no secret has to live in config.
Under the hood
Clients keep long-lived streaming connections (AMQP or gRPC) and receive messages pushed with flow control; unacknowledged messages are tracked by the service with a lease. The lease is extended automatically by the client library until your handler finishes, up to a maximum.
Common mistakes
- Ack before the work is finished.
- Assuming ordering without ordering keys or sessions.
- Connection strings with keys in source code.
- Not handling poison messages (no dead-letter).
How Unravel recognises it
When you paste code, Unravel looks for these signs of azure service bus, event hubs and google pub/sub. Spotting them yourself is a good habit when you read unfamiliar code:
- Types:
ServiceBusClientBuilderServiceBusSenderClientServiceBusProcessorClientServiceBusReceiverClientServiceBusMessageServiceBusReceivedMessageEventHubProducerClientEventHubClientBuilderEventProcessorClientEventProcessorClientBuilderEventDataPubsubMessageAckReplyConsumerMessageReceiverand 6 more
Part of a learning path
- Cloud-native Java ยท previous: DynamoDB access ยท next: Serverless function handlers (AWS Lambda, Azure, GCP)
Related concepts
Frequently asked questions
What is Azure Service Bus, Event Hubs and Google Pub/Sub?
Azure and Google offer managed brokers (Service Bus and Event Hubs; Pub/Sub). The pattern is familiar: publish to a topic, subscribers receive and must acknowledge, and unacknowledged messages come back.
What are the most common mistakes with Azure Service Bus, Event Hubs and Google Pub/Sub?
Ack before the work is finished. Assuming ordering without ordering keys or sessions. Connection strings with keys in source code. Not handling poison messages (no dead-letter).
How can I see Azure Service Bus, Event Hubs and Google Pub/Sub in real code?
Paste your own Java into Unravel and it will point out where azure service bus, event hubs and google pub/sub appears, explain how it works at the detail level you choose, and draw the flow of the code around it.