Search in sources :

Example 1 with ConsumerBuilderImpl

use of org.apache.pulsar.client.impl.ConsumerBuilderImpl in project pulsar-adapters by apache.

the class PulsarKafkaConsumerTest method testPulsarKafkaConsumerConfig.

@Test
public void testPulsarKafkaConsumerConfig() throws Exception {
    // https://kafka.apache.org/08/documentation.html#consumerconfigs
    Properties properties = new Properties();
    properties.put("zookeeper.connect", "http://localhost:8080/");
    properties.put("group.id", "group1");
    properties.put("consumer.id", "cons1");
    properties.put("auto.commit.enable", "true");
    properties.put("auto.commit.interval.ms", "100");
    properties.put("queued.max.message.chunks", "100");
    ConsumerConfig config = new ConsumerConfig(properties);
    ConsumerConnector connector = new ConsumerConnector(config);
    ConsumerBuilderImpl<byte[]> consumerBuilder = (ConsumerBuilderImpl<byte[]>) connector.getConsumerBuilder();
    Field confField = consumerBuilder.getClass().getDeclaredField("conf");
    confField.setAccessible(true);
    ConsumerConfigurationData conf = (ConsumerConfigurationData) confField.get(consumerBuilder);
    assertEquals(conf.getSubscriptionName(), "group1");
    assertEquals(conf.getReceiverQueueSize(), 100);
    assertEquals(conf.getAcknowledgementsGroupTimeMicros(), TimeUnit.MILLISECONDS.toMicros(100));
    System.out.println(conf);
}
Also used : Field(java.lang.reflect.Field) ConsumerConfigurationData(org.apache.pulsar.client.impl.conf.ConsumerConfigurationData) ConsumerConfig(kafka.consumer.ConsumerConfig) Properties(java.util.Properties) ConsumerBuilderImpl(org.apache.pulsar.client.impl.ConsumerBuilderImpl) Test(org.testng.annotations.Test)

Example 2 with ConsumerBuilderImpl

use of org.apache.pulsar.client.impl.ConsumerBuilderImpl in project pulsar-adapters by apache.

the class PulsarKafkaConsumerTest method testPulsarKafkaConsumerWithDefaultConfig.

@Test
public void testPulsarKafkaConsumerWithDefaultConfig() throws Exception {
    // https://kafka.apache.org/08/documentation.html#consumerconfigs
    Properties properties = new Properties();
    properties.put("zookeeper.connect", "http://localhost:8080/");
    properties.put("group.id", "group1");
    ConsumerConfig config = new ConsumerConfig(properties);
    ConsumerConnector connector = new ConsumerConnector(config);
    ConsumerBuilderImpl<byte[]> consumerBuilder = (ConsumerBuilderImpl<byte[]>) connector.getConsumerBuilder();
    Field confField = consumerBuilder.getClass().getDeclaredField("conf");
    confField.setAccessible(true);
    ConsumerConfigurationData conf = (ConsumerConfigurationData) confField.get(consumerBuilder);
    assertEquals(conf.getSubscriptionName(), "group1");
    assertEquals(conf.getReceiverQueueSize(), 1000);
}
Also used : Field(java.lang.reflect.Field) ConsumerConfigurationData(org.apache.pulsar.client.impl.conf.ConsumerConfigurationData) ConsumerConfig(kafka.consumer.ConsumerConfig) Properties(java.util.Properties) ConsumerBuilderImpl(org.apache.pulsar.client.impl.ConsumerBuilderImpl) Test(org.testng.annotations.Test)

Example 3 with ConsumerBuilderImpl

use of org.apache.pulsar.client.impl.ConsumerBuilderImpl in project pulsar-adapters by apache.

the class PulsarCliebtKafkaConfigTest method testPulsarKafkaProducer.

@Test
public void testPulsarKafkaProducer() {
    ClientBuilder mockClientBuilder = mock(ClientBuilder.class);
    ProducerBuilder mockProducerBuilder = mock(ProducerBuilder.class);
    doAnswer(invocation -> {
        Assert.assertEquals((int) invocation.getArguments()[0], 1000000, "Send time out is suppose to be 1000.");
        return mockProducerBuilder;
    }).when(mockProducerBuilder).sendTimeout(anyInt(), any(TimeUnit.class));
    doReturn(mockClientBuilder).when(mockClientBuilder).serviceUrl(anyString());
    doAnswer(invocation -> {
        Assert.assertEquals((int) invocation.getArguments()[0], 1000, "Keep alive interval is suppose to be 1000.");
        return mockClientBuilder;
    }).when(mockClientBuilder).keepAliveInterval(anyInt(), any(TimeUnit.class));
    // validate producer
    Properties properties = new Properties();
    properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    properties.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, DefaultPartitioner.class);
    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, Arrays.asList("pulsar://localhost:6650"));
    properties.put(ProducerConfig.CONNECTIONS_MAX_IDLE_MS_CONFIG, "1000000");
    properties.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, "1000000");
    properties.put(PulsarProducerKafkaConfig.CRYPTO_READER_FACTORY_CLASS_NAME, CryptoKeyReaderFactoryImpl.class.getName());
    PulsarKafkaProducer producer = new PulsarKafkaProducer<>(properties);
    ProducerBuilderImpl producerBuilder = (ProducerBuilderImpl) producer.pulsarProducerBuilder;
    assertEquals(producerBuilder.getConf().getCryptoKeyReader(), CryptoKeyReaderFactoryImpl.reader);
    assertEquals(producerBuilder.getConf().getEncryptionKeys(), CryptoKeyReaderFactoryImpl.encryptionKeys);
    // validate consumer
    properties = new Properties();
    properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, Arrays.asList("pulsar://localhost:6650"));
    properties.put(PulsarProducerKafkaConfig.CRYPTO_READER_FACTORY_CLASS_NAME, CryptoKeyReaderFactoryImpl.class.getName());
    PulsarClient client = mock(PulsarClient.class);
    ConsumerBuilderImpl<byte[]> consumerBuilder = new ConsumerBuilderImpl<>(null, null);
    doReturn(consumerBuilder).when(client).newConsumer();
    PulsarConsumerKafkaConfig.getConsumerBuilder(client, properties);
    assertEquals(consumerBuilder.getConf().getCryptoKeyReader(), CryptoKeyReaderFactoryImpl.reader);
}
Also used : ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) TimeUnit(java.util.concurrent.TimeUnit) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Properties(java.util.Properties) ConsumerBuilderImpl(org.apache.pulsar.client.impl.ConsumerBuilderImpl) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder) Test(org.testng.annotations.Test)

Aggregations

Properties (java.util.Properties)3 ConsumerBuilderImpl (org.apache.pulsar.client.impl.ConsumerBuilderImpl)3 Test (org.testng.annotations.Test)3 Field (java.lang.reflect.Field)2 ConsumerConfig (kafka.consumer.ConsumerConfig)2 ConsumerConfigurationData (org.apache.pulsar.client.impl.conf.ConsumerConfigurationData)2 TimeUnit (java.util.concurrent.TimeUnit)1 ClientBuilder (org.apache.pulsar.client.api.ClientBuilder)1 ProducerBuilder (org.apache.pulsar.client.api.ProducerBuilder)1 PulsarClient (org.apache.pulsar.client.api.PulsarClient)1 ProducerBuilderImpl (org.apache.pulsar.client.impl.ProducerBuilderImpl)1