org.apache.pulsar.client.impl.PulsarClientImpl Java Examples

The following examples show how to use org.apache.pulsar.client.impl.PulsarClientImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProxyTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private static PulsarClient getClientActiveConsumerChangeNotSupported(ClientConfigurationData conf)
        throws Exception {
    ThreadFactory threadFactory = new DefaultThreadFactory("pulsar-client-io", Thread.currentThread().isDaemon());
    EventLoopGroup eventLoopGroup = EventLoopUtil.newEventLoopGroup(conf.getNumIoThreads(), threadFactory);

    ConnectionPool cnxPool = new ConnectionPool(conf, eventLoopGroup, () -> {
        return new ClientCnx(conf, eventLoopGroup, ProtocolVersion.v11_VALUE) {
            @Override
            protected void handleActiveConsumerChange(CommandActiveConsumerChange change) {
                throw new UnsupportedOperationException();
            }
        };
    });

    return new PulsarClientImpl(conf, eventLoopGroup, cnxPool);
}
 
Example #2
Source File: SimpleSchemaTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSchemaByVersion() throws PulsarClientException, PulsarAdminException, ExecutionException, InterruptedException {
    final String topic = "persistent://my-property/my-ns/testGetSchemaByVersion";

    PulsarClientImpl httpProtocolClient = (PulsarClientImpl) PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    PulsarClientImpl binaryProtocolClient = (PulsarClientImpl) pulsarClient;

    pulsarClient.newProducer(Schema.AVRO(V1Data.class))
        .topic(topic)
        .create();

    pulsarClient.newProducer(Schema.AVRO(V2Data.class))
        .topic(topic)
        .create();

    LookupService httpLookupService = httpProtocolClient.getLookup();
    LookupService binaryLookupService = binaryProtocolClient.getLookup();
    Assert.assertTrue(httpLookupService instanceof HttpLookupService);
    Assert.assertTrue(binaryLookupService instanceof BinaryProtoLookupService);
    Assert.assertEquals(admin.schemas().getAllSchemas(topic).size(), 2);
    Assert.assertTrue(httpLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(0).array()).get().isPresent());
    Assert.assertTrue(httpLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(1).array()).get().isPresent());
    Assert.assertTrue(binaryLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(0).array()).get().isPresent());
    Assert.assertTrue(binaryLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(1).array()).get().isPresent());
}
 
Example #3
Source File: CachedPulsarClientTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldReturnSameInstanceWithSameParam() throws Exception {
    PulsarClientImpl impl1 = Mockito.mock(PulsarClientImpl.class);
    PulsarClientImpl impl2 = Mockito.mock(PulsarClientImpl.class);

    ClientConfigurationData conf1 = new ClientConfigurationData();
    conf1.setServiceUrl(SERVICE_URL);

    ClientConfigurationData conf2 = new ClientConfigurationData();
    conf2.setServiceUrl(SERVICE_URL);

    PowerMockito.whenNew(PulsarClientImpl.class)
        .withArguments(conf1).thenReturn(impl1);
    PowerMockito.whenNew(PulsarClientImpl.class)
        .withArguments(conf2).thenReturn(impl2);

    PulsarClientImpl client1 = CachedPulsarClient.getOrCreate(conf1);
    PulsarClientImpl client2 = CachedPulsarClient.getOrCreate(conf2);
    PulsarClientImpl client3 = CachedPulsarClient.getOrCreate(conf1);

    assertEquals(client1, client2);
    assertEquals(client1, client3);

    assertEquals(CachedPulsarClient.getAsMap().size(), 1);
}
 
Example #4
Source File: TopicSchema.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * If the topic is already created, we should be able to fetch the schema type (avro, json, ...)
 */
