Finding specific ATOM categories using Atom Nuke and Java

Post to Twitter

This is the third article I’ve written now on the new Atom Nuke framework. Today I’m going to go over turning ATOM feeds and entries into selectable events. This is built into Nuke via the EPS, or Event Processing System. I’m going to piggyback off of a couple examples already provided (here and here).

Note: I’ll be using Netbeans 7.2 and Java 1.6 along with the latest build of Atom Nuke which is currently 0.9.2.

To select only categories with specific values I’ll be using the Category Selector.

With NetBeans create a new Maven Java Application called: EPSSelector

Note: Make sure to add the dependency for Nuke.

Modify the App.java file to look like the following:

package com.giantflyingsaucer.epsselector;

import java.util.concurrent.TimeUnit;
import org.atomnuke.Nuke;
import org.atomnuke.NukeKernel;
import org.atomnuke.atom.model.Category;
import org.atomnuke.atom.model.Entry;
import org.atomnuke.listener.eps.Relay;
import org.atomnuke.listener.eps.eventlet.AtomEventletException;
import org.atomnuke.listener.eps.eventlet.AtomEventletPartial;
import org.atomnuke.listener.eps.selectors.CategorySelector;
import org.atomnuke.source.crawler.FeedCrawlerSourceFactory;
import org.atomnuke.task.Task;
import org.atomnuke.util.TimeValue;

public class App {

   public static void main(String[] args) throws Exception {
      final Relay relay = new Relay();

      relay.enlistHandler(new AtomEventletPartial() {

         // Only capture ATOM entries with a category containing 'MyCategory2'
         @Override
         public void entry(Entry entry) throws AtomEventletException {
            System.out.println("Entry Title: " + entry.title().toString());

            for(Category category : entry.categories()) {
                if(category.term() != null) {
                    System.out.println("Category Term: " + category.term());
                }
                if(category.label() != null) {
                    System.out.println("Category Label: " + category.label());
                }
            }
         }
      }, new CategorySelector(new String[]{"MyCategory2"}));

      final Nuke nukeKernel = new NukeKernel();
      final FeedCrawlerSourceFactory feedCrawlerFactory = new FeedCrawlerSourceFactory();
      // Hit the feed for updates every 4 seconds
      final Task crawlerTask = nukeKernel.follow(
              feedCrawlerFactory.newCrawlerSource("myCrawler", "http://localhost:8080/namespace/feed/"),
              new TimeValue(4, TimeUnit.SECONDS));
      crawlerTask.addListener(relay);

      nukeKernel.start();
      // Stop after 10 seconds
      Thread.sleep(10000);

      nukeKernel.destroy();
   }
}

At this point I’ll use Atom Hopper and run it locally. If your not familiar with Atom Hopper you can read my articles/tutorials on it as well as the up to date wiki.

I’m going to populate Atom Hopper with the following ATOM entries:

Entry #1:

<entry xmlns="http://www.w3.org/2005/Atom">
  <title type="text">This is entry 1</title>
  <author>
    <name>Chad</name>
  </author>
  <content type="text">This is entry 1 with one category</content>
  <category term="MyCategory1" />
</entry>

Entry #2:

<entry xmlns="http://www.w3.org/2005/Atom">
  <title type="text">This is entry 2</title>
  <author>
    <name>Chad</name>
  </author>
  <content type="text">This is entry 2 with two categories</content>
  <category term="MyCategory1" />
  <category term="MyCategory2" />
</entry>

Entry #3:

<entry xmlns="http://www.w3.org/2005/Atom">
  <title type="text">This is entry 3</title>
  <author>
    <name>Chad</name>
  </author>
  <content type="text">This is entry 3 with three categories</content>
  <category term="MyCategory1" />
  <category term="MyCategory2" />
  <category term="MyCategory3" />
</entry>

Because I’m only selecting entries with the criteria MyCategory2 I should only get two responses back. So when I run the program I should see these results:

Entry Title: This is entry 3
Category Term: MyCategory1
Category Term: MyCategory2
Category Term: MyCategory3

Entry Title: This is entry 2
Category Term: MyCategory1
Category Term: MyCategory2

That is what I expected since only entry #2 and #3 contain MyCategory2. This is very simple example however it shows you how easily you can use selectors to filter the entries you want when consuming an ATOM feed.

Post to Twitter

This entry was posted in Atom Hopper, AtomNuke, Java, Open Source. Bookmark the permalink.