org.apache.pulsar.client.api.ClientBuilder Java Examples

The following examples show how to use org.apache.pulsar.client.api.ClientBuilder. 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: PulsarBlockChainEventBroadcaster.java    From eventeum with Apache License 2.0 6 votes vote down vote up
public PulsarBlockChainEventBroadcaster(PulsarSettings settings, ObjectMapper mapper) throws PulsarClientException {
	this.mapper = mapper;

	ClientBuilder builder = PulsarClient.builder();

	if (settings.getConfig() != null) {
		builder.loadConf(settings.getConfig());
	}

	Authentication authSettings = settings.getAuthentication();
	if (authSettings != null) {
		builder.authentication(
				authSettings.getPluginClassName(),
				authSettings.getParams());
	}

	client = builder.build();

	blockEventProducer = createProducer(settings.getTopic().getBlockEvents());
	contractEventProducer = createProducer(settings.getTopic().getContractEvents());
       transactionEventProducer = createProducer(settings.getTopic().getTransactionEvents());
}
 
Example #2
Source File: TestBasePulsarConfig.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws PulsarClientException {
  // New mocks are needed when using Mockito.times() as each mock counts the number of times a method has been called
  contextMock = Mockito.mock(Stage.Context.class);
  pulsarSecurityConfigMock = Mockito.mock(PulsarSecurityConfig.class);
  pulsarClientMock = Mockito.mock(PulsarClient.class);
  clientBuilderMock = Mockito.mock(ClientBuilder.class);

  PowerMockito.mockStatic(PulsarClient.class);
  BDDMockito.given(PulsarClient.builder()).willReturn(clientBuilderMock);

  Mockito.when(pulsarSecurityConfigMock.init(contextMock)).thenReturn(Collections.emptyList());

  Mockito.when(clientBuilderMock.serviceUrl(Mockito.anyString())).thenReturn(clientBuilderMock);
  Mockito.when(clientBuilderMock.keepAliveInterval(Mockito.anyInt(), Mockito.any())).thenReturn(clientBuilderMock);
  Mockito.when(clientBuilderMock.operationTimeout(Mockito.anyInt(), Mockito.any())).thenReturn(clientBuilderMock);
  Mockito.when(clientBuilderMock.build()).thenReturn(pulsarClientMock);

  basePulsarConfig = new BasePulsarConfig();
  basePulsarConfig.serviceURL = "http://localhost:8080";
  basePulsarConfig.keepAliveInterval = 30000;
  basePulsarConfig.operationTimeout = 30000;
  basePulsarConfig.properties = new HashMap<>();
  basePulsarConfig.securityConfig = pulsarSecurityConfigMock;
}
 
Example #3
Source File: PulsarSecurityConfig.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public ClientBuilder configurePulsarBuilder(ClientBuilder builder) throws StageException {
  builder.enableTls(tlsEnabled);
  if (tlsEnabled) {
    builder.tlsTrustCertsFilePath(caCertFileFullPath);

    if (tlsAuthEnabled) {
      Map<String, String> authParams = new HashMap<>();
      authParams.put("tlsCertFile", clientCertFileFullPath);
      authParams.put("tlsKeyFile", clientKeyFileFullPath);

      try {
        builder.authentication(AuthenticationFactory.create(AuthenticationTls.class.getName(), authParams));
      } catch (PulsarClientException.UnsupportedAuthenticationException e) {
        throw new StageException(PulsarErrors.PULSAR_17, e.toString(), e);
      }
    }
  }

  return builder;
}
 
Example #4
Source File: KeyStoreTlsProducerConsumerTestWithAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected void internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    if (pulsarClient != null) {
        pulsarClient.close();
    }

    Set<String> tlsProtocols = Sets.newConcurrentHashSet();
    tlsProtocols.add("TLSv1.2");

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .tlsProtocols(tlsProtocols)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    pulsarClient = clientBuilder.build();
}
 
Example #5
Source File: TlsProducerConsumerBase.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected void internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    if (pulsarClient != null) {
        pulsarClient.close();
    }

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(lookupUrl)
            .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH).enableTls(true).allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
        authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
        clientBuilder.authentication(AuthenticationTls.class.getName(), authParams);
    }
    pulsarClient = clientBuilder.build();
}
 
