Search in sources :

Example 1 with PulsarHandler

use of org.apache.pulsar.common.protocol.PulsarHandler in project pulsar by apache.

the class ServerCnxTest method setConnectionVersion.

private void setConnectionVersion(int version) throws Exception {
    PulsarHandler cnx = 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.protocol.PulsarHandler)

Example 2 with PulsarHandler

use of org.apache.pulsar.common.protocol.PulsarHandler in project 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(dataProvider = "subType")
public void testUnsupportedBatchMessageConsumer(SubscriptionType subType) throws Exception {
    log.info("-- Starting {} test --", methodName);
    final String topicName = "persistent://my-property/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();
    final int numMessagesPerBatch = 10;
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).enableBatching(false).create();
    Producer<byte[]> batchProducer = pulsarClient.newProducer().topic(topicName).enableBatching(true).batchingMaxPublishDelay(Long.MAX_VALUE, TimeUnit.SECONDS).batchingMaxMessages(numMessagesPerBatch).create();
    // update consumer's version to incompatible batch-message version = Version.V3
    Topic topic = pulsar.getBrokerService().getOrCreateTopic(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
    MessageId lastNonBatchedMessageId = null;
    for (int i = 0; i < numMessagesPerBatch; i++) {
        String message = "my-message-" + i;
        lastNonBatchedMessageId = producer.send(message.getBytes());
    }
    Set<String> messageSet = Sets.newHashSet();
    Message<byte[]> msg = null;
    for (int i = 0; i < numMessagesPerBatch; 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 < numMessagesPerBatch; i++) {
        String message = "my-batch-message-" + i;
        batchProducer.sendAsync(message.getBytes());
    }
    batchProducer.flush();
    // consumer should have not received any message as it should have been disconnected
    msg = consumer1.receive(100, TimeUnit.MILLISECONDS);
    assertNull(msg);
    // subscribe consumer2 with supporting batch version
    @Cleanup PulsarClient // Creates new client connection
    newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);
    Consumer<byte[]> consumer2 = newPulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscriptionType(subType).subscribe();
    consumer2.seek(lastNonBatchedMessageId);
    messageSet.clear();
    for (int i = 0; i < numMessagesPerBatch; i++) {
        msg = consumer2.receive();
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-batch-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
        consumer2.acknowledge(msg);
    }
    consumer2.close();
    producer.close();
    batchProducer.close();
    log.info("-- Exiting {} test --", methodName);
}
Also used : Cleanup(lombok.Cleanup) Field(java.lang.reflect.Field) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PulsarHandler(org.apache.pulsar.common.protocol.PulsarHandler) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

Field (java.lang.reflect.Field)2 PulsarHandler (org.apache.pulsar.common.protocol.PulsarHandler)2 Cleanup (lombok.Cleanup)1 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)1 Topic (org.apache.pulsar.broker.service.Topic)1 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)1 MessageId (org.apache.pulsar.client.api.MessageId)1 PulsarClient (org.apache.pulsar.client.api.PulsarClient)1 Test (org.testng.annotations.Test)1