standalone模式

cd pulsar-1.14
bin/pulsar standalone

maven

        <dependency>
            <groupId>com.yahoo.pulsar</groupId>
            <artifactId>pulsar-client</artifactId>
            <version>1.14</version>
        </dependency>

producer

@Test
    public void producer() throws PulsarClientException {
        PulsarClient client = PulsarClient.create("http://localhost:8080");

        Producer producer = client.createProducer(
                "persistent://sample/standalone/ns1/my-topic");

// Publish 10 messages to the topic
        for (int i = 0; i < 10; i++) {
            producer.send("my-message".getBytes());
        }

        client.close();
    }

consumer

@Test
    public void consumer() throws PulsarClientException {
        PulsarClient client = PulsarClient.create("http://localhost:8080");

        Consumer consumer = client.subscribe(
                "persistent://sample/standalone/ns1/my-topic",
                "my-subscribtion-name");

        for(int i=0;i<100;i++) {
            // Wait for a message
            Message msg = consumer.receive();

            System.out.println("Received message: " + new String(msg.getData()));

            // Acknowledge the message so that it can be deleted by broker
            consumer.acknowledge(msg);
        }

        client.close();
    }

docs


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论