Search in sources :

Example 1 with KeyValue

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();
}
Also used : KeyValue(org.apache.pulsar.common.api.proto.KeyValue) TreeMap(java.util.TreeMap)

Example 2 with KeyValue

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);
    }
}
Also used : KeyValue(org.apache.pulsar.common.api.proto.KeyValue) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) KeyValueSchemaImpl(org.apache.pulsar.client.impl.schema.KeyValueSchemaImpl)

Example 3 with KeyValue

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();
}
Also used : Set(java.util.Set) KeyValue(org.apache.pulsar.common.api.proto.KeyValue) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) TopicMessageImpl(org.apache.pulsar.client.impl.TopicMessageImpl) Test(org.testng.annotations.Test)

Example 4 with KeyValue

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();
}
Also used : Set(java.util.Set) KeyValue(org.apache.pulsar.common.api.proto.KeyValue) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) TopicMessageImpl(org.apache.pulsar.client.impl.TopicMessageImpl) Test(org.testng.annotations.Test)

Example 5 with KeyValue

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();
}
Also used : Set(java.util.Set) KeyValue(org.apache.pulsar.common.api.proto.KeyValue) CompletableFuture(java.util.concurrent.CompletableFuture) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) TopicMessageImpl(org.apache.pulsar.client.impl.TopicMessageImpl) Test(org.testng.annotations.Test)

Aggregations

KeyValue (org.apache.pulsar.common.api.proto.KeyValue)15 Set (java.util.Set)6 MessageImpl (org.apache.pulsar.client.impl.MessageImpl)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 ByteBuf (io.netty.buffer.ByteBuf)3 List (java.util.List)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 TimeUnit (java.util.concurrent.TimeUnit)3 TopicMessageImpl (org.apache.pulsar.client.impl.TopicMessageImpl)3 EncryptionKeys (org.apache.pulsar.common.api.proto.EncryptionKeys)3 MessageMetadata (org.apache.pulsar.common.api.proto.MessageMetadata)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Test (org.testng.annotations.Test)3 Sets (com.google.common.collect.Sets)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2