Example #6
Source File: TlsProducerConsumerBase.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected void internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    if (pulsarClient != null) {
        pulsarClient.close();
    }

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(lookupUrl)
            .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH).enableTls(true).allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
        authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
        clientBuilder.authentication(AuthenticationTls.class.getName(), authParams);
    }
    pulsarClient = clientBuilder.build();
}
 
Example #7
Source File: KeyStoreTlsProducerConsumerTestWithoutAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected void internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    if (pulsarClient != null) {
        pulsarClient.close();
    }

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    pulsarClient = clientBuilder.build();
}
 
Example #8
Source File: ProxyKeyStoreTlsTestWithoutAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected PulsarClient internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    ClientBuilder clientBuilder = PulsarClient.builder()
            .serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    return clientBuilder.build();
}
 
Example #9
Source File: ProxyKeyStoreTlsTestWithAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected PulsarClient internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    ClientBuilder clientBuilder = PulsarClient.builder()
            .serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    return clientBuilder.build();
}
 
Example #10
Source File: PulsarClientTool.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void updateConfig() throws UnsupportedAuthenticationException {
    ClientBuilder clientBuilder = PulsarClient.builder();
    Authentication authentication = null;
    if (isNotBlank(this.authPluginClassName)) {
        authentication = AuthenticationFactory.create(authPluginClassName, authParams);
        clientBuilder.authentication(authentication);
    }
    clientBuilder.allowTlsInsecureConnection(this.tlsAllowInsecureConnection);
    clientBuilder.tlsTrustCertsFilePath(this.tlsTrustCertsFilePath);
    clientBuilder.serviceUrl(serviceURL);

    clientBuilder.useKeyStoreTls(useKeyStoreTls)
            .tlsTrustStoreType(tlsTrustStoreType)
            .tlsTrustStorePath(tlsTrustStorePath)
            .tlsTrustStorePassword(tlsTrustStorePassword);

    if (StringUtils.isNotBlank(proxyServiceURL)) {
        if (proxyProtocol == null) {
            System.out.println("proxy-protocol must be provided with proxy-url");
            System.exit(-1);
        }
        clientBuilder.proxyServiceUrl(proxyServiceURL, proxyProtocol);
    }
    this.produceCommand.updateConfig(clientBuilder, authentication, this.serviceURL);
    this.consumeCommand.updateConfig(clientBuilder, authentication, this.serviceURL);
}
 
Example #11
Source File: ThreadRuntimeFactory.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private static PulsarClient createPulsarClient(String pulsarServiceUrl, AuthenticationConfig authConfig)
        throws PulsarClientException {
    ClientBuilder clientBuilder = null;
    if (isNotBlank(pulsarServiceUrl)) {
        clientBuilder = PulsarClient.builder().serviceUrl(pulsarServiceUrl);
        if (authConfig != null) {
            if (isNotBlank(authConfig.getClientAuthenticationPlugin())
                    && isNotBlank(authConfig.getClientAuthenticationParameters())) {
                clientBuilder.authentication(authConfig.getClientAuthenticationPlugin(),
                        authConfig.getClientAuthenticationParameters());
            }
            clientBuilder.enableTls(authConfig.isUseTls());
            clientBuilder.allowTlsInsecureConnection(authConfig.isTlsAllowInsecureConnection());
            clientBuilder.enableTlsHostnameVerification(authConfig.isTlsHostnameVerificationEnable());
            clientBuilder.tlsTrustCertsFilePath(authConfig.getTlsTrustCertsFilePath());
        }
        return clientBuilder.build();
    }
    return null;
}
 
Example #12
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 #13
Source File: TestPulsarSecurityConfig.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  pulsarSecurityConfig = new PulsarSecurityConfig();
  pulsarSecurityConfig.tlsEnabled = false;
  pulsarSecurityConfig.caCertPem = null;
  pulsarSecurityConfig.tlsAuthEnabled = false;
  pulsarSecurityConfig.clientCertPem = null;
  pulsarSecurityConfig.clientKeyPem = null;

  contextMock = Mockito.mock(Stage.Context.class);
  Mockito.when(contextMock.getResourcesDirectory()).thenReturn(resourceDirectoryPath);

  clientBuilderMock = Mockito.mock(ClientBuilder.class);
}
 
