|
| 1 | +/* |
| 2 | + * Copyright 2023-2023 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.integration.debezium.dsl; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.concurrent.ThreadFactory; |
| 22 | + |
| 23 | +import io.debezium.engine.ChangeEvent; |
| 24 | +import io.debezium.engine.DebeziumEngine; |
| 25 | +import io.debezium.engine.Header; |
| 26 | +import io.debezium.engine.format.SerializationFormat; |
| 27 | + |
| 28 | +import org.springframework.integration.debezium.inbound.DebeziumMessageProducer; |
| 29 | +import org.springframework.integration.debezium.support.DefaultDebeziumHeaderMapper; |
| 30 | +import org.springframework.integration.dsl.MessageProducerSpec; |
| 31 | +import org.springframework.messaging.support.HeaderMapper; |
| 32 | +import org.springframework.scheduling.concurrent.CustomizableThreadFactory; |
| 33 | + |
| 34 | +/** |
| 35 | + * A {@link org.springframework.integration.dsl.MessageProducerSpec} for {@link DebeziumMessageProducer}. |
| 36 | + * |
| 37 | + * @author Christian Tzolov |
| 38 | + * |
| 39 | + * @since 6.2 |
| 40 | + */ |
| 41 | +public class DebeziumMessageProducerSpec |
| 42 | + extends MessageProducerSpec<DebeziumMessageProducerSpec, DebeziumMessageProducer> { |
| 43 | + |
| 44 | + protected DebeziumMessageProducerSpec(DebeziumEngine.Builder<ChangeEvent<byte[], byte[]>> debeziumEngineBuilder) { |
| 45 | + super(new DebeziumMessageProducer(debeziumEngineBuilder)); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Enable the {@link ChangeEvent} batch mode handling. When enabled the channel adapter will send a {@link List} of |
| 50 | + * {@link ChangeEvent}s as a payload in a single downstream {@link org.springframework.messaging.Message}. |
| 51 | + * Such a batch payload is not serializable. |
| 52 | + * By default, the batch mode is disabled, e.g. every input {@link ChangeEvent} is converted into a |
| 53 | + * single downstream {@link org.springframework.messaging.Message}. |
| 54 | + * @param enable set to true to enable the batch mode. Disabled by default. |
| 55 | + * @return the spec. |
| 56 | + */ |
| 57 | + public DebeziumMessageProducerSpec enableBatch(boolean enable) { |
| 58 | + this.target.setEnableBatch(enable); |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Enable support for tombstone (aka delete) messages. On a database row delete, Debezium can send a tombstone |
| 64 | + * change event that has the same key as the deleted row and a value of {@link Optional#empty()}. This record is a |
| 65 | + * marker for downstream processors. It indicates that log compaction can remove all records that have this key. |
| 66 | + * When the tombstone functionality is enabled in the Debezium connector configuration you should enable the empty |
| 67 | + * payload as well. |
| 68 | + * @param enabled set true to enable the empty payload. Disabled by default. |
| 69 | + * @return the spec. |
| 70 | + */ |
| 71 | + public DebeziumMessageProducerSpec enableEmptyPayload(boolean enabled) { |
| 72 | + this.target.setEnableEmptyPayload(enabled); |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Set a {@link ThreadFactory} for the Debezium executor. Defaults to the {@link CustomizableThreadFactory} with a |
| 78 | + * {@code debezium:inbound-channel-adapter-thread-} prefix. |
| 79 | + * @param threadFactory the {@link ThreadFactory} instance to use. |
| 80 | + * @return the spec. |
| 81 | + */ |
| 82 | + public DebeziumMessageProducerSpec threadFactory(ThreadFactory threadFactory) { |
| 83 | + this.target.setThreadFactory(threadFactory); |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Set the outbound message content type. Must be aligned with the {@link SerializationFormat} configuration used by |
| 89 | + * the provided {@link DebeziumEngine}. |
| 90 | + * @param contentType payload content type. |
| 91 | + * @return the spec. |
| 92 | + */ |
| 93 | + public DebeziumMessageProducerSpec contentType(String contentType) { |
| 94 | + this.target.setContentType(contentType); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Comma-separated list of names of {@link ChangeEvent} headers to be mapped into outbound Message headers. |
| 100 | + * Debezium's NewRecordStateExtraction 'add.headers' property configures the metadata to be used as |
| 101 | + * {@link ChangeEvent} headers. |
| 102 | + * <p> |
| 103 | + * You should prefix the names passed to the 'headerNames' with the prefix configured by the Debezium |
| 104 | + * 'add.headers.prefix' property. Later defaults to '__'. For example for 'add.headers=op,name' and |
| 105 | + * 'add.headers.prefix=__' you should use header hames like: '__op', '__name'. |
| 106 | + * @param headerNames The values in this list can be a simple patterns to be matched against the header names. |
| 107 | + * @return the spec. |
| 108 | + */ |
| 109 | + public DebeziumMessageProducerSpec headerNames(String... headerNames) { |
| 110 | + DefaultDebeziumHeaderMapper headerMapper = new DefaultDebeziumHeaderMapper(); |
| 111 | + headerMapper.setHeaderNamesToMap(headerNames); |
| 112 | + |
| 113 | + return headerMapper(headerMapper); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Set a {@link HeaderMapper} to convert the {@link ChangeEvent} headers |
| 118 | + * into {@link org.springframework.messaging.Message} headers. |
| 119 | + * @param headerMapper {@link HeaderMapper} implementation to use. Defaults to {@link DefaultDebeziumHeaderMapper}. |
| 120 | + * @return the spec. |
| 121 | + */ |
| 122 | + public DebeziumMessageProducerSpec headerMapper(HeaderMapper<List<Header<Object>>> headerMapper) { |
| 123 | + this.target.setHeaderMapper(headerMapper); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments