-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pulsar KeyValueExtractors Fix for Pulsar OutgoingMessage.of factory method which did not set the key correctly
- Loading branch information
1 parent
8fcdb37
commit d3d238e
Showing
17 changed files
with
562 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
.../io/smallrye/reactive/messaging/pulsar/converters/KeyValueFromPulsarMessageExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.smallrye.reactive.messaging.pulsar.converters; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Optional; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.Message; | ||
|
||
import io.smallrye.reactive.messaging.keyed.KeyValueExtractor; | ||
import io.smallrye.reactive.messaging.providers.helpers.TypeUtils; | ||
import io.smallrye.reactive.messaging.pulsar.PulsarIncomingMessageMetadata; | ||
|
||
/** | ||
* Key/Value extractor extracting the key from a Pulsar message and passing the message's payload as value. | ||
* | ||
* This extractor has the default priority ({@link KeyValueExtractor#DEFAULT_PRIORITY}). | ||
*/ | ||
@ApplicationScoped | ||
public class KeyValueFromPulsarMessageExtractor implements KeyValueExtractor { | ||
@Override | ||
public boolean canExtract(Message<?> message, Type keyType, Type valueType) { | ||
Optional<PulsarIncomingMessageMetadata> metadata = message.getMetadata(PulsarIncomingMessageMetadata.class); | ||
// The type checks can be expensive, so, we do it only once, and rely on the fact the pulsar schema are constant. | ||
return metadata.filter( | ||
incomingMetadata -> (incomingMetadata.hasKey() | ||
&& TypeUtils.isAssignable(keyType, incomingMetadata.getKey().getClass()) | ||
&& TypeUtils.isAssignable(valueType, message.getPayload().getClass()))) | ||
.isPresent(); | ||
} | ||
|
||
@Override | ||
public Object extractKey(Message<?> message, Type keyType) { | ||
return message.getMetadata(PulsarIncomingMessageMetadata.class) | ||
.<Object> map(PulsarIncomingMessageMetadata::getKey) | ||
.orElseThrow(); | ||
} | ||
|
||
@Override | ||
public Object extractValue(Message<?> message, Type valueType) { | ||
return message.getPayload(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...lrye/reactive/messaging/pulsar/converters/KeyValueFromPulsarMessageKeyValueExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.smallrye.reactive.messaging.pulsar.converters; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Optional; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import org.apache.pulsar.common.schema.KeyValue; | ||
import org.eclipse.microprofile.reactive.messaging.Message; | ||
|
||
import io.smallrye.reactive.messaging.keyed.KeyValueExtractor; | ||
import io.smallrye.reactive.messaging.providers.helpers.TypeUtils; | ||
import io.smallrye.reactive.messaging.pulsar.PulsarIncomingMessageMetadata; | ||
|
||
/** | ||
* Key/Value extractor extracting the key and value from a Pulsar message with KeyValue schema. | ||
* | ||
* This extractor has the default priority ({@link KeyValueExtractor#DEFAULT_PRIORITY}). | ||
*/ | ||
@ApplicationScoped | ||
public class KeyValueFromPulsarMessageKeyValueExtractor implements KeyValueExtractor { | ||
@Override | ||
public boolean canExtract(Message<?> message, Type keyType, Type valueType) { | ||
Optional<PulsarIncomingMessageMetadata> metadata = message.getMetadata(PulsarIncomingMessageMetadata.class); | ||
// The type checks can be expensive, so, we do it only once, and rely on the fact the pulsar schema are constant. | ||
return metadata.filter( | ||
incomingMetadata -> (message.getPayload() instanceof KeyValue | ||
&& TypeUtils.isAssignable(keyType, ((KeyValue) message.getPayload()).getKey().getClass()) | ||
&& TypeUtils.isAssignable(valueType, ((KeyValue) message.getPayload()).getValue().getClass()))) | ||
.isPresent(); | ||
} | ||
|
||
@Override | ||
public Object extractKey(Message<?> message, Type keyType) { | ||
return ((KeyValue) message.getPayload()).getKey(); | ||
} | ||
|
||
@Override | ||
public Object extractValue(Message<?> message, Type valueType) { | ||
return ((KeyValue) message.getPayload()).getValue(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...ging-pulsar/src/main/java/io/smallrye/reactive/messaging/pulsar/fault/PulsarContinue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.smallrye.reactive.messaging.pulsar.fault; | ||
|
||
import static io.smallrye.reactive.messaging.pulsar.i18n.PulsarLogging.log; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import org.apache.pulsar.client.api.Consumer; | ||
import org.eclipse.microprofile.reactive.messaging.Metadata; | ||
|
||
import io.smallrye.common.annotation.Identifier; | ||
import io.smallrye.mutiny.Uni; | ||
import io.smallrye.reactive.messaging.pulsar.PulsarConnectorIncomingConfiguration; | ||
import io.smallrye.reactive.messaging.pulsar.PulsarFailureHandler; | ||
import io.smallrye.reactive.messaging.pulsar.PulsarIncomingMessage; | ||
|
||
/** | ||
* Failure strategy `continue` which calls logs message failure but continues the stream without nacking or acking the message | ||
*/ | ||
public class PulsarContinue implements PulsarFailureHandler { | ||
public static final String STRATEGY_NAME = "continue"; | ||
|
||
@ApplicationScoped | ||
@Identifier(STRATEGY_NAME) | ||
public static class Factory implements PulsarFailureHandler.Factory { | ||
|
||
@Override | ||
public PulsarFailureHandler create(Consumer<?> consumer, PulsarConnectorIncomingConfiguration config, | ||
BiConsumer<Throwable, Boolean> reportFailure) { | ||
return new PulsarContinue(config.getChannel()); | ||
} | ||
} | ||
|
||
private final String channel; | ||
|
||
public PulsarContinue(String channel) { | ||
this.channel = channel; | ||
} | ||
|
||
@Override | ||
public Uni<Void> handle(PulsarIncomingMessage<?> message, Throwable reason, Metadata metadata) { | ||
log.messageNackedIgnored(channel, reason.getMessage()); | ||
log.messageNackedFullIgnored(reason); | ||
return Uni.createFrom().voidItem() | ||
.emitOn(message::runOnMessageContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.