Example #14
Source File: WorkerUtils.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static PulsarClient getPulsarClient(String pulsarServiceUrl, String authPlugin, String authParams,
                                           Boolean useTls, String tlsTrustCertsFilePath,
                                           Boolean allowTlsInsecureConnection,
                                           Boolean enableTlsHostnameVerificationEnable) {

    try {
        ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(pulsarServiceUrl);

        if (isNotBlank(authPlugin)
                && isNotBlank(authParams)) {
            clientBuilder.authentication(authPlugin, authParams);
        }
        if (useTls != null) {
            clientBuilder.enableTls(useTls);
        }
        if (allowTlsInsecureConnection != null) {
            clientBuilder.allowTlsInsecureConnection(allowTlsInsecureConnection);
        }
        if (isNotBlank(tlsTrustCertsFilePath)) {
            clientBuilder.tlsTrustCertsFilePath(tlsTrustCertsFilePath);
        }
        if (enableTlsHostnameVerificationEnable != null) {
            clientBuilder.enableTlsHostnameVerification(enableTlsHostnameVerificationEnable);
        }

        return clientBuilder.build();
    } catch (PulsarClientException e) {
        log.error("Error creating pulsar client", e);
        throw new RuntimeException(e);
    }
}
 
Example #15
Source File: ProxyWithAuthorizationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private PulsarClient createPulsarClient(String proxyServiceUrl, ClientBuilder clientBuilder)
        throws PulsarClientException {
    Map<String, String> authParams = Maps.newHashMap();
    authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
    authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
    Authentication authTls = new AuthenticationTls();
    authTls.configure(authParams);

    return clientBuilder.serviceUrl(proxyServiceUrl).statsInterval(0, TimeUnit.SECONDS)
            .tlsTrustCertsFilePath(TLS_PROXY_TRUST_CERT_FILE_PATH).allowTlsInsecureConnection(true)
            .authentication(authTls).enableTls(true)
            .operationTimeout(1000, TimeUnit.MILLISECONDS).build();
}
 
Example #16
Source File: ClientBuilderImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public ClientBuilder authentication(String authPluginClassName, String authParamsString)
        throws UnsupportedAuthenticationException {
    conf.setAuthPluginClassName(authPluginClassName);
    conf.setAuthParams(authParamsString);
    conf.setAuthParamMap(null);
    conf.setAuthentication(AuthenticationFactory.create(authPluginClassName, authParamsString));
    return this;
}
 
Example #17
Source File: ClientBuilderImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public ClientBuilder authentication(String authPluginClassName, Map<String, String> authParams)
        throws UnsupportedAuthenticationException {
    conf.setAuthPluginClassName(authPluginClassName);
    conf.setAuthParamMap(authParams);
    conf.setAuthParams(null);
    conf.setAuthentication(AuthenticationFactory.create(authPluginClassName, authParams));
    return this;
}
 
Example #18
Source File: PulsarService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public synchronized PulsarClient getClient() throws PulsarServerException {
    if (this.client == null) {
        try {
            ClientBuilder builder = PulsarClient.builder()
                .serviceUrl(this.getConfiguration().isTlsEnabled()
                            ? this.brokerServiceUrlTls : this.brokerServiceUrl)
                .enableTls(this.getConfiguration().isTlsEnabled())
                .allowTlsInsecureConnection(this.getConfiguration().isTlsAllowInsecureConnection())
                .tlsTrustCertsFilePath(this.getConfiguration().getTlsCertificateFilePath());

            if (this.getConfiguration().isBrokerClientTlsEnabled()) {
                if (this.getConfiguration().isBrokerClientTlsEnabledWithKeyStore()) {
                    builder.useKeyStoreTls(true)
                            .tlsTrustStoreType(this.getConfiguration().getBrokerClientTlsTrustStoreType())
                            .tlsTrustStorePath(this.getConfiguration().getBrokerClientTlsTrustStore())
                            .tlsTrustStorePassword(this.getConfiguration().getBrokerClientTlsTrustStorePassword());
                } else {
                    builder.tlsTrustCertsFilePath(
                            isNotBlank(this.getConfiguration().getBrokerClientTrustCertsFilePath())
                                    ? this.getConfiguration().getBrokerClientTrustCertsFilePath()
                                    : this.getConfiguration().getTlsCertificateFilePath());
                }
            }

            if (isNotBlank(this.getConfiguration().getBrokerClientAuthenticationPlugin())) {
                builder.authentication(this.getConfiguration().getBrokerClientAuthenticationPlugin(),
                                       this.getConfiguration().getBrokerClientAuthenticationParameters());
            }
            this.client = builder.build();
        } catch (Exception e) {
            throw new PulsarServerException(e);
        }
    }
    return this.client;
}
 
