|
| 1 | +/* |
| 2 | + * Copyright 2019-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.streams.messaging; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 24 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 25 | +import org.apache.kafka.common.header.Headers; |
| 26 | +import org.apache.kafka.common.record.TimestampType; |
| 27 | +import org.apache.kafka.streams.kstream.Transformer; |
| 28 | +import org.apache.kafka.streams.processor.api.ContextualProcessor; |
| 29 | +import org.apache.kafka.streams.processor.api.ProcessorContext; |
| 30 | +import org.apache.kafka.streams.processor.api.Record; |
| 31 | +import org.apache.kafka.streams.processor.api.RecordMetadata; |
| 32 | + |
| 33 | +import org.springframework.kafka.support.KafkaHeaders; |
| 34 | +import org.springframework.kafka.support.converter.MessagingMessageConverter; |
| 35 | +import org.springframework.messaging.Message; |
| 36 | +import org.springframework.util.Assert; |
| 37 | + |
| 38 | +/** |
| 39 | + * A {@link Transformer} implementation that invokes a {@link MessagingFunction} |
| 40 | + * converting to/from spring-messaging {@link Message}. Can be used, for example, |
| 41 | + * to invoke a Spring Integration flow. |
| 42 | + * |
| 43 | + * @param <Kin> the input key type. |
| 44 | + * @param <Vin> the input value type. |
| 45 | + * @param <Kout> the output key type. |
| 46 | + * @param <Vout> the output value type. |
| 47 | + * |
| 48 | + * @author Gary Russell |
| 49 | + * @since 2.3 |
| 50 | + * |
| 51 | + */ |
| 52 | +public class MessagingProcessor<Kin, Vin, Kout, Vout> extends ContextualProcessor<Kin, Vin, Kout, Vout> { |
| 53 | + |
| 54 | + private final MessagingFunction function; |
| 55 | + |
| 56 | + private final MessagingMessageConverter converter; |
| 57 | + |
| 58 | + /** |
| 59 | + * Construct an instance with the provided function and converter. |
| 60 | + * @param function the function. |
| 61 | + * @param converter the converter. |
| 62 | + */ |
| 63 | + public MessagingProcessor(MessagingFunction function, MessagingMessageConverter converter) { |
| 64 | + Assert.notNull(function, "'function' cannot be null"); |
| 65 | + Assert.notNull(converter, "'converter' cannot be null"); |
| 66 | + this.function = function; |
| 67 | + this.converter = converter; |
| 68 | + } |
| 69 | + |
| 70 | + @SuppressWarnings("unchecked") |
| 71 | + @Override |
| 72 | + public void process(Record<Kin, Vin> record) { |
| 73 | + ProcessorContext<Kout, Vout> context = context(); |
| 74 | + RecordMetadata meta = context.recordMetadata().orElse(null); |
| 75 | + Assert.state(meta != null, "No record metadata present"); |
| 76 | + Headers headers = record.headers(); |
| 77 | + ConsumerRecord<Object, Object> rebuilt = new ConsumerRecord<Object, Object>(meta.topic(), |
| 78 | + meta.partition(), meta.offset(), |
| 79 | + record.timestamp(), TimestampType.NO_TIMESTAMP_TYPE, |
| 80 | + 0, 0, |
| 81 | + record.key(), record.value(), |
| 82 | + headers, Optional.empty()); |
| 83 | + Message<?> message = this.converter.toMessage(rebuilt, null, null, null); |
| 84 | + message = this.function.exchange(message); |
| 85 | + List<String> headerList = new ArrayList<>(); |
| 86 | + headers.forEach(header -> headerList.add(header.key())); |
| 87 | + headerList.forEach(name -> headers.remove(name)); |
| 88 | + ProducerRecord<?, ?> fromMessage = this.converter.fromMessage(message, "dummy"); |
| 89 | + fromMessage.headers().forEach(header -> { |
| 90 | + if (!header.key().equals(KafkaHeaders.TOPIC)) { |
| 91 | + headers.add(header); |
| 92 | + } |
| 93 | + }); |
| 94 | + context.forward(new Record<>((Kout) message.getHeaders().get(KafkaHeaders.KEY), (Vout) message.getPayload(), |
| 95 | + record.timestamp(), headers)); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void close() { |
| 100 | + // NO-OP |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments