use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project pulsar by apache.
the class V1_ProducerConsumerTest method testInvalidSequence.
@Test
public void testInvalidSequence() throws Exception {
log.info("-- Starting {} test --", methodName);
PulsarClient client1 = PulsarClient.builder().serviceUrl(pulsar.getWebServiceAddress()).build();
client1.close();
try {
client1.newConsumer().topic("persistent://my-property/use/my-ns/my-topic6").subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Exclusive).subscribe();
Assert.fail("Should fail");
} catch (PulsarClientException e) {
Assert.assertTrue(e instanceof PulsarClientException.AlreadyClosedException);
}
try {
client1.newProducer().topic("persistent://my-property/use/my-ns/my-topic6").create();
Assert.fail("Should fail");
} catch (PulsarClientException e) {
Assert.assertTrue(e instanceof PulsarClientException.AlreadyClosedException);
}
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic("persistent://my-property/use/my-ns/my-topic6").subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Exclusive).subscribe();
Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic6").create();
try {
TypedMessageBuilder<byte[]> builder = producer.newMessage().value("InvalidMessage".getBytes());
Message<byte[]> msg = ((TypedMessageBuilderImpl<byte[]>) builder).getMessage();
consumer.acknowledge(msg);
} catch (PulsarClientException.InvalidMessageException e) {
// ok
}
consumer.close();
try {
consumer.receive();
Assert.fail("Should fail");
} catch (PulsarClientException.AlreadyClosedException e) {
// ok
}
try {
consumer.unsubscribe();
Assert.fail("Should fail");
} catch (PulsarClientException.AlreadyClosedException e) {
// ok
}
producer.close();
try {
producer.send("message".getBytes());
Assert.fail("Should fail");
} catch (PulsarClientException.AlreadyClosedException e) {
// ok
}
}
use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project pulsar by apache.
the class PartitionedProducerConsumerTest method testInvalidSequence.
@Test(timeOut = 30000)
public void testInvalidSequence() throws Exception {
log.info("-- Starting {} test --", methodName);
int numPartitions = 4;
TopicName topicName = TopicName.get("persistent://my-property/my-ns/my-partitionedtopic4-" + System.currentTimeMillis());
admin.topics().createPartitionedTopic(topicName.toString(), numPartitions);
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName("my-subscriber-name").subscribe();
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName.toString()).enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
try {
TypedMessageBuilderImpl<byte[]> mb = (TypedMessageBuilderImpl<byte[]>) producer.newMessage().value("InvalidMessage".getBytes());
consumer.acknowledge(mb.getMessage());
} catch (PulsarClientException.InvalidMessageException e) {
// ok
}
consumer.close();
try {
consumer.receive();
Assert.fail("Should fail");
} catch (PulsarClientException.AlreadyClosedException e) {
// ok
}
try {
consumer.unsubscribe();
Assert.fail("Should fail");
} catch (PulsarClientException.AlreadyClosedException e) {
// ok
}
producer.close();
try {
producer.send("message".getBytes());
Assert.fail("Should fail");
} catch (PulsarClientException.AlreadyClosedException e) {
// ok
}
admin.topics().deletePartitionedTopic(topicName.toString());
}
use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project pulsar by apache.
the class ContextImplTest method setup.
@BeforeMethod
public void setup() throws PulsarClientException {
config = new InstanceConfig();
config.setExposePulsarAdminClientEnabled(true);
FunctionDetails functionDetails = FunctionDetails.newBuilder().setUserConfig("").build();
config.setFunctionDetails(functionDetails);
logger = mock(Logger.class);
pulsarAdmin = mock(PulsarAdmin.class);
client = mock(PulsarClientImpl.class);
when(client.newProducer()).thenReturn(new ProducerBuilderImpl(client, Schema.BYTES));
when(client.createProducerAsync(any(ProducerConfigurationData.class), any(), any())).thenReturn(CompletableFuture.completedFuture(producer));
when(client.getSchema(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
when(producer.sendAsync(anyString())).thenReturn(CompletableFuture.completedFuture(null));
clientBuilder = mock(ClientBuilder.class);
when(clientBuilder.build()).thenReturn(client);
TypedMessageBuilder messageBuilder = spy(new TypedMessageBuilderImpl(mock(ProducerBase.class), Schema.STRING));
doReturn(new CompletableFuture<>()).when(messageBuilder).sendAsync();
when(producer.newMessage()).thenReturn(messageBuilder);
context = new ContextImpl(config, logger, client, new EnvironmentBasedSecretsProvider(), FunctionCollectorRegistry.getDefaultImplementation(), new String[0], FunctionDetails.ComponentType.FUNCTION, null, new InstanceStateManager(), pulsarAdmin, clientBuilder);
context.setCurrentMessageContext((Record<String>) () -> null);
}
use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project kop by streamnative.
the class TransactionLogTest method shouldReadWriteMessages.
@Test
public void shouldReadWriteMessages() {
Map<String, Long> pidMappings = Maps.newHashMap();
pidMappings.put("zero", 0L);
pidMappings.put("one", 1L);
pidMappings.put("two", 2L);
pidMappings.put("three", 3L);
pidMappings.put("four", 4L);
pidMappings.put("five", 5L);
Map<Long, TransactionState> transactionStates = Maps.newHashMap();
transactionStates.put(0L, TransactionState.EMPTY);
transactionStates.put(1L, TransactionState.ONGOING);
transactionStates.put(2L, TransactionState.PREPARE_COMMIT);
transactionStates.put(3L, TransactionState.COMPLETE_COMMIT);
transactionStates.put(4L, TransactionState.PREPARE_ABORT);
transactionStates.put(5L, TransactionState.COMPLETE_ABORT);
TypedMessageBuilderImpl<ByteBuffer> typedMessageBuilder = new TypedMessageBuilderImpl<>(null, Schema.BYTEBUFFER);
// generate transaction log messages
List<Message<ByteBuffer>> txnMessages = new ArrayList<>();
pidMappings.forEach((transactionalId, producerId) -> {
TransactionMetadata txnMetadata = TransactionMetadata.builder().transactionalId(transactionalId).producerId(producerId).lastProducerId(RecordBatch.NO_PRODUCER_ID).producerEpoch(producerEpoch).lastProducerEpoch(RecordBatch.NO_PRODUCER_EPOCH).txnTimeoutMs(transactionTimeoutMs).state(transactionStates.get(producerId)).topicPartitions(Sets.newSet()).txnStartTimestamp(0).txnLastUpdateTimestamp(0).build();
if (!txnMetadata.getState().equals(TransactionState.EMPTY)) {
txnMetadata.addPartitions(topicPartitions);
}
byte[] keyBytes = new TransactionLogKey(transactionalId).toBytes();
ByteBuffer valueBytes = new TransactionLogValue(txnMetadata.prepareNoTransit()).toByteBuffer();
typedMessageBuilder.keyBytes(keyBytes);
typedMessageBuilder.value(valueBytes);
txnMessages.add(typedMessageBuilder.getMessage());
});
int count = 0;
for (Message<ByteBuffer> message : txnMessages) {
TransactionLogKey logKey = TransactionLogKey.decode(ByteBuffer.wrap(message.getKeyBytes()), TransactionLogKey.HIGHEST_SUPPORTED_VERSION);
String transactionalId = logKey.getTransactionId();
TransactionMetadata txnMetadata = TransactionLogValue.readTxnRecordValue(transactionalId, message.getValue());
assertEquals(pidMappings.get(transactionalId), new Long(txnMetadata.getProducerId()));
assertEquals(producerEpoch, new Short(txnMetadata.getProducerEpoch()));
assertEquals(transactionTimeoutMs, new Integer(txnMetadata.getTxnTimeoutMs()));
assertEquals(transactionStates.get(txnMetadata.getProducerId()), txnMetadata.getState());
if (txnMetadata.getState().equals(TransactionState.EMPTY)) {
assertEquals(Collections.EMPTY_SET, txnMetadata.getTopicPartitions());
} else {
assertEquals(topicPartitions, txnMetadata.getTopicPartitions());
}
count += 1;
}
assertEquals(pidMappings.size(), count);
}
use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project pulsar-adapters by apache.
the class PulsarKafkaProducer method getRecordMetadata.
private RecordMetadata getRecordMetadata(String topic, TypedMessageBuilder<byte[]> msgBuilder, MessageId messageId, int size) {
MessageIdImpl msgId = (MessageIdImpl) messageId;
// Combine ledger id and entry id to form offset
long offset = MessageIdUtils.getOffset(msgId);
int partition = msgId.getPartitionIndex();
TopicPartition tp = new TopicPartition(topic, partition);
TypedMessageBuilderImpl<byte[]> mb = (TypedMessageBuilderImpl<byte[]>) msgBuilder;
long publishTime = 0L;
try {
// there is no hasPublishTime() currently
publishTime = mb.getPublishTime();
} catch (IllegalStateException ise) {
logger.debug("could not get publish time");
}
return new RecordMetadata(tp, offset, 0L, publishTime, 0L, mb.hasKey() ? mb.getKey().length() : 0, size);
}
Aggregations