private SchemaType getSchemaTypeOrDefault(String topic, Class<?> clazz) {
    if (GenericRecord.class.isAssignableFrom(clazz)) {
        return SchemaType.AUTO_CONSUME;
    } else if (byte[].class.equals(clazz)
            || ByteBuf.class.equals(clazz)
            || ByteBuffer.class.equals(clazz)) {
        // if function uses bytes, we should ignore
        return SchemaType.NONE;
    } else {
        Optional<SchemaInfo> schema = ((PulsarClientImpl) client).getSchema(topic).join();
        if (schema.isPresent()) {
            if (schema.get().getType() == SchemaType.NONE) {
                return getDefaultSchemaType(clazz);
            } else {
                return schema.get().getType();
            }
        } else {
            return getDefaultSchemaType(clazz);
        }
    }
}
 
Example #5
Source File: ContextImplTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void setup() {
    config = new InstanceConfig();
    FunctionDetails functionDetails = FunctionDetails.newBuilder()
        .setUserConfig("")
        .build();
    config.setFunctionDetails(functionDetails);
    logger = mock(Logger.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));

    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(), new CollectorRegistry(), new String[0],
            FunctionDetails.ComponentType.FUNCTION, null, null);
    context.setCurrentMessageContext((Record<String>) () -> null);
}
 
Example #6
Source File: CachedPulsarClientTest.java    From pulsar-flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldReturnSameInstanceWithSameParam() throws Exception {
    PulsarClientImpl impl1 = Mockito.mock(PulsarClientImpl.class);
    PulsarClientImpl impl2 = Mockito.mock(PulsarClientImpl.class);

    ClientConfigurationData conf1 = new ClientConfigurationData();
    conf1.setServiceUrl(SERVICE_URL);

    ClientConfigurationData conf2 = new ClientConfigurationData();
    conf2.setServiceUrl(SERVICE_URL);

    PowerMockito.whenNew(PulsarClientImpl.class)
            .withArguments(conf1).thenReturn(impl1);
    PowerMockito.whenNew(PulsarClientImpl.class)
            .withArguments(conf2).thenReturn(impl2);

    PulsarClientImpl client1 = CachedPulsarClient.getOrCreate(conf1);
    PulsarClientImpl client2 = CachedPulsarClient.getOrCreate(conf2);
    PulsarClientImpl client3 = CachedPulsarClient.getOrCreate(conf1);

    assertEquals(client1, client2);
    assertEquals(client1, client3);

    assertEquals(CachedPulsarClient.getAsMap().size(), 1);
}
 
Example #7
Source File: ProxyTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSchema() throws Exception {
    @Cleanup
    PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl())
            .build();
    Schema<Foo> schema = Schema.AVRO(Foo.class);
    try {
        try (Producer<Foo> ignored = client.newProducer(schema).topic("persistent://sample/test/local/get-schema")
            .create()) {
        }
    } catch (Exception ex) {
        Assert.fail("Should not have failed since can acquire LookupRequestSemaphore");
    }
    byte[] schemaVersion = new byte[8];
    byte b = new Long(0l).byteValue();
    for (int i = 0; i<8; i++){
        schemaVersion[i] = b;
    }
    SchemaInfo schemaInfo = ((PulsarClientImpl) client).getLookup()
            .getSchema(TopicName.get("persistent://sample/test/local/get-schema"), schemaVersion).get().orElse(null);
    Assert.assertEquals(new String(schemaInfo.getSchema()), new String(schema.getSchemaInfo().getSchema()));
}
 
Example #8
Source File: ProxyParserTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private static PulsarClient getClientActiveConsumerChangeNotSupported(ClientConfigurationData conf)
        throws Exception {
    ThreadFactory threadFactory = new DefaultThreadFactory("pulsar-client-io", Thread.currentThread().isDaemon());
    EventLoopGroup eventLoopGroup = EventLoopUtil.newEventLoopGroup(conf.getNumIoThreads(), threadFactory);

    ConnectionPool cnxPool = new ConnectionPool(conf, eventLoopGroup, () -> {
        return new ClientCnx(conf, eventLoopGroup, ProtocolVersion.v11_VALUE) {
            @Override
            protected void handleActiveConsumerChange(CommandActiveConsumerChange change) {
                throw new UnsupportedOperationException();
            }
        };
    });

    return new PulsarClientImpl(conf, eventLoopGroup, cnxPool);
}
 
