Add cloud-stream module: Spring Cloud Stream 5.1 on Boot 4.2 with Kafka and RabbitMQ binders

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b5uEyg58znuchZ8C8i
This commit is contained in:
asmhatre
2026-11-29 13:20:43 +05:30
co-authored by Claude Sonnet 5.5
parent e54cf840fd
commit e2c42dc268
40 changed files with 1244 additions and 0 deletions
@@ -0,0 +1,12 @@
package com.ankurm.cloudstream;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CloudStreamApplication {
public static void main(String[] args) {
SpringApplication.run(CloudStreamApplication.class, args);
}
}
@@ -0,0 +1,5 @@
package com.ankurm.cloudstream;
/** What the function emits and the consumer receives. */
public record Invoice(String orderId, String customerId, double total) {
}
@@ -0,0 +1,5 @@
package com.ankurm.cloudstream;
/** What the supplier emits and the function consumes. Serialized as JSON by Spring Cloud Stream. */
public record Order(String orderId, String customerId, int quantity, double unitPrice) {
}
@@ -0,0 +1,62 @@
package com.ankurm.cloudstream;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* The whole application: three plain java.util.function beans. Nothing here imports a binder or a
* broker client, which is what makes switching brokers a configuration change.
*
* <pre>
* orderSource (Supplier) --&gt; orders --&gt; priceOrder (Function) --&gt; invoices --&gt; bill (Consumer)
* </pre>
*/
@Configuration(proxyBeanMethods = false)
public class Pipeline {
private static final Logger log = LoggerFactory.getLogger(Pipeline.class);
private static final List<String> CUSTOMERS = List.of("alice", "bob", "carol", "dave");
/**
* Polled by the framework (every second by default), one order per call. A supplier that returns
* {@code null} sends nothing, which is how {@code demo.max-orders} makes the demos finite (0 = unbounded).
*/
@Bean
public Supplier<Order> orderSource(@Value("${demo.max-orders:0}") int maxOrders) {
AtomicInteger n = new AtomicInteger();
return () -> {
int i = n.incrementAndGet();
if (maxOrders > 0 && i > maxOrders) {
return null;
}
return new Order("o-" + i, CUSTOMERS.get(i % CUSTOMERS.size()), 1 + i % 3, 9.5);
};
}
/** Input and output are both typed; the framework converts JSON to and from the records. */
@Bean
public Function<Order, Invoice> priceOrder() {
return order -> new Invoice(order.orderId(), order.customerId(), order.quantity() * order.unitPrice());
}
/** A negative total is the "poison" in the dead-letter demos: this consumer throws on it. */
@Bean
public Consumer<Invoice> bill() {
return invoice -> {
log.info("bill attempt for {}", invoice.orderId());
if (invoice.total() < 0) {
throw new IllegalArgumentException("Refusing to bill " + invoice.orderId() + ": negative total");
}
log.info("billed {} for {}", invoice.orderId(), invoice.total());
};
}
}