Example #19
Source File: PulsarKafkaProducerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testPulsarKafkaProducer() {
    ClientBuilder mockClientBuilder = mock(ClientBuilder.class);
    ProducerBuilder mockProducerBuilder = mock(ProducerBuilder.class);
    doAnswer(invocation -> {
        Assert.assertEquals((int)invocation.getArguments()[0], 1000000, "Send time out is suppose to be 1000.");
        return mockProducerBuilder;
    }).when(mockProducerBuilder).sendTimeout(anyInt(), any(TimeUnit.class));
    doReturn(mockClientBuilder).when(mockClientBuilder).serviceUrl(anyString());
    doAnswer(invocation -> {
        Assert.assertEquals((int)invocation.getArguments()[0], 1000, "Keep alive interval is suppose to be 1000.");
        return mockClientBuilder;
    }).when(mockClientBuilder).keepAliveInterval(anyInt(), any(TimeUnit.class));

    PowerMockito.mockStatic(PulsarClientKafkaConfig.class);
    PowerMockito.mockStatic(PulsarProducerKafkaConfig.class);
    when(PulsarClientKafkaConfig.getClientBuilder(any(Properties.class))).thenReturn(mockClientBuilder);
    when(PulsarProducerKafkaConfig.getProducerBuilder(any(), any())).thenReturn(mockProducerBuilder);

    Properties properties = new Properties();
    properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    properties.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, DefaultPartitioner.class);
    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, Arrays.asList("pulsar://localhost:6650"));
    properties.put(ProducerConfig.CONNECTIONS_MAX_IDLE_MS_CONFIG, "1000000");
    properties.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, "1000000");
    properties.put(PulsarProducerKafkaConfig.BLOCK_IF_PRODUCER_QUEUE_FULL, Boolean.FALSE.toString());

    new PulsarKafkaProducer<>(properties);

    verify(mockClientBuilder, times(1)).keepAliveInterval(1000, TimeUnit.SECONDS);
    verify(mockProducerBuilder, times(1)).sendTimeout(1000000, TimeUnit.MILLISECONDS);
    verify(mockProducerBuilder, times(1)).blockIfQueueFull(false);
}
 
Example #20
Source File: PulsarKafkaProducerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testPulsarKafkaProducer() {
    ClientBuilder mockClientBuilder = mock(ClientBuilder.class);
    ProducerBuilder mockProducerBuilder = mock(ProducerBuilder.class);
    doAnswer(invocation -> {
        Assert.assertEquals((int)invocation.getArguments()[0], 1000000, "Send time out is suppose to be 1000.");
        return mockProducerBuilder;
    }).when(mockProducerBuilder).sendTimeout(anyInt(), any(TimeUnit.class));
    doReturn(mockClientBuilder).when(mockClientBuilder).serviceUrl(anyString());
    doAnswer(invocation -> {
        Assert.assertEquals((int)invocation.getArguments()[0], 1000, "Keep alive interval is suppose to be 1000.");
        return mockClientBuilder;
    }).when(mockClientBuilder).keepAliveInterval(anyInt(), any(TimeUnit.class));

    PowerMockito.mockStatic(PulsarClientKafkaConfig.class);
    PowerMockito.mockStatic(PulsarProducerKafkaConfig.class);
    when(PulsarClientKafkaConfig.getClientBuilder(any(Properties.class))).thenReturn(mockClientBuilder);
    when(PulsarProducerKafkaConfig.getProducerBuilder(any(), any())).thenReturn(mockProducerBuilder);

    Properties properties = new Properties();
    properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    properties.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, DefaultPartitioner.class);
    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, Arrays.asList("pulsar://localhost:6650"));
    properties.put(ProducerConfig.CONNECTIONS_MAX_IDLE_MS_CONFIG, "1000000");
    properties.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, "1000000");

    new PulsarKafkaProducer<>(properties);

    verify(mockClientBuilder, times(1)).keepAliveInterval(1000, TimeUnit.SECONDS);
    verify(mockProducerBuilder, times(1)).sendTimeout(1000000, TimeUnit.MILLISECONDS);
}
 
