Search in sources :

Example 16 with ConsumerConfiguration

use of org.apache.pulsar.client.api.ConsumerConfiguration in project incubator-pulsar by apache.

the class MembershipManagerTest method testConsumerEventListener.

@Test
public void testConsumerEventListener() throws Exception {
    PulsarClient mockClient = mock(PulsarClient.class);
    Consumer mockConsumer = mock(Consumer.class);
    AtomicReference<ConsumerEventListener> listenerHolder = new AtomicReference<>();
    when(mockClient.subscribe(eq(workerConfig.getClusterCoordinationTopic()), eq(MembershipManager.COORDINATION_TOPIC_SUBSCRIPTION), any(ConsumerConfiguration.class))).thenAnswer(invocationOnMock -> {
        ConsumerConfiguration conf = invocationOnMock.getArgumentAt(2, ConsumerConfiguration.class);
        listenerHolder.set(conf.getConsumerEventListener());
        return mockConsumer;
    });
    MembershipManager membershipManager = spy(new MembershipManager(workerConfig, mockClient));
    assertFalse(membershipManager.isLeader());
    verify(mockClient, times(1)).subscribe(eq(workerConfig.getClusterCoordinationTopic()), eq(MembershipManager.COORDINATION_TOPIC_SUBSCRIPTION), any(ConsumerConfiguration.class));
    listenerHolder.get().becameActive(mockConsumer, 0);
    assertTrue(membershipManager.isLeader());
    listenerHolder.get().becameInactive(mockConsumer, 0);
    assertFalse(membershipManager.isLeader());
}
Also used : ConsumerEventListener(org.apache.pulsar.client.api.ConsumerEventListener) Consumer(org.apache.pulsar.client.api.Consumer) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 17 with ConsumerConfiguration

use of org.apache.pulsar.client.api.ConsumerConfiguration in project incubator-pulsar by apache.

the class SparkStreamingPulsarReceiverExample method main.

public static void main(String[] args) throws InterruptedException {
    SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("pulsar-spark");
    JavaStreamingContext jssc = new JavaStreamingContext(conf, Durations.seconds(5));
    ClientConfiguration clientConf = new ClientConfiguration();
    ConsumerConfiguration consConf = new ConsumerConfiguration();
    String url = "pulsar://localhost:6650/";
    String topic = "persistent://sample/standalone/ns1/topic1";
    String subs = "sub1";
    JavaReceiverInputDStream<byte[]> msgs = jssc.receiverStream(new SparkStreamingPulsarReceiver(clientConf, consConf, url, topic, subs));
    JavaDStream<Integer> isContainingPulsar = msgs.flatMap(new FlatMapFunction<byte[], Integer>() {

        @Override
        public Iterator<Integer> call(byte[] msg) {
            return Arrays.asList(((new String(msg)).indexOf("Pulsar") != -1) ? 1 : 0).iterator();
        }
    });
    JavaDStream<Integer> numOfPulsar = isContainingPulsar.reduce(new Function2<Integer, Integer, Integer>() {

        @Override
        public Integer call(Integer i1, Integer i2) {
            return i1 + i2;
        }
    });
    numOfPulsar.print();
    jssc.start();
    jssc.awaitTermination();
}
Also used : JavaStreamingContext(org.apache.spark.streaming.api.java.JavaStreamingContext) SparkStreamingPulsarReceiver(org.apache.pulsar.spark.SparkStreamingPulsarReceiver) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) Iterator(java.util.Iterator) SparkConf(org.apache.spark.SparkConf) ClientConfiguration(org.apache.pulsar.client.api.ClientConfiguration)

Example 18 with ConsumerConfiguration

use of org.apache.pulsar.client.api.ConsumerConfiguration in project incubator-pulsar by apache.

the class PulsarSpoutTest method setup.

@Override
protected void setup() throws Exception {
    super.internalSetup();
    super.producerBaseSetup();
    pulsarSpoutConf = new PulsarSpoutConfiguration();
    pulsarSpoutConf.setServiceUrl(serviceUrl);
    pulsarSpoutConf.setTopic(topic);
    pulsarSpoutConf.setSubscriptionName(subscriptionName);
    pulsarSpoutConf.setMessageToValuesMapper(messageToValuesMapper);
    pulsarSpoutConf.setFailedRetriesTimeout(1, TimeUnit.SECONDS);
    pulsarSpoutConf.setMaxFailedRetries(2);
    pulsarSpoutConf.setSharedConsumerEnabled(true);
    pulsarSpoutConf.setMetricsTimeIntervalInSecs(60);
    consumerConf = new ConsumerConfiguration();
    consumerConf.setSubscriptionType(SubscriptionType.Shared);
    spout = new PulsarSpout(pulsarSpoutConf, new ClientConfiguration(), consumerConf);
    mockCollector = new MockSpoutOutputCollector();
    SpoutOutputCollector collector = new SpoutOutputCollector(mockCollector);
    TopologyContext context = mock(TopologyContext.class);
    when(context.getThisComponentId()).thenReturn("test-spout-" + methodName);
    when(context.getThisTaskId()).thenReturn(0);
    spout.open(Maps.newHashMap(), context, collector);
    producer = pulsarClient.createProducer(topic);
}
Also used : PulsarSpoutConfiguration(org.apache.pulsar.storm.PulsarSpoutConfiguration) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) TopologyContext(org.apache.storm.task.TopologyContext) PulsarSpout(org.apache.pulsar.storm.PulsarSpout) ClientConfiguration(org.apache.pulsar.client.api.ClientConfiguration)

