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);
}
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);
}
Aggregations