use of org.apache.pulsar.common.api.proto.KeyValue in project pulsar by apache.
the class SchemaInfoUtil method newSchemaInfo.
public static SchemaInfo newSchemaInfo(Schema schema) {
SchemaInfoImpl.SchemaInfoImplBuilder si = SchemaInfoImpl.builder().name(schema.getName()).schema(schema.getSchemaData()).type(Commands.getSchemaType(schema.getType()));
if (schema.getPropertiesCount() == 0) {
si.properties(Collections.emptyMap());
} else {
Map<String, String> properties = new TreeMap<>();
for (int i = 0; i < schema.getPropertiesCount(); i++) {
KeyValue kv = schema.getPropertyAt(i);
properties.put(kv.getKey(), kv.getValue());
}
si.properties(properties);
}
return si.build();
}
use of org.apache.pulsar.common.api.proto.KeyValue in project pulsar by apache.
the class MessageImpl method getKeyValueBySchemaVersion.
private T getKeyValueBySchemaVersion() {
KeyValueSchemaImpl kvSchema = getKeyValueSchema();
byte[] schemaVersion = getSchemaVersion();
if (kvSchema.getKeyValueEncodingType() == KeyValueEncodingType.SEPARATED) {
org.apache.pulsar.common.schema.KeyValue keyValue = (org.apache.pulsar.common.schema.KeyValue) kvSchema.decode(getKeyBytes(), getData(), schemaVersion);
if (schema instanceof AutoConsumeSchema) {
return (T) AutoConsumeSchema.wrapPrimitiveObject(keyValue, ((AutoConsumeSchema) schema).getSchemaInfo(schemaVersion).getType(), schemaVersion);
} else {
return (T) keyValue;
}
} else {
return decode(schemaVersion);
}
}
use of org.apache.pulsar.common.api.proto.KeyValue in project pulsar by apache.
the class InterceptorsTest method testConsumerInterceptorWithMultiTopicSubscribe.
@Test
public void testConsumerInterceptorWithMultiTopicSubscribe() throws PulsarClientException {
ConsumerInterceptor<String> interceptor = new ConsumerInterceptor<String>() {
@Override
public void close() {
}
@Override
public Message<String> beforeConsume(Consumer<String> consumer, Message<String> message) {
MessageImpl<String> msg = (MessageImpl<String>) message;
msg.getMessageBuilder().addProperty().setKey("beforeConsumer").setValue("1");
return msg;
}
@Override
public void onAcknowledge(Consumer<String> consumer, MessageId messageId, Throwable cause) {
log.info("onAcknowledge messageId: {}", messageId, cause);
}
@Override
public void onAcknowledgeCumulative(Consumer<String> consumer, MessageId messageId, Throwable cause) {
log.info("onAcknowledgeCumulative messageIds: {}", messageId, cause);
}
@Override
public void onNegativeAcksSend(Consumer<String> consumer, Set<MessageId> messageIds) {
}
@Override
public void onAckTimeoutSend(Consumer<String> consumer, Set<MessageId> messageIds) {
}
};
Producer<String> producer = pulsarClient.newProducer(Schema.STRING).topic("persistent://my-property/my-ns/my-topic").create();
Producer<String> producer1 = pulsarClient.newProducer(Schema.STRING).topic("persistent://my-property/my-ns/my-topic1").create();
Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING).topic("persistent://my-property/my-ns/my-topic", "persistent://my-property/my-ns/my-topic1").subscriptionType(SubscriptionType.Shared).intercept(interceptor).subscriptionName("my-subscription").subscribe();
producer.newMessage().value("Hello Pulsar!").send();
producer1.newMessage().value("Hello Pulsar!").send();
int keyCount = 0;
for (int i = 0; i < 2; i++) {
Message<String> received = consumer.receive();
MessageImpl<String> msg = (MessageImpl<String>) ((TopicMessageImpl<String>) received).getMessage();
for (KeyValue keyValue : msg.getMessageBuilder().getPropertiesList()) {
if ("beforeConsumer".equals(keyValue.getKey())) {
keyCount++;
}
}
consumer.acknowledge(received);
}
Assert.assertEquals(2, keyCount);
producer.close();
producer1.close();
consumer.close();
}
use of org.apache.pulsar.common.api.proto.KeyValue in project pulsar by apache.
the class InterceptorsTest method testConsumerInterceptorWithPatternTopicSubscribe.
@Test
public void testConsumerInterceptorWithPatternTopicSubscribe() throws PulsarClientException {
ConsumerInterceptor<String> interceptor = new ConsumerInterceptor<String>() {
@Override
public void close() {
}
@Override
public Message<String> beforeConsume(Consumer<String> consumer, Message<String> message) {
MessageImpl<String> msg = (MessageImpl<String>) message;
msg.getMessageBuilder().addProperty().setKey("beforeConsumer").setValue("1");
return msg;
}
@Override
public void onAcknowledge(Consumer<String> consumer, MessageId messageId, Throwable cause) {
log.info("onAcknowledge messageId: {}", messageId, cause);
}
@Override
public void onAcknowledgeCumulative(Consumer<String> consumer, MessageId messageId, Throwable cause) {
log.info("onAcknowledgeCumulative messageIds: {}", messageId, cause);
}
@Override
public void onNegativeAcksSend(Consumer<String> consumer, Set<MessageId> messageIds) {
}
@Override
public void onAckTimeoutSend(Consumer<String> consumer, Set<MessageId> messageIds) {
}
};
Producer<String> producer = pulsarClient.newProducer(Schema.STRING).topic("persistent://my-property/my-ns/my-topic").create();
Producer<String> producer1 = pulsarClient.newProducer(Schema.STRING).topic("persistent://my-property/my-ns/my-topic1").create();
Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING).topicsPattern("persistent://my-property/my-ns/my-.*").subscriptionType(SubscriptionType.Shared).intercept(interceptor).subscriptionName("my-subscription").subscribe();
producer.newMessage().value("Hello Pulsar!").send();
producer1.newMessage().value("Hello Pulsar!").send();
int keyCount = 0;
for (int i = 0; i < 2; i++) {
Message<String> received = consumer.receive();
MessageImpl<String> msg = (MessageImpl<String>) ((TopicMessageImpl<String>) received).getMessage();
for (KeyValue keyValue : msg.getMessageBuilder().getPropertiesList()) {
if ("beforeConsumer".equals(keyValue.getKey())) {
keyCount++;
}
}
consumer.acknowledge(received);
}
Assert.assertEquals(2, keyCount);
producer.close();
producer1.close();
consumer.close();
}
use of org.apache.pulsar.common.api.proto.KeyValue in project pulsar by apache.
the class InterceptorsTest method testConsumerInterceptorWithSingleTopicSubscribe.
@Test(dataProvider = "receiverQueueSize")
public void testConsumerInterceptorWithSingleTopicSubscribe(Integer receiverQueueSize) throws Exception {
ConsumerInterceptor<String> interceptor = new ConsumerInterceptor<String>() {
@Override
public void close() {
}
@Override
public Message<String> beforeConsume(Consumer<String> consumer, Message<String> message) {
MessageImpl<String> msg = (MessageImpl<String>) message;
msg.getMessageBuilder().addProperty().setKey("beforeConsumer").setValue("1");
return msg;
}
@Override
public void onAcknowledge(Consumer<String> consumer, MessageId messageId, Throwable cause) {
log.info("onAcknowledge messageId: {}", messageId, cause);
}
@Override
public void onAcknowledgeCumulative(Consumer<String> consumer, MessageId messageId, Throwable cause) {
log.info("onAcknowledgeCumulative messageIds: {}", messageId, cause);
}
@Override
public void onNegativeAcksSend(Consumer<String> consumer, Set<MessageId> messageIds) {
}
@Override
public void onAckTimeoutSend(Consumer<String> consumer, Set<MessageId> messageIds) {
}
};
Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING).topic("persistent://my-property/my-ns/my-topic").subscriptionType(SubscriptionType.Shared).intercept(interceptor).subscriptionName("my-subscription").receiverQueueSize(receiverQueueSize).subscribe();
Producer<String> producer = pulsarClient.newProducer(Schema.STRING).topic("persistent://my-property/my-ns/my-topic").enableBatching(false).create();
// Receive a message synchronously
producer.newMessage().value("Hello Pulsar!").send();
Message<String> received = consumer.receive();
MessageImpl<String> msg = (MessageImpl<String>) received;
boolean haveKey = false;
for (KeyValue keyValue : msg.getMessageBuilder().getPropertiesList()) {
if ("beforeConsumer".equals(keyValue.getKey())) {
haveKey = true;
}
}
Assert.assertTrue(haveKey);
consumer.acknowledge(received);
// Receive a message asynchronously
producer.newMessage().value("Hello Pulsar!").send();
received = consumer.receiveAsync().get();
msg = (MessageImpl<String>) received;
haveKey = false;
for (KeyValue keyValue : msg.getMessageBuilder().getPropertiesList()) {
if ("beforeConsumer".equals(keyValue.getKey())) {
haveKey = true;
}
}
Assert.assertTrue(haveKey);
consumer.acknowledge(received);
consumer.close();
final CompletableFuture<Message<String>> future = new CompletableFuture<>();
consumer = pulsarClient.newConsumer(Schema.STRING).topic("persistent://my-property/my-ns/my-topic").subscriptionType(SubscriptionType.Shared).intercept(interceptor).subscriptionName("my-subscription").receiverQueueSize(receiverQueueSize).messageListener((c, m) -> {
try {
c.acknowledge(m);
} catch (Exception e) {
Assert.fail("Failed to acknowledge", e);
}
future.complete(m);
}).subscribe();
// Receive a message using the message listener
producer.newMessage().value("Hello Pulsar!").send();
received = future.get();
msg = (MessageImpl<String>) received;
haveKey = false;
for (KeyValue keyValue : msg.getMessageBuilder().getPropertiesList()) {
if ("beforeConsumer".equals(keyValue.getKey())) {
haveKey = true;
}
}
Assert.assertTrue(haveKey);
producer.close();
consumer.close();
}
Aggregations