Example 19 with ConsumerConfiguration

use of org.apache.pulsar.client.api.ConsumerConfiguration in project incubator-pulsar by apache.

the class PulsarSpoutTest method testFailedConsumer.

@Test
public void testFailedConsumer() throws Exception {
    PulsarSpoutConfiguration pulsarSpoutConf = new PulsarSpoutConfiguration();
    pulsarSpoutConf.setServiceUrl(serviceUrl);
    pulsarSpoutConf.setTopic("persistent://invalidTopic");
    pulsarSpoutConf.setSubscriptionName(subscriptionName);
    pulsarSpoutConf.setMessageToValuesMapper(messageToValuesMapper);
    pulsarSpoutConf.setFailedRetriesTimeout(1, TimeUnit.SECONDS);
    pulsarSpoutConf.setMaxFailedRetries(2);
    pulsarSpoutConf.setSharedConsumerEnabled(false);
    pulsarSpoutConf.setMetricsTimeIntervalInSecs(60);
    ConsumerConfiguration consumerConf = new ConsumerConfiguration();
    consumerConf.setSubscriptionType(SubscriptionType.Shared);
    PulsarSpout spout = new PulsarSpout(pulsarSpoutConf, new ClientConfiguration(), consumerConf);
    MockSpoutOutputCollector mockCollector = new MockSpoutOutputCollector();
    SpoutOutputCollector collector = new SpoutOutputCollector(mockCollector);
    TopologyContext context = mock(TopologyContext.class);
    when(context.getThisComponentId()).thenReturn("new-test" + methodName);
    when(context.getThisTaskId()).thenReturn(0);
    try {
        spout.open(Maps.newHashMap(), context, collector);
        fail("should have failed as consumer creation failed");
    } catch (IllegalStateException e) {
    // Ok
    }
}
Also used : PulsarSpoutConfiguration(org.apache.pulsar.storm.PulsarSpoutConfiguration) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) TopologyContext(org.apache.storm.task.TopologyContext) PulsarSpout(org.apache.pulsar.storm.PulsarSpout) ClientConfiguration(org.apache.pulsar.client.api.ClientConfiguration) Test(org.testng.annotations.Test)

Example 20 with ConsumerConfiguration

use of org.apache.pulsar.client.api.ConsumerConfiguration in project incubator-pulsar by apache.

the class ConsumerHandler method getConsumerConfiguration.

private ConsumerConfiguration getConsumerConfiguration() {
    ConsumerConfiguration conf = new ConsumerConfiguration();
    if (queryParams.containsKey("ackTimeoutMillis")) {
        conf.setAckTimeout(Integer.parseInt(queryParams.get("ackTimeoutMillis")), TimeUnit.MILLISECONDS);
    }
    if (queryParams.containsKey("subscriptionType")) {
        checkArgument(Enums.getIfPresent(SubscriptionType.class, queryParams.get("subscriptionType")).isPresent(), "Invalid subscriptionType %s", queryParams.get("subscriptionType"));
        conf.setSubscriptionType(SubscriptionType.valueOf(queryParams.get("subscriptionType")));
    }
    if (queryParams.containsKey("receiverQueueSize")) {
        conf.setReceiverQueueSize(Math.min(Integer.parseInt(queryParams.get("receiverQueueSize")), 1000));
    }
    if (queryParams.containsKey("consumerName")) {
        conf.setConsumerName(queryParams.get("consumerName"));
    }
    if (queryParams.containsKey("priorityLevel")) {
        conf.setPriorityLevel(Integer.parseInt(queryParams.get("priorityLevel")));
    }
    return conf;
}
Also used : ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration)

Aggregations

ConsumerConfiguration (org.apache.pulsar.client.api.ConsumerConfiguration)36 Test (org.testng.annotations.Test)32 Producer (org.apache.pulsar.client.api.Producer)29 ProducerConfiguration (org.apache.pulsar.client.api.ProducerConfiguration)29 Consumer (org.apache.pulsar.client.api.Consumer)28 Message (org.apache.pulsar.client.api.Message)25 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)15 IOException (java.io.IOException)14 ExecutionException (java.util.concurrent.ExecutionException)14 CompletableFuture (java.util.concurrent.CompletableFuture)9 MessageId (org.apache.pulsar.client.api.MessageId)9 Future (java.util.concurrent.Future)7 ConsumerImpl (org.apache.pulsar.client.impl.ConsumerImpl)7 Map (java.util.Map)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExecutorService (java.util.concurrent.ExecutorService)6 CryptoKeyReader (org.apache.pulsar.client.api.CryptoKeyReader)6 EncryptionKeyInfo (org.apache.pulsar.client.api.EncryptionKeyInfo)6 Field (java.lang.reflect.Field)5 ManagedLedgerImpl (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl)5