Example #21
Source File: WebSocketService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private PulsarClient createClientInstance(ClusterData clusterData) throws IOException {
     ClientBuilder clientBuilder = PulsarClient.builder() //
             .statsInterval(0, TimeUnit.SECONDS) //
             .enableTls(config.isTlsEnabled()) //
             .allowTlsInsecureConnection(config.isTlsAllowInsecureConnection()) //
             .tlsTrustCertsFilePath(config.getBrokerClientTrustCertsFilePath()) //
             .ioThreads(config.getWebSocketNumIoThreads()) //
             .connectionsPerBroker(config.getWebSocketConnectionsPerBroker());

     if (isNotBlank(config.getBrokerClientAuthenticationPlugin())
             && isNotBlank(config.getBrokerClientAuthenticationParameters())) {
         clientBuilder.authentication(config.getBrokerClientAuthenticationPlugin(),
                 config.getBrokerClientAuthenticationParameters());
     }

     if (config.isBrokerClientTlsEnabled()) {
if (isNotBlank(clusterData.getBrokerServiceUrlTls())) {
		clientBuilder.serviceUrl(clusterData.getBrokerServiceUrlTls());
} else if (isNotBlank(clusterData.getServiceUrlTls())) {
		clientBuilder.serviceUrl(clusterData.getServiceUrlTls());
}
     } else if (isNotBlank(clusterData.getBrokerServiceUrl())) {
         clientBuilder.serviceUrl(clusterData.getBrokerServiceUrl());
     } else {
         clientBuilder.serviceUrl(clusterData.getServiceUrl());
     }

     return clientBuilder.build();
 }
 
Example #22
Source File: ClientBuilderImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public ClientBuilder maxConcurrentLookupRequests(int concurrentLookupRequests) {
    conf.setConcurrentLookupRequest(concurrentLookupRequests);
    return this;
}
 
