BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Messaging and Kafka

I/O and data

Azure Service Bus, Event Hubs and Google Pub/Sub

Azure and Google offer managed brokers (Service Bus and Event Hubs; Pub/Sub).

๐Ÿ”Ž Unravel this example

At a glance

Mental model

Different post companies with the same service.

What happens, in order
  1. 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.
  2. 2Event Hubs is closer to Kafka: partitions, a consumer group and checkpoints stored in blob storage that track progress per partition
  3. 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.
  4. 4Spring Cloud GCP and Spring Cloud Azure provide PubSubTemplate and Spring Integration adapters so you can use them like other messaging in Spring
Remember
Watch out
Ack before the work is finishedAssuming ordering without ordering keys or sessionsConnection strings with keys in source codeNot handling poison messages (no dead-letter)
Example (highlighted lines explain it)
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();
  1. 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:

Part of a learning path

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.