Pulsar plugin development

You can develop various plugins for Pulsar, such as entry filters, protocol handlers, interceptors, and so on.

Entry filter

This chapter describes what the entry filter is and shows how to use the entry filter.

What is an entry filter?

The entry filter is an extension point for implementing a custom message entry strategy. With an entry filter, you can decide whether to send messages to consumers (brokers can use the return values of entry filters to determine whether the messages need to be sent or discarded) or send messages to specific consumers.

To implement features such as tagged messages or custom delayed messages, use subscriptionProperties, ​​properties, and entry filters.

How to use an entry filter?

Follow the steps below:

  1. Create a Maven project.

  2. Implement the EntryFilter interface.

  3. Package the implementation class into a NAR file.

  4. Configure the broker.conf file (or the standalone.conf file) and restart your broker.

Step 1: Create a Maven project

For how to create a Maven project, see here.

Step 2: Implement the EntryFilter interface

  1. Add a dependency for Pulsar broker in the pom.xml file to display. Otherwise, you can not find the EntryFilter interface.

    1. <dependency>
    2. <groupId>org.apache.pulsar</groupId>
    3. <artifactId>pulsar-broker</artifactId>
    4. <version>${pulsar.version}</version>
    5. <scope>provided</scope>
    6. </dependency>
  1. Implement the FilterResult filterEntry(Entry entry, FilterContext context); method.

    • If the method returns ACCEPT or NULL, this message is sent to consumers.

    • If the method returns REJECT, this message is filtered out and it does not consume message permits.

    • If there are multiple entry filters, this message passes through all filters in the pipeline in a round-robin manner. If any entry filter returns REJECT, this message is discarded.

    You can get entry metadata, subscriptions, and other information through FilterContext.

  2. Describe a NAR file.

    Create an entry_filter.yml file in the resources/META-INF/services directory to describe a NAR file.

    1. # Entry filter name, which should be configured in the broker.conf file later
    2. name: entryFilter
    3. # Entry filter description
    4. description: entry filter
    5. # Implementation class name of entry filter
    6. entryFilterClass: com.xxxx.xxxx.xxxx.DefaultEntryFilterImpl

Step 3: package implementation class of entry filter into a NAR file

  1. Add the compiled plugin of the NAR file to your pom.xml file.

    1. <build>
    2. <finalName>${project.artifactId}</finalName>
    3. <plugins>
    4. <plugin>
    5. <groupId>org.apache.nifi</groupId>
    6. <artifactId>nifi-nar-maven-plugin</artifactId>
    7. <version>1.2.0</version>
    8. <extensions>true</extensions>
    9. <configuration>
    10. <finalName>${project.artifactId}-${project.version}</finalName>
    11. </configuration>
    12. <executions>
    13. <execution>
    14. <id>default-nar</id>
    15. <phase>package</phase>
    16. <goals>
    17. <goal>nar</goal>
    18. </goals>
    19. </execution>
    20. </executions>
    21. </plugin>
    22. </plugins>
    23. </build>
  1. Generate a NAR file in the target directory.

    1. mvn clean install

Step 4: configure and restart broker

  1. Configure the following parameters in the broker.conf file (or the standalone.conf file).

    1. # Class name of pluggable entry filters
    2. # Multiple classes need to be separated by commas.
    3. entryFilterNames=entryFilter1,entryFilter2,entryFilter3
    4. # The directory for all entry filter implementations
    5. entryFiltersDirectory=tempDir
  1. Restart your broker.

    You can see the following broker log if the plug-in is successfully loaded.

    1. Successfully loaded entry filter for name `{name of your entry filter}`