public CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(DestinationName destination) { return httpClient.get(String.format("admin/%s/partitions", destination.getLookupName()), PartitionedTopicMetadata.class); }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); log.info("{} Connected to broker", ctx.channel()); String authData = ""; if (authentication.getAuthData().hasDataFromCommand()) { authData = authentication.getAuthData().getCommandData(); } // Send CONNECT command ctx.writeAndFlush(Commands.newConnect(authentication.getAuthMethodName(), authData, getPulsarClientVersion())) .addListener(future -> { if (future.isSuccess()) { if (log.isDebugEnabled()) { log.debug("Complete: {}", future.isSuccess()); } state = State.SentConnectFrame; } else { log.warn("Error during handshake", future.cause()); ctx.close(); } }); }
@Override public void shutdown() throws PulsarClientException { try { if (httpClient != null) { httpClient.close(); } cnxPool.close(); timer.stop(); externalExecutorProvider.shutdownNow(); conf.getAuthentication().close(); } catch (Throwable t) { log.warn("Failed to shutdown Pulsar client", t); throw new PulsarClientException(t); } }
public PulsarClientImpl(String serviceUrl, ClientConfiguration conf, EventLoopGroup eventLoopGroup) throws PulsarClientException { if (serviceUrl == null || conf == null || eventLoopGroup == null) { throw new PulsarClientException.InvalidConfigurationException("Invalid client configuration"); } this.eventLoopGroup = eventLoopGroup; this.conf = conf; conf.getAuthentication().start(); cnxPool = new ConnectionPool(this, eventLoopGroup); if (serviceUrl.startsWith("http")) { httpClient = new HttpClient(serviceUrl, conf.getAuthentication(), eventLoopGroup, conf.isTlsAllowInsecureConnection(), conf.getTlsTrustCertsFilePath()); lookup = new HttpLookupService(httpClient, conf.isUseTls()); } else { lookup = new BinaryProtoLookupService(this, serviceUrl, conf.isUseTls()); } timer = new HashedWheelTimer(new DefaultThreadFactory("pulsar-timer"), 1, TimeUnit.MILLISECONDS); externalExecutorProvider = new ExecutorProvider(conf.getListenerThreads(), "pulsar-external-listener"); producers = Maps.newIdentityHashMap(); consumers = Maps.newIdentityHashMap(); state.set(State.Open); }
/** * Calls http-lookup api to find broker-service address which can serve a given topic. * * @param destination: topic-name * @return broker-socket-address that serves given topic */ @SuppressWarnings("deprecation") public CompletableFuture<InetSocketAddress> getBroker(DestinationName destination) { return httpClient.get(BasePath + destination.getLookupName(), LookupData.class).thenCompose(lookupData -> { // Convert LookupData into as SocketAddress, handling exceptions URI uri = null; try { if (useTls) { uri = new URI(lookupData.getBrokerUrlTls()); } else { String serviceUrl = lookupData.getBrokerUrl(); if (serviceUrl == null) { serviceUrl = lookupData.getNativeUrl(); } uri = new URI(serviceUrl); } return CompletableFuture.completedFuture(new InetSocketAddress(uri.getHost(), uri.getPort())); } catch (Exception e) { // Failed to parse url log.warn("[{}] Lookup Failed due to invalid url {}, {}", destination, uri, e.getMessage()); return FutureUtil.failedFuture(e); } }); }
confBuilder.setConnectTimeout(connectTimeoutInSeconds * 1000); confBuilder.setReadTimeout(readTimeoutInSeconds * 1000); confBuilder.setUserAgent(String.format("Pulsar-Java-v%s", getPulsarClientVersion())); confBuilder.setKeepAliveStrategy(new DefaultKeepAliveStrategy() { @Override