concept previous section in category apache pulsar

appears as: previous section
Apache Pulsar in Action

This is an excerpt from Manning's book Apache Pulsar in Action.

Figure 2.14: The data for a Pulsar topic is stored as a sequence of ledgers inside the BookKeeper layer. A list of these ledger IDs is stored inside a logical construct known as a managed ledger on the Zookeeper. Each ledger holds 50,000 entries that contain that store a replica of the physical data across multiple Bookies for redundancy.
A close up of a map Description automatically generated
Listing 4.11 Testing the KeywordFilterFunction with the LocalRunner
public class KeywordFilterFunctionLocalRunnerTest {
  final static String BROKER_URL = "pulsar://localhost:6650";
  final static String IN = "persistent://public/default/raw-feed"; 
  final static String OUT = "persistent://public/default/filtered-feed";
    
  private static ExecutorService executor;
  private static LocalRunner localRunner;
  private static PulsarClient client;
  private static Producer<String> producer;
  private static Consumer<String> consumer;
  private static String keyword = "";
    
  public static void main(String[] args) throws Exception {
    if (args.length > 0) {
    keyword = args[0];    #A
 }
 startLocalRunner();
 init();
 startConsumer();
 sendData();
 shutdown();
  }
 
  private static void startLocalRunner() throws Exception {
    localRunner = LocalRunner.builder()
            .brokerServiceUrl(BROKER_URL)    #B
            .functionConfig(getFunctionConfig())    #C
            .build();
 localRunner.start(false);    #D
  }
 
  private static FunctionConfig getFunctionConfig() {
    Map<String, ConsumerConfig> inputSpecs = 
       new HashMap<String, ConsumerConfig> ();
 
  inputSpecs.put(IN, ConsumerConfig.builder()    #E
         .schemaType(Schema.STRING.getSchemaInfo().getName())
         .build());
 
  Map<String, Object> userConfig = new HashMap<String, Object>();
  userConfig.put(KeywordFilterFunction.KEYWORD_CONFIG, keyword);
  userConfig.put(KeywordFilterFunction.IGNORE_CONFIG, true);    #F
        
  return FunctionConfig.builder()
          .className(KeywordFilterFunction.class.getName())    #G
          .inputs(Collections.singleton(IN))    #H
          .inputSpecs(inputSpecs)    #I
          .output(OUT)    #J
          .name("keyword-filter")
          .tenant("public")
          .namespace("default")
          .runtime(FunctionConfig.Runtime.JAVA)    #K
          .subName("keyword-filter-sub")
          .userConfig(userConfig)    #L
          .build();
  }
 
  private static void init() throws PulsarClientException {    #M
  executor = Executors.newFixedThreadPool(2);
  client = PulsarClient.builder()
            .serviceUrl(BROKER_URL)
            .build();
  producer = client.newProducer(Schema.STRING).topic(IN).create();    
  consumer = client.newConsumer(Schema.STRING).topic(OUT)
              .subscriptionName("validation-sub").subscribe();
  }
    
  private static void startConsumer() {    #N
  Runnable runnableTask = () -> {
    while (true) {
      Message<String> msg = null;
      try {
         msg = consumer.receive();
         System.out.printf("Message received: %s \n", msg.getValue());
         consumer.acknowledge(msg);
      } catch (Exception e) {
         consumer.negativeAcknowledge(msg);
      }
     }};
  executor.execute(runnableTask);
  }
    
  private static void sendData() throws IOException {    #O
    InputStream inputStream = Thread.currentThread().getContextClassLoader()
       .getResourceAsStream("test-data.txt");
 
 InputStreamReader streamReader = new InputStreamReader(inputStream,
    StandardCharsets.UTF_8);
 
    BufferedReader reader = new BufferedReader(streamReader);
    for (String line; (line = reader.readLine()) != null;) {
      producer.send(line);
    }
  }
        
  private static void shutdown() throws Exception {    #P
    executor.shutdown();
    localRunner.stop();
 . . .
  }   
}

#I Indicate to the source system that this record has failed to be processed.