Search in sources :

Example 1 with PulsarHandler

use of org.apache.pulsar.common.api.PulsarHandler in project incubator-pulsar by apache.

the class ServerCnxTest method setConnectionVersion.

private void setConnectionVersion(int version) throws Exception {
    PulsarHandler cnx = (PulsarHandler) serverCnx;
    Field versionField = PulsarHandler.class.getDeclaredField("remoteEndpointProtocolVersion");
    versionField.setAccessible(true);
    versionField.set(cnx, version);
}
Also used : Field(java.lang.reflect.Field) PulsarHandler(org.apache.pulsar.common.api.PulsarHandler)

Example 2 with PulsarHandler

use of org.apache.pulsar.common.api.PulsarHandler in project incubator-pulsar by apache.

the class BrokerClientIntegrationTest method testUnsupportedBatchMessageConsumer.

/**
 * It verifies that consumer which doesn't support batch-message:
 * <p>
 * 1. broker disconnects that consumer
 * <p>
 * 2. redeliver all those messages to other supported consumer under the same subscription
 *
 * @param subType
 * @throws Exception
 */
@Test(timeOut = 7000, dataProvider = "subType")
public void testUnsupportedBatchMessageConsumer(SubscriptionType subType) throws Exception {
    log.info("-- Starting {} test --", methodName);
    final int batchMessageDelayMs = 1000;
    final String topicName = "persistent://my-property/use/my-ns/my-topic1";
    final String subscriptionName = "my-subscriber-name" + subType;
    ConsumerImpl<byte[]> consumer1 = (ConsumerImpl<byte[]>) pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscriptionType(subType).subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    Producer<byte[]> batchProducer = pulsarClient.newProducer().topic(topicName).enableBatching(true).batchingMaxPublishDelay(batchMessageDelayMs, TimeUnit.MILLISECONDS).batchingMaxMessages(20).create();
    // update consumer's version to incompatible batch-message version = Version.V3
    Topic topic = pulsar.getBrokerService().getTopic(topicName).get();
    org.apache.pulsar.broker.service.Consumer brokerConsumer = topic.getSubscriptions().get(subscriptionName).getConsumers().get(0);
    Field cnxField = org.apache.pulsar.broker.service.Consumer.class.getDeclaredField("cnx");
    cnxField.setAccessible(true);
    PulsarHandler cnx = (PulsarHandler) cnxField.get(brokerConsumer);
    Field versionField = PulsarHandler.class.getDeclaredField("remoteEndpointProtocolVersion");
    versionField.setAccessible(true);
    versionField.set(cnx, 3);
    // (1) send non-batch message: consumer should be able to consume
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Set<String> messageSet = Sets.newHashSet();
    Message<byte[]> msg = null;
    for (int i = 0; i < 10; i++) {
        msg = consumer1.receive(1, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
        consumer1.acknowledge(msg);
    }
    // Also set clientCnx of the consumer to null so, it avoid reconnection so, other consumer can consume for
    // verification
    consumer1.setClientCnx(null);
    // (2) send batch-message which should not be able to consume: as broker will disconnect the consumer
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        batchProducer.sendAsync(message.getBytes());
    }
    Thread.sleep(batchMessageDelayMs);
    // consumer should have not received any message as it should have been disconnected
    msg = consumer1.receive(2, TimeUnit.SECONDS);
    assertNull(msg);
    // subscrie consumer2 with supporting batch version
    Consumer<byte[]> consumer2 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
    messageSet.clear();
    for (int i = 0; i < 10; i++) {
        msg = consumer2.receive(1, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
        consumer2.acknowledge(msg);
    }
    consumer2.close();
    producer.close();
    batchProducer.close();
    log.info("-- Exiting {} test --", methodName);
}
Also used : Field(java.lang.reflect.Field) Topic(org.apache.pulsar.broker.service.Topic) PulsarHandler(org.apache.pulsar.common.api.PulsarHandler) Test(org.testng.annotations.Test)

Aggregations

Field (java.lang.reflect.Field)2 PulsarHandler (org.apache.pulsar.common.api.PulsarHandler)2 Topic (org.apache.pulsar.broker.service.Topic)1 Test (org.testng.annotations.Test)1