Search in sources :

Example 6 with DiscoveryService

use of org.apache.pulsar.discovery.service.DiscoveryService in project incubator-pulsar by apache.

the class DiscoveryServiceStarter method init.

public static void init(String configFile) throws Exception {
    // setup handlers
    removeHandlersForRootLogger();
    install();
    setDefaultUncaughtExceptionHandler((thread, exception) -> {
        log.error("Uncaught exception in thread {}: {}", thread.getName(), exception.getMessage(), exception);
    });
    // load config file
    final ServiceConfig config = PulsarConfigurationLoader.create(configFile, ServiceConfig.class);
    checkArgument(!isEmpty(config.getZookeeperServers()), "zookeeperServers must be provided");
    checkArgument(!isEmpty(config.getGlobalZookeeperServers()), "global-zookeeperServers must be provided");
    // create Discovery service
    DiscoveryService discoveryService = new DiscoveryService(config);
    // create a web-service
    final ServerManager server = new ServerManager(config);
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                discoveryService.close();
                server.stop();
            } catch (Exception e) {
                log.warn("server couldn't stop gracefully {}", e.getMessage(), e);
            }
        }
    });
    discoveryService.start();
    startWebService(server, config);
}
Also used : DiscoveryService(org.apache.pulsar.discovery.service.DiscoveryService)

Example 7 with DiscoveryService

use of org.apache.pulsar.discovery.service.DiscoveryService in project incubator-pulsar by apache.

the class BrokerServiceLookupTest method testDiscoveryLookup.

/**
 * Discovery-Service lookup over binary-protocol 1. Start discovery service 2. start broker 3. Create
 * Producer/Consumer: by calling Discovery service for partitionedMetadata and topic lookup
 *
 * @throws Exception
 */
@Test
public void testDiscoveryLookup() throws Exception {
    // (1) start discovery service
    ServiceConfig config = new ServiceConfig();
    config.setServicePort(nextFreePort());
    config.setBindOnLocalhost(true);
    DiscoveryService discoveryService = spy(new DiscoveryService(config));
    doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
    discoveryService.start();
    // (2) lookup using discovery service
    final String discoverySvcUrl = discoveryService.getServiceUrl();
    PulsarClient pulsarClient2 = PulsarClient.builder().serviceUrl(discoverySvcUrl).build();
    Consumer<byte[]> consumer = pulsarClient2.newConsumer().topic("persistent://my-property2/use2/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = pulsarClient2.newProducer().topic("persistent://my-property2/use2/my-ns/my-topic1").create();
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Message<byte[]> msg = null;
    Set<String> messageSet = Sets.newHashSet();
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
    }
    // Acknowledge the consumption of all messages at once
    consumer.acknowledgeCumulative(msg);
    consumer.close();
    producer.close();
}
Also used : ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) DiscoveryService(org.apache.pulsar.discovery.service.DiscoveryService) Test(org.testng.annotations.Test)

Aggregations

DiscoveryService (org.apache.pulsar.discovery.service.DiscoveryService)7 ServiceConfig (org.apache.pulsar.discovery.service.server.ServiceConfig)6 Test (org.testng.annotations.Test)5 HashMap (java.util.HashMap)4 Map (java.util.Map)3 Cleanup (lombok.Cleanup)2 AuthenticationTls (org.apache.pulsar.client.impl.auth.AuthenticationTls)1