Example #9
Source File: AuthenticationTokenTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthTokenClientConfig() throws Exception {
    ClientConfigurationData clientConfig = new ClientConfigurationData();
    clientConfig.setServiceUrl("pulsar://service-url");
    clientConfig.setAuthPluginClassName(AuthenticationToken.class.getName());
    clientConfig.setAuthParams("token-xyz");

    PulsarClientImpl pulsarClient = new PulsarClientImpl(clientConfig);

    Authentication authToken = pulsarClient.getConfiguration().getAuthentication();
    assertEquals(authToken.getAuthMethodName(), "token");

    AuthenticationDataProvider authData = authToken.getAuthData();
    assertTrue(authData.hasDataFromCommand());
    assertEquals(authData.getCommandData(), "token-xyz");

    assertFalse(authData.hasDataForTls());
    assertNull(authData.getTlsCertificates());
    assertNull(authData.getTlsPrivateKey());

    assertTrue(authData.hasDataForHttp());
    assertEquals(authData.getHttpHeaders(),
            Collections.singletonMap("Authorization", "Bearer token-xyz").entrySet());

    authToken.close();
}
 
Example #10
Source File: AbstractReplicator.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public AbstractReplicator(String topicName, String replicatorPrefix, String localCluster, String remoteCluster,
        BrokerService brokerService) throws NamingException {
    validatePartitionedTopic(topicName, brokerService);
    this.brokerService = brokerService;
    this.topicName = topicName;
    this.replicatorPrefix = replicatorPrefix;
    this.localCluster = localCluster.intern();
    this.remoteCluster = remoteCluster.intern();
    this.client = (PulsarClientImpl) brokerService.getReplicationClient(remoteCluster);
    this.producer = null;
    this.producerQueueSize = brokerService.pulsar().getConfiguration().getReplicationProducerQueueSize();

    this.producerBuilder = client.newProducer() //
            .topic(topicName)
            .messageRoutingMode(MessageRoutingMode.SinglePartition)
            .enableBatching(false)
            .sendTimeout(0, TimeUnit.SECONDS) //
            .maxPendingMessages(producerQueueSize) //
            .producerName(getReplicatorName(replicatorPrefix, localCluster));
    STATE_UPDATER.set(this, State.Stopped);
}
 
Example #11
Source File: LookupProtocolTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test(timeOut = 10000)
public void binaryTlsLookupTest() throws Exception{
    WebSocketProxyConfiguration conf = new WebSocketProxyConfiguration();
    conf.setServiceUrl("http://localhost:8080");
    conf.setServiceUrlTls("https://localhost:8443");
    conf.setBrokerServiceUrl("pulsar://localhost:6650");
    conf.setBrokerServiceUrlTls("pulsar+ssl://localhost:6651");
    conf.setBrokerClientTlsEnabled(true);
    WebSocketService service  = new WebSocketService(conf);
    PulsarClientImpl testClient = (PulsarClientImpl) service.getPulsarClient();
    Field lookupField = PulsarClientImpl.class.getDeclaredField("lookup");
    lookupField.setAccessible(true);
    Assert.assertEquals(lookupField.get(testClient).getClass().getName(), "org.apache.pulsar.client.impl.BinaryProtoLookupService");
    Assert.assertTrue(testClient.getConfiguration().isUseTls());
    service.close();
}
 