Example #23
Source File: PulsarClientKafkaConfig.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static ClientBuilder getClientBuilder(Properties properties) {
    ClientBuilder clientBuilder = PulsarClient.builder();

    if (properties.containsKey(AUTHENTICATION_CLASS)) {
        String className = properties.getProperty(AUTHENTICATION_CLASS);
        try {
            if (properties.containsKey(AUTHENTICATION_PARAMS_STRING)) {
                String authParamsString = (String) properties.get(AUTHENTICATION_PARAMS_STRING);
                clientBuilder.authentication(className, authParamsString);
            } else if (properties.containsKey(AUTHENTICATION_PARAMS_MAP)) {
                Map<String, String> authParams = (Map<String, String>) properties.get(AUTHENTICATION_PARAMS_MAP);
                clientBuilder.authentication(className, authParams);
            } else {
                @SuppressWarnings("unchecked")
                Class<Authentication> clazz = (Class<Authentication>) Class.forName(className);
                Authentication auth = clazz.newInstance();
                clientBuilder.authentication(auth);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    clientBuilder.enableTls(Boolean.parseBoolean(properties.getProperty(USE_TLS, "false")));
    clientBuilder.allowTlsInsecureConnection(
            Boolean.parseBoolean(properties.getProperty(TLS_ALLOW_INSECURE_CONNECTION, "false")));
    clientBuilder.enableTlsHostnameVerification(
            Boolean.parseBoolean(properties.getProperty(TLS_HOSTNAME_VERIFICATION, "false")));

    if (properties.containsKey(TLS_TRUST_CERTS_FILE_PATH)) {
        clientBuilder.tlsTrustCertsFilePath(properties.getProperty(TLS_TRUST_CERTS_FILE_PATH));
    }

    if (properties.containsKey(OPERATION_TIMEOUT_MS)) {
        clientBuilder.operationTimeout(Integer.parseInt(properties.getProperty(OPERATION_TIMEOUT_MS)),
                TimeUnit.MILLISECONDS);
    }

    if (properties.containsKey(STATS_INTERVAL_SECONDS)) {
        clientBuilder.statsInterval(Integer.parseInt(properties.getProperty(STATS_INTERVAL_SECONDS)),
                TimeUnit.SECONDS);
    }

    if (properties.containsKey(NUM_IO_THREADS)) {
        clientBuilder.ioThreads(Integer.parseInt(properties.getProperty(NUM_IO_THREADS)));
    }

    if (properties.containsKey(CONNECTIONS_PER_BROKER)) {
        clientBuilder.connectionsPerBroker(Integer.parseInt(properties.getProperty(CONNECTIONS_PER_BROKER)));
    }

    if (properties.containsKey(USE_TCP_NODELAY)) {
        clientBuilder.enableTcpNoDelay(Boolean.parseBoolean(properties.getProperty(USE_TCP_NODELAY)));
    }

    if (properties.containsKey(CONCURRENT_LOOKUP_REQUESTS)) {
        clientBuilder.maxConcurrentLookupRequests(Integer.parseInt(properties.getProperty(CONCURRENT_LOOKUP_REQUESTS)));
    }

    if (properties.containsKey(MAX_NUMBER_OF_REJECTED_REQUESTS_PER_CONNECTION)) {
        clientBuilder.maxNumberOfRejectedRequestPerConnection(
                Integer.parseInt(properties.getProperty(MAX_NUMBER_OF_REJECTED_REQUESTS_PER_CONNECTION)));
    }

    if (properties.containsKey(KEEPALIVE_INTERVAL_MS)) {
        clientBuilder.keepAliveInterval(Integer.parseInt(properties.getProperty(KEEPALIVE_INTERVAL_MS)),
                TimeUnit.MILLISECONDS);
    }

    return clientBuilder;
}
 
Example #24
Source File: PulsarClientKafkaConfig.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static ClientBuilder getClientBuilder(Properties properties) {
    ClientBuilder clientBuilder = PulsarClient.builder();
    if (properties == null) {
        return clientBuilder;
    }

    if (properties.containsKey(AUTHENTICATION_CLASS)) {
        String className = properties.getProperty(AUTHENTICATION_CLASS);
        try {
            if (properties.containsKey(AUTHENTICATION_PARAMS_STRING)) {
                String authParamsString = (String) properties.get(AUTHENTICATION_PARAMS_STRING);
                clientBuilder.authentication(className, authParamsString);
            } else if (properties.containsKey(AUTHENTICATION_PARAMS_MAP)) {
                Map<String, String> authParams = (Map<String, String>) properties.get(AUTHENTICATION_PARAMS_MAP);
                clientBuilder.authentication(className, authParams);
            } else {
                @SuppressWarnings("unchecked")
                Class<Authentication> clazz = (Class<Authentication>) Class.forName(className);
                Authentication auth = clazz.newInstance();
                clientBuilder.authentication(auth);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    clientBuilder.enableTls(Boolean.parseBoolean(properties.getProperty(USE_TLS, "false")));
    clientBuilder.allowTlsInsecureConnection(
            Boolean.parseBoolean(properties.getProperty(TLS_ALLOW_INSECURE_CONNECTION, "false")));
    clientBuilder.enableTlsHostnameVerification(
            Boolean.parseBoolean(properties.getProperty(TLS_HOSTNAME_VERIFICATION, "false")));

    if (properties.containsKey(TLS_TRUST_CERTS_FILE_PATH)) {
        clientBuilder.tlsTrustCertsFilePath(properties.getProperty(TLS_TRUST_CERTS_FILE_PATH));
    }

    if (properties.containsKey(OPERATION_TIMEOUT_MS)) {
        clientBuilder.operationTimeout(Integer.parseInt(properties.getProperty(OPERATION_TIMEOUT_MS)),
                TimeUnit.MILLISECONDS);
    }

    if (properties.containsKey(STATS_INTERVAL_SECONDS)) {
        clientBuilder.statsInterval(Integer.parseInt(properties.getProperty(STATS_INTERVAL_SECONDS)),
                TimeUnit.SECONDS);
    }

    if (properties.containsKey(NUM_IO_THREADS)) {
        clientBuilder.ioThreads(Integer.parseInt(properties.getProperty(NUM_IO_THREADS)));
    }

    if (properties.containsKey(CONNECTIONS_PER_BROKER)) {
        clientBuilder.connectionsPerBroker(Integer.parseInt(properties.getProperty(CONNECTIONS_PER_BROKER)));
    }

    if (properties.containsKey(USE_TCP_NODELAY)) {
        clientBuilder.enableTcpNoDelay(Boolean.parseBoolean(properties.getProperty(USE_TCP_NODELAY)));
    }

    if (properties.containsKey(CONCURRENT_LOOKUP_REQUESTS)) {
        clientBuilder
                .maxConcurrentLookupRequests(Integer.parseInt(properties.getProperty(CONCURRENT_LOOKUP_REQUESTS)));
    }

    if (properties.containsKey(MAX_NUMBER_OF_REJECTED_REQUESTS_PER_CONNECTION)) {
        clientBuilder.maxNumberOfRejectedRequestPerConnection(
                Integer.parseInt(properties.getProperty(MAX_NUMBER_OF_REJECTED_REQUESTS_PER_CONNECTION)));
    }

    return clientBuilder;
}
 
Example #25
Source File: ClientBuilderImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public ClientBuilder ioThreads(int numIoThreads) {
    conf.setNumIoThreads(numIoThreads);
    return this;
}
 
Example #26
Source File: ClientBuilderImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public ClientBuilder listenerThreads(int numListenerThreads) {
    conf.setNumListenerThreads(numListenerThreads);
    return this;
}
 
Example #27
Source File: PulsarFunctionsITest.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
static void start() throws Exception {
  // Start local bookkeeper ensemble
  final LocalBookkeeperEnsemble bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT,TestUtil::nextFreePort);
  bkEnsemble.start();

  final String brokerServiceUrl = "http://127.0.0.1:" + brokerWebServicePort;

  final ServiceConfiguration config = spy(new ServiceConfiguration());
  config.setClusterName(CLUSTER_NAME);
  final Set<String> superUsers = Sets.newHashSet("superUser");
  config.setSuperUserRoles(superUsers);
  config.setWebServicePort(Optional.of(brokerWebServicePort));
  config.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
  config.setBrokerServicePort(Optional.of(brokerServicePort));
  config.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());
  config.setTlsAllowInsecureConnection(true);
  config.setAdvertisedAddress("localhost");

  config.setAuthenticationEnabled(false);
  config.setAuthorizationEnabled(false);

  config.setBrokerClientTlsEnabled(false);
  config.setAllowAutoTopicCreationType("non-partitioned");

  final WorkerService functionsWorkerService = createPulsarFunctionWorker(config);
  final URL urlTls = new URL(brokerServiceUrl);
  final Optional<WorkerService> functionWorkerService = Optional.of(functionsWorkerService);
  try (final PulsarService pulsar = new PulsarService(config, functionWorkerService)) {
    pulsar.start();
    try (final PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(brokerServiceUrl).allowTlsInsecureConnection(true).build()) {
      // update cluster metadata
      final ClusterData clusterData = new ClusterData(urlTls.toString());
      admin.clusters().updateCluster(config.getClusterName(), clusterData);

      final TenantInfo propAdmin = new TenantInfo();
      propAdmin.getAdminRoles().add("superUser");
      propAdmin.setAllowedClusters(Sets.newHashSet(CLUSTER_NAME));
      admin.tenants().updateTenant(tenant, propAdmin);

      final String jarFilePathUrl = Utils.FILE + ":" + ExclamationFunction.class.getProtectionDomain().getCodeSource().getLocation().getPath();

      final ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(workerConfig.getPulsarServiceUrl());
      try (final PulsarClient pulsarClient = clientBuilder.build()) {
        testE2EPulsarFunction(jarFilePathUrl, admin, pulsarClient);
      }
    }
  }
}
 
Example #28
Source File: ClientBuilderImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public ClientBuilder enableTcpNoDelay(boolean useTcpNoDelay) {
    conf.setUseTcpNoDelay(useTcpNoDelay);
    return this;
}
 
Example #29
Source File: ClientBuilderImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public ClientBuilder enableTls(boolean useTls) {
    conf.setUseTls(useTls);
    return this;
}
 
Example #30
Source File: ClientBuilderImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public ClientBuilder allowTlsInsecureConnection(boolean tlsAllowInsecureConnection) {
    conf.setTlsAllowInsecureConnection(tlsAllowInsecureConnection);
    return this;
}