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,4 @@
$ function definition = priceOrder (binder: test, no broker)
in orders : {"orderId":"o-1","customerId":"alice","quantity":2,"unitPrice":9.5}
out invoices : {"orderId":"o-1","customerId":"alice","total":19.0}
out contentType header: application/json
@@ -0,0 +1,12 @@
$ function definition = orderSource, poller fixed-delay = 100 ms, demo.max-orders = 5
polled #1: {"orderId":"o-1","customerId":"bob","quantity":2,"unitPrice":9.5}
headers: [contentType, id, scst_partition, timestamp] scst_partition=0
polled #2: {"orderId":"o-2","customerId":"carol","quantity":3,"unitPrice":9.5}
headers: [contentType, id, scst_partition, timestamp] scst_partition=2
polled #3: {"orderId":"o-3","customerId":"dave","quantity":1,"unitPrice":9.5}
headers: [contentType, id, scst_partition, timestamp] scst_partition=1
polled #4: {"orderId":"o-4","customerId":"alice","quantity":2,"unitPrice":9.5}
headers: [contentType, id, scst_partition, timestamp] scst_partition=0
polled #5: {"orderId":"o-5","customerId":"bob","quantity":3,"unitPrice":9.5}
headers: [contentType, id, scst_partition, timestamp] scst_partition=0
messages sent before the supplier started returning null: 5
@@ -0,0 +1,4 @@
$ function definition = bill (max-attempts=3, back-off 200 ms)
good invoice sent -> log lines containing 'bill attempt for o-1': 1
poison invoice sent -> attempts: 3
send() returned normally
+14
View File
@@ -0,0 +1,14 @@
$ profile kafka, supplier capped at 12 orders (demo.max-orders=12), broker apache/kafka:4.4.0
billed lines in the application log: 12
topic invoices-k2 created by the binder with 1 partition(s)
topic orders-k2 created by the binder with 3 partition(s)
consumer group 'pricing': 3 members (concurrency=3), partitions per member [1, 1, 1]
records on orders-k2 by partition (customer of each record):
partition 0: [bob, alice, bob, alice, bob, alice]
partition 1: [dave, dave, dave]
partition 2: [carol, carol, carol]
records that carry a Kafka message key: 0
alice: in 1 partition(s), actually partition 0; murmur2 % 3 = 0, hashCode % 3 = 0
bob: in 1 partition(s), actually partition 0; murmur2 % 3 = 0, hashCode % 3 = 1
carol: in 1 partition(s), actually partition 2; murmur2 % 3 = 2, hashCode % 3 = 1
dave: in 1 partition(s), actually partition 1; murmur2 % 3 = 1, hashCode % 3 = 2
+14
View File
@@ -0,0 +1,14 @@
$ profile kafka, function definition = bill, enable-dlq=true, max-attempts=3
bill attempts: o-1=1 o-2=3 o-3=1
billed: o-1=1 o-2=0 o-3=1 (o-3 was behind the poison record on the same partition)
DLQ topic invoices-k3.billing.dlq holds 1 record(s)
payload: {"orderId":"o-2","customerId":"bob","total":-1.0}
header x-original-topic = invoices-k3
header x-original-partition = 0
header x-original-offset = 1
header x-original-timestamp = (epoch millis, omitted)
header x-original-timestamp-type = CreateTime
header x-exception-fqcn = org.springframework.messaging.MessagingException
header x-exception-message = error occurred in message handler [org.springframework.cloud.stream.function.FunctionConfi...
header x-exception-stacktrace = org.springframework.messaging.MessagingException: Retry policy for operation 'org.springfr...
stack trace mentions the real cause (IllegalArgumentException: Refusing to bill o-2): true
@@ -0,0 +1,17 @@
$ profile rabbit, 12 orders: alice, bob, carol, dave x 3, partition-count=3, consumer instance-count=3 instance-index=0
run A, no required-groups on the orders producer: billed 6 of 12
queues on the broker (name, messages, consumers):
orders-r4a.pricing-0 0 1
run B, required-groups=pricing on the orders producer: billed 6 of 12
queues on the broker (name, messages, consumers):
orders-r4b.pricing-0 0 1
orders-r4b.pricing-1 3 0
orders-r4b.pricing-2 3 0
run C, plus instance-index 1 and 2: billed 12 of 12
queues on the broker (name, messages, consumers):
orders-r4b.pricing-0 0 1
orders-r4b.pricing-1 0 1
orders-r4b.pricing-2 0 1
+25
View File
@@ -0,0 +1,25 @@
$ profile rabbit, function definition = bill, auto-bind-dlq=true, max-attempts=3
--- republish-to-dlq=true (what application-rabbit.yml sets) ---
bill attempts for the poison invoice: 3
queues (name, messages, consumers):
invoices-<run>.billing 0 1
invoices-<run>.billing.dlq 1 0
DLQ payload: {"orderId":"p-r5a","customerId":"alice","total":-1.0}
DLQ header names: [retry-count, x-exception-message, x-exception-stacktrace, x-original-exchange, x-original-routingKey]
x-exception-message = Retry policy for operation 'org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter$Listener$$Lambda/0x...' exhausted; aborting execution
x-original-exchange = invoices-<run>
x-original-routingKey = invoices-<run>
DLQ header has x-exception-stacktrace: true
--- republish-to-dlq=false (broker-side dead lettering only) ---
bill attempts for the poison invoice: 3
queues (name, messages, consumers):
invoices-<run>.billing 0 1
invoices-<run>.billing.dlq 1 0
DLQ payload: {"orderId":"p-r5b","customerId":"alice","total":-1.0}
DLQ header names: [x-death, x-first-death-exchange, x-first-death-queue, x-first-death-reason, x-last-death-exchange, x-last-death-queue, x-last-death-reason]
x-first-death-queue = invoices-<run>.billing
x-first-death-reason = rejected
x-death: reason=rejected queue=invoices-<run>.billing count=1
DLQ header has x-exception-stacktrace: false
@@ -0,0 +1,2 @@
$ both binder starters on the classpath, no spring.cloud.stream.default-binder, no per-binding binder
BeanCreationException: Error creating bean with name 'functionInitializer' defined in class path resource [org/springframework/cloud/stream/function/FunctionConfiguration.class]: A default binder has been requested, but there is more than one binder available for 'org.springframework.messaging.MessageChannel' : kafka,rabbit, and no default binder has been set.
@@ -0,0 +1,3 @@
$ three function beans (orderSource, priceOrder, bill), spring.cloud.function.definition left empty
beans of type Function: [functionRouter, priceOrder], Supplier: [orderSource], Consumer: [bill]
send threw NullPointerException: Cannot invoke "org.springframework.messaging.SubscribableChannel.send(org.springframework.messaging.Message)" because the return value of "org.springframework.cloud.stream.binder.test.InputDestination.getChannelByName(String)" is null
@@ -0,0 +1,2 @@
$ rabbit profile, but the producer binding priceOrder-out-0 has required-groups=billing and auto-bind-dlq=false
reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'x-dead-letter-exchange' for queue 'invoices-<run>.billing' in vhost '/': received the value 'DLX' of type 'longstr' but current is none
+9
View File
@@ -0,0 +1,9 @@
$ docker run <kafka broker> ; java -jar target/cloud-stream-demo-1.0.jar --spring.profiles.active=kafka --demo.max-orders=8 --spring.cloud.stream.kafka.binder.brokers=localhost:19092
com.ankurm.cloudstream.Pipeline : billed o-1 for 19.0
com.ankurm.cloudstream.Pipeline : billed o-2 for 28.5
com.ankurm.cloudstream.Pipeline : billed o-3 for 9.5
com.ankurm.cloudstream.Pipeline : billed o-4 for 19.0
com.ankurm.cloudstream.Pipeline : billed o-5 for 28.5
com.ankurm.cloudstream.Pipeline : billed o-6 for 9.5
com.ankurm.cloudstream.Pipeline : billed o-7 for 19.0
com.ankurm.cloudstream.Pipeline : billed o-8 for 28.5
+5
View File
@@ -0,0 +1,5 @@
$ docker run <rabbit broker> ; java -jar target/cloud-stream-demo-1.0.jar --spring.profiles.active=rabbit --demo.max-orders=8 --spring.rabbitmq.port=25672
com.ankurm.cloudstream.Pipeline : billed o-1 for 19.0
com.ankurm.cloudstream.Pipeline : billed o-4 for 19.0
com.ankurm.cloudstream.Pipeline : billed o-5 for 28.5
com.ankurm.cloudstream.Pipeline : billed o-8 for 28.5
+16
View File
@@ -0,0 +1,16 @@
$ grep -rilE 'kafka|rabbit' src/main/java | wc -l
0
$ grep -rilE 'kafka|rabbit' src/main/resources
src/main/resources/application-kafka.yml
src/main/resources/application-rabbit.yml
$ grep -E 'import' src/main/java/com/ankurm/cloudstream/Pipeline.java
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;
+6
View File
@@ -0,0 +1,6 @@
$ mvn dependency:tree -Dincludes=org.springframework.boot:spring-boot-starter-webflux
com.ankurm:cloud-stream-demo:jar:1.0
\- org.springframework.cloud:spring-cloud-starter-stream-rabbit:jar:5.1.0:compile
\- org.springframework.cloud:spring-cloud-stream-binder-rabbit:jar:5.1.0:compile
\- org.springframework.cloud:spring-cloud-stream-binder-rabbit-core:jar:5.1.0:compile
\- org.springframework.boot:spring-boot-starter-webflux:jar:4.2.0:compile
@@ -0,0 +1,14 @@
$ javap -p -c PartitionHandler$DefaultPartitionSelector (selectPartition and the murmur2 call)
public int selectPartition(java.lang.Object, int);
Code:
0: aload_1
1: invokevirtual #7 // Method java/lang/Object.toString:()Ljava/lang/String;
4: getstatic #11 // Field java/nio/charset/StandardCharsets.UTF_8:Ljava/nio/charset/Charset;
7: invokevirtual #17 // Method java/lang/String.getBytes:(Ljava/nio/charset/Charset;)[B
10: astore_3
11: aload_0
12: aload_3
13: invokevirtual #23 // Method murmur2:([B)I
16: ldc #29 // int 2147483647
18: iand
19: ireturn