Example #12
Source File: ProxyConfigurationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "setProxyConfig", timeOut = 10000)
public void configTest(int numIoThreads, int connectionsPerBroker) throws Exception {
    config.setWebSocketNumIoThreads(numIoThreads);
    config.setWebSocketConnectionsPerBroker(connectionsPerBroker);
    WebSocketService service = spy(new WebSocketService(config));
    doReturn(mockZooKeeperClientFactory).when(service).getZooKeeperClientFactory();
    service.start();

    PulsarClientImpl client = (PulsarClientImpl) service.getPulsarClient();
    assertEquals(client.getConfiguration().getNumIoThreads(), numIoThreads);
    assertEquals(client.getConfiguration().getConnectionsPerBroker(), connectionsPerBroker);

    service.close();
}
 
Example #13
Source File: JavaInstanceRunnable.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public JavaInstanceRunnable(InstanceConfig instanceConfig,
                            FunctionCacheManager fnCache,
                            String jarFile,
                            PulsarClient pulsarClient,
                            String stateStorageServiceUrl,
                            SecretsProvider secretsProvider,
                            CollectorRegistry collectorRegistry,
                            String narExtractionDirectory) {
    this.instanceConfig = instanceConfig;
    this.fnCache = fnCache;
    this.jarFile = jarFile;
    this.client = (PulsarClientImpl) pulsarClient;
    this.stateStorageServiceUrl = stateStorageServiceUrl;
    this.secretsProvider = secretsProvider;
    this.collectorRegistry = collectorRegistry;
    this.narExtractionDirectory = narExtractionDirectory;
    this.metricsLabels = new String[]{
            instanceConfig.getFunctionDetails().getTenant(),
            String.format("%s/%s", instanceConfig.getFunctionDetails().getTenant(),
                    instanceConfig.getFunctionDetails().getNamespace()),
            instanceConfig.getFunctionDetails().getName(),
            String.valueOf(instanceConfig.getInstanceId()),
            instanceConfig.getClusterName(),
            FunctionCommon.getFullyQualifiedName(instanceConfig.getFunctionDetails())
    };

    this.componentType = InstanceUtils.calculateSubjectType(instanceConfig.getFunctionDetails());

    this.properties = InstanceUtils.getProperties(this.componentType,
            FunctionCommon.getFullyQualifiedName(instanceConfig.getFunctionDetails()),
            this.instanceConfig.getInstanceId());

    // Declare function local collector registry so that it will not clash with other function instances'
    // metrics collection especially in threaded mode
    // In process mode the JavaInstanceMain will declare a CollectorRegistry and pass it down
    this.collectorRegistry = collectorRegistry;

    this.instanceClassLoader = Thread.currentThread().getContextClassLoader();
}
 
