dev.responsive.kafka.api.async.AsyncFixedKeyProcessorSupplier Maven / Gradle / Ivy
/*
* Copyright 2024 Responsive Computing, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.responsive.kafka.api.async;
import static dev.responsive.kafka.api.async.internals.AsyncProcessor.createAsyncFixedKeyProcessor;
import static dev.responsive.kafka.api.async.internals.AsyncUtils.initializeAsyncBuilders;
import dev.responsive.kafka.api.async.internals.AsyncProcessor;
import dev.responsive.kafka.api.async.internals.stores.AbstractAsyncStoreBuilder;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.kafka.streams.processor.api.FixedKeyProcessor;
import org.apache.kafka.streams.processor.api.FixedKeyProcessorSupplier;
import org.apache.kafka.streams.processor.api.ProcessorSupplier;
import org.apache.kafka.streams.state.StoreBuilder;
/**
* A fixed-key version of the async processing api, for topologies that use the
* fixed-key PAPI. This should be used in place of
* {@link FixedKeyProcessorSupplier} from Kafka Streams.
*
* See javadocs for {@link AsyncProcessorSupplier} for full instructions and
* documentation on the async processing framework.
*/
public class AsyncFixedKeyProcessorSupplier
implements FixedKeyProcessorSupplier {
private final FixedKeyProcessorSupplier userProcessorSupplier;
private final Map> asyncStoreBuilders;
/**
* Create an AsyncProcessorSupplier that wraps a custom {@link ProcessorSupplier}
* to enable async processing. If you aren't using a fixed-key processor, use
* {@link AsyncProcessorSupplier#createAsyncProcessorSupplier} instead
*
* @param processorSupplier the {@link FixedKeyProcessorSupplier} that returns a (new)
* instance of your custom {@link FixedKeyProcessor} on each
* invocation of {@link ProcessorSupplier#get}
*/
@SuppressWarnings("checkstyle:linelength")
public static AsyncFixedKeyProcessorSupplier createAsyncProcessorSupplier(
final FixedKeyProcessorSupplier processorSupplier
) {
return new AsyncFixedKeyProcessorSupplier<>(processorSupplier, processorSupplier.stores());
}
private AsyncFixedKeyProcessorSupplier(
final FixedKeyProcessorSupplier userProcessorSupplier,
final Set> userStoreBuilders
) {
this.userProcessorSupplier = userProcessorSupplier;
this.asyncStoreBuilders = initializeAsyncBuilders(userStoreBuilders);
}
@Override
public AsyncProcessor get() {
return createAsyncFixedKeyProcessor(userProcessorSupplier.get(), asyncStoreBuilders);
}
@Override
public Set> stores() {
return new HashSet<>(asyncStoreBuilders.values());
}
}