Example #14
Source File: NamespaceService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public PulsarClientImpl getNamespaceClient(ClusterData cluster) {
    PulsarClientImpl client = namespaceClients.get(cluster);
    if (client != null) {
        return client;
    }

    return namespaceClients.computeIfAbsent(cluster, key -> {
        try {
            ClientBuilder clientBuilder = PulsarClient.builder()
                .enableTcpNoDelay(false)
                .statsInterval(0, TimeUnit.SECONDS);

            if (pulsar.getConfiguration().isAuthenticationEnabled()) {
                clientBuilder.authentication(pulsar.getConfiguration().getBrokerClientAuthenticationPlugin(),
                    pulsar.getConfiguration().getBrokerClientAuthenticationParameters());
            }

            if (pulsar.getConfiguration().isTlsEnabled()) {
                clientBuilder
                    .serviceUrl(isNotBlank(cluster.getBrokerServiceUrlTls())
                        ? cluster.getBrokerServiceUrlTls() : cluster.getServiceUrlTls())
                    .enableTls(true)
                    .tlsTrustCertsFilePath(pulsar.getConfiguration().getBrokerClientTrustCertsFilePath())
                    .allowTlsInsecureConnection(pulsar.getConfiguration().isTlsAllowInsecureConnection());
            } else {
                clientBuilder.serviceUrl(isNotBlank(cluster.getBrokerServiceUrl())
                    ? cluster.getBrokerServiceUrl() : cluster.getServiceUrl());
            }

            // Share all the IO threads across broker and client connections
            ClientConfigurationData conf = ((ClientBuilderImpl) clientBuilder).getClientConfigurationData();
            return new PulsarClientImpl(conf, (EventLoopGroup)pulsar.getBrokerService().executor());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
}
 
Example #15
Source File: ServiceUrlProviderTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateClientWithServiceUrlProvider() throws Exception {

    PulsarClient client = PulsarClient.builder()
            .serviceUrlProvider(new TestServiceUrlProvider(pulsar.getSafeBrokerServiceUrl()))
            .statsInterval(1, TimeUnit.SECONDS)
            .build();
    Assert.assertTrue(((PulsarClientImpl) client).getConfiguration().getServiceUrlProvider() instanceof TestServiceUrlProvider);
    Producer<String> producer = client.newProducer(Schema.STRING)
            .topic("persistent://my-property/my-ns/my-topic")
            .create();
    Consumer<String> consumer = client.newConsumer(Schema.STRING)
            .topic("persistent://my-property/my-ns/my-topic")
            .subscriptionName("my-subscribe")
            .subscribe();
    for (int i = 0; i < 100; i++) {
        producer.send("Hello Pulsar[" + i + "]");
    }
    client.updateServiceUrl(pulsar.getSafeBrokerServiceUrl());
    for (int i = 100; i < 200; i++) {
        producer.send("Hello Pulsar[" + i + "]");
    }
    int received = 0;
    do {
        Message<String> message = consumer.receive();
        System.out.println(message.getValue());
        received++;
    } while (received < 200);
    Assert.assertEquals(200, received);
    producer.close();
    consumer.close();
    client.close();
}
 
Example #16
Source File: ConsumerCleanupTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllTimerTaskShouldCanceledAfterConsumerClosed() throws PulsarClientException, InterruptedException {
    PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 1);
    Consumer<byte[]> consumer = pulsarClient.newConsumer()
            .topic("persistent://public/default/" + UUID.randomUUID().toString())
            .subscriptionName("test")
            .subscribe();
    consumer.close();
    Thread.sleep(2000);
    HashedWheelTimer timer = (HashedWheelTimer) ((PulsarClientImpl) pulsarClient).timer();
    Assert.assertEquals(timer.pendingTimeouts(), 0);
}
 
Example #17
Source File: CachedPulsarClient.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static PulsarClientImpl getOrCreate(ClientConfigurationData config) throws ExecutionException {
    PulsarClientImpl instance = guavaCache.get(config);
    if (instance.getState().get() == PulsarClientImpl.State.Open) {
        return instance;
    } else {
        guavaCache.invalidate(config);
        return guavaCache.get(config);
    }
}
 
Example #18
Source File: ProducerCleanupTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllTimerTaskShouldCanceledAfterProducerClosed() throws PulsarClientException, InterruptedException {
    Producer<byte[]> producer = pulsarClient.newProducer()
            .topic("persistent://public/default/" + UUID.randomUUID().toString())
            .sendTimeout(1, TimeUnit.SECONDS)
            .create();
    producer.close();
    Thread.sleep(2000);
    HashedWheelTimer timer = (HashedWheelTimer) ((PulsarClientImpl) pulsarClient).timer();
    Assert.assertEquals(timer.pendingTimeouts(), 0);
}
 
Example #19
Source File: BasePulsarOutputFormat.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private Producer<byte[]> createPulsarProducer()
        throws PulsarClientException {
    try {
        PulsarClientImpl client = new PulsarClientImpl(clientConf);
        return client.createProducerAsync(producerConf).get();
    } catch (PulsarClientException | InterruptedException | ExecutionException e) {
        LOG.error("Pulsar producer cannot be created.", e);
        throw new PulsarClientException(e);
    }
}
 
Example #20
Source File: ReplicatorTlsTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplicationClient() throws Exception {
    log.info("--- Starting ReplicatorTlsTest::testReplicationClient ---");
    for (BrokerService ns : Lists.newArrayList(ns1, ns2, ns3)) {
        ns.getReplicationClients().forEach((cluster, client) -> {
            assertTrue(((PulsarClientImpl) client).getConfiguration().isUseTls());
            assertEquals(((PulsarClientImpl) client).getConfiguration().getTlsTrustCertsFilePath(),
                    TLS_SERVER_CERT_FILE_PATH);
        });
    }
}
 
Example #21
Source File: PersistentTopicTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testClosingReplicationProducerTwice() throws Exception {
    final String globalTopicName = "persistent://prop/global/ns/testClosingReplicationProducerTwice";
    String localCluster = "local";
    String remoteCluster = "remote";
    final ManagedLedger ledgerMock = mock(ManagedLedger.class);
    doNothing().when(ledgerMock).asyncDeleteCursor(any(), any(), any());
    doReturn(new ArrayList<Object>()).when(ledgerMock).getCursors();

    PersistentTopic topic = new PersistentTopic(globalTopicName, ledgerMock, brokerService);

    final URL brokerUrl = new URL(
            "http://" + pulsar.getAdvertisedAddress() + ":" + pulsar.getConfiguration().getBrokerServicePort().get());
    PulsarClient client = spy(PulsarClient.builder().serviceUrl(brokerUrl.toString()).build());
    PulsarClientImpl clientImpl = (PulsarClientImpl) client;
    doReturn(new CompletableFuture<Producer>()).when(clientImpl)
        .createProducerAsync(any(ProducerConfigurationData.class), any(Schema.class));

    ManagedCursor cursor = mock(ManagedCursorImpl.class);
    doReturn(remoteCluster).when(cursor).getName();
    brokerService.getReplicationClients().put(remoteCluster, client);
    PersistentReplicator replicator = new PersistentReplicator(topic, cursor, localCluster, remoteCluster, brokerService);

    // PersistentReplicator constructor calls startProducer()
    verify(clientImpl)
        .createProducerAsync(
            any(ProducerConfigurationData.class),
            any(), eq(null)
        );

    replicator.disconnect(false);
    replicator.disconnect(false);

    replicator.startProducer();

    verify(clientImpl, Mockito.times(2)).createProducerAsync(any(), any(), any());
}
 
Example #22
Source File: ClientGetSchemaTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "serviceUrl")
public void testGetSchema(String serviceUrl) throws Exception {
    @Cleanup
    PulsarClientImpl client = (PulsarClientImpl) PulsarClient.builder().serviceUrl(serviceUrl).build();

    assertEquals(client.getSchema("non-existing-topic").join(), Optional.empty());
    assertEquals(client.getSchema(topicBytes).join(), Optional.empty());
    assertEquals(client.getSchema(topicString).join(), Optional.of(Schema.STRING.getSchemaInfo()));
    assertEquals(client.getSchema(topicJson).join(), Optional.of(Schema.JSON(MyClass.class).getSchemaInfo()));
    assertEquals(client.getSchema(topicAvro).join(), Optional.of(Schema.AVRO(MyClass.class).getSchemaInfo()));
}
 
Example #23
Source File: KafkaTopicManager.java    From kop with Apache License 2.0 5 votes vote down vote up
private Producer registerInPersistentTopic(PersistentTopic persistentTopic) throws Exception {
    Producer producer = new InternalProducer(persistentTopic, internalServerCnx,
        ((PulsarClientImpl) (pulsarService.getClient())).newRequestId(),
        brokerService.generateUniqueProducerName());

    if (log.isDebugEnabled()) {
        log.debug("[{}] Register Mock Producer {} into PersistentTopic {}",
            requestHandler.ctx.channel(), producer, persistentTopic.getName());
    }

    // this will register and add USAGE_COUNT_UPDATER.
    persistentTopic.addProducer(producer);
    return producer;
}
 
Example #24
Source File: PulsarSourceTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that JavaInstance does not support functions that take Void type as input.
 */
private static PulsarClientImpl getPulsarClient() throws PulsarClientException {
    PulsarClientImpl pulsarClient = mock(PulsarClientImpl.class);
    ConsumerBuilder<?> goodConsumerBuilder = mock(ConsumerBuilder.class);
    ConsumerBuilder<?> badConsumerBuilder = mock(ConsumerBuilder.class);
    doReturn(goodConsumerBuilder).when(goodConsumerBuilder).topics(argThat(new TopicMatcher("persistent://sample/ns1/test_result")));
    doReturn(goodConsumerBuilder).when(goodConsumerBuilder).topics(argThat(new TopicMatcher("persistent://sample/ns1/test_result1")));
    doReturn(badConsumerBuilder).when(goodConsumerBuilder).topics(argThat(new TopicMatcher("persistent://sample/ns1/test_result2")));
    doReturn(goodConsumerBuilder).when(goodConsumerBuilder).topics(argThat(new TopicMatcher("persistent://sample/ns1/test_result3")));
    doReturn(goodConsumerBuilder).when(goodConsumerBuilder).cryptoFailureAction(any());
    doReturn(goodConsumerBuilder).when(goodConsumerBuilder).subscriptionName(any());
    doReturn(goodConsumerBuilder).when(goodConsumerBuilder).subscriptionInitialPosition(any());
    doReturn(goodConsumerBuilder).when(goodConsumerBuilder).subscriptionType(any());
    doReturn(goodConsumerBuilder).when(goodConsumerBuilder).ackTimeout(anyLong(), any());
    doReturn(goodConsumerBuilder).when(goodConsumerBuilder).messageListener(any());
    doReturn(goodConsumerBuilder).when(goodConsumerBuilder).properties(any());
    doReturn(badConsumerBuilder).when(badConsumerBuilder).cryptoFailureAction(any());
    doReturn(badConsumerBuilder).when(badConsumerBuilder).subscriptionName(any());
    doReturn(badConsumerBuilder).when(badConsumerBuilder).subscriptionInitialPosition(any());
    doReturn(badConsumerBuilder).when(badConsumerBuilder).subscriptionType(any());
    doReturn(badConsumerBuilder).when(badConsumerBuilder).ackTimeout(anyLong(), any());
    doReturn(badConsumerBuilder).when(badConsumerBuilder).messageListener(any());
    doReturn(badConsumerBuilder).when(badConsumerBuilder).properties(any());

    Consumer<?> consumer = mock(Consumer.class);
    doReturn(consumer).when(goodConsumerBuilder).subscribe();
    doReturn(goodConsumerBuilder).when(pulsarClient).newConsumer(any());
    doReturn(CompletableFuture.completedFuture(consumer)).when(goodConsumerBuilder).subscribeAsync();
    CompletableFuture<Consumer<?>> badFuture = new CompletableFuture<>();
    badFuture.completeExceptionally(new PulsarClientException("Some Error"));
    doReturn(badFuture).when(badConsumerBuilder).subscribeAsync();
    doThrow(PulsarClientException.class).when(badConsumerBuilder).subscribe();
    doReturn(CompletableFuture.completedFuture(Optional.empty())).when(pulsarClient).getSchema(anyString());
    return pulsarClient;
}
 
Example #25
Source File: TransactionImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
TransactionImpl(PulsarClientImpl client,
                long transactionTimeoutMs,
                long txnIdLeastBits,
                long txnIdMostBits) {
    this.client = client;
    this.transactionTimeoutMs = transactionTimeoutMs;
    this.txnIdLeastBits = txnIdLeastBits;
    this.txnIdMostBits = txnIdMostBits;
    this.sendOps = new LinkedHashMap<>();
    this.producedTopics = new HashSet<>();
    this.ackOps = new HashSet<>();
    this.ackedTopics = new HashSet<>();
}
 
Example #26
Source File: MultiVersionSchemaInfoProviderTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setup() {
    PulsarClientImpl client = mock(PulsarClientImpl.class);
    when(client.getLookup()).thenReturn(mock(LookupService.class));
    schemaProvider = new MultiVersionSchemaInfoProvider(
            TopicName.get("persistent://public/default/my-topic"), client);
}
 
Example #27
Source File: ConfigurationDataUtilsTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigBuilder() throws PulsarClientException {
    ClientConfigurationData clientConfig = new ClientConfigurationData();
    clientConfig.setServiceUrl("pulsar://unknown:6650");
    clientConfig.setStatsIntervalSeconds(80);

    PulsarClientImpl pulsarClient = new PulsarClientImpl(clientConfig);
    assertNotNull(pulsarClient, "Pulsar client built using config should not be null");

    assertEquals(pulsarClient.getConfiguration().getServiceUrl(), "pulsar://unknown:6650");
    assertEquals(pulsarClient.getConfiguration().getNumListenerThreads(), 1, "builder default not set properly");
    assertEquals(pulsarClient.getConfiguration().getStatsIntervalSeconds(), 80,
            "builder default should overrite if set explicitly");
}
 
Example #28
Source File: PulsarLogger.java    From json-data-generator with Apache License 2.0 5 votes vote down vote up
public PulsarLogger(Map<String, Object> props) throws PulsarClientException {
    
    String brokerHost = (String) props.get(PULSAR_SERVICE_URL_PROP_NAME);
    Integer brokerPort = (Integer) props.get(PULSAR_SERVICE_URL_PORT_PROP_NAME);

    pulsarURL.append(brokerHost);
    pulsarURL.append(":");
    pulsarURL.append(brokerPort);
    
    this.topic = (String) props.get("topic");
    
    if (props.get("sync") != null) {
        this.sync = (Boolean) props.get("sync");
    } else {
        this.sync = false;
    }
    
    if (props.get("flatten") != null) {
        this.flatten = (Boolean) props.get("flatten");
    } else {
        this.flatten = false;
    }
    
    this.pulsarClient = new PulsarClientImpl(pulsarURL.toString(), new ClientConfiguration());
    this.producer = pulsarClient.createProducer(topic);
    
    this.jsonUtils = new JsonUtils();
    
}
 
Example #29
Source File: ProducerConstructorInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    PulsarClientImpl pulsarClient = (PulsarClientImpl) allArguments[0];
    String topic = (String) allArguments[1];
    ProducerEnhanceRequiredInfo producerEnhanceRequiredInfo = new ProducerEnhanceRequiredInfo();
    producerEnhanceRequiredInfo.setTopic(topic);
    /*
     * Pulsar url can specify with specific URL or a service url provider, use pulsarClient.getLookup().getServiceUrl()
     * can handle the service url provider which use a dynamic service url
     */
    producerEnhanceRequiredInfo.setServiceUrl(pulsarClient.getLookup().getServiceUrl());
    objInst.setSkyWalkingDynamicField(producerEnhanceRequiredInfo);
}
 
Example #30
Source File: ConsumerConstructorInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    PulsarClientImpl pulsarClient = (PulsarClientImpl) allArguments[0];
    String topic = (String) allArguments[1];
    ConsumerConfigurationData consumerConfigurationData = (ConsumerConfigurationData) allArguments[2];
    ConsumerEnhanceRequiredInfo requireInfo = new ConsumerEnhanceRequiredInfo();
    /*
     * Pulsar url can specify with specific URL or a service url provider, use pulsarClient.getLookup().getServiceUrl()
     * can handle the service url provider which use a dynamic service url
     */
    requireInfo.setServiceUrl(pulsarClient.getLookup().getServiceUrl());
    requireInfo.setTopic(topic);
    requireInfo.setSubscriptionName(consumerConfigurationData.getSubscriptionName());
    objInst.setSkyWalkingDynamicField(requireInfo);
}