Mule Atom Feed Example

0

Atom feed consists of a series of related items that a content provider publishes on the Internet. It comprises an XML-based format that uses the Atom Syndication Format to describe the feed and the items of information in it, and a protocol for publishing and editing Atom feeds.
Here is a sample:

<feed>
	<title>Java Articles</title>
	<subtitle>...</subtitle>
	<updated>2018-06-21T06:08:53Z</updated>
        ...
	<entry>
		<author>
			<name>Ram Satish</name>
		</author>
		<title>...</title>
		...
		<updated>2018-06-21T06:08:53Z</updated>
		<published>2018-06-21T06:07:57Z</published>
		<category../>
		<summary>...</summary>
	</entry>
	<entry>
		...
	</entry>
	...
</feed>

The items of information that make up an Atom feed are known as Atom entries. A content provider publishes, or “syndicates”, an Atom feed by making it available through a URL on the Internet and updating it with new items. For example: http://www.javarticles.com/feed/atom

Web feeds allow software programs to check for updates published on a website.

In the example, we will receive a feed over JMS. The feed received is then consumed in two ways.

  1. Feed is consumed as it is.
  2. Feed split into entries and each entry is then consumed.

muleContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:atom="http://www.mulesoft.org/schema/mule/atom"
      xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
      xmlns:test="http://www.mulesoft.org/schema/mule/test"
      xsi:schemaLocation="
               http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
               http://www.mulesoft.org/schema/mule/atom http://www.mulesoft.org/schema/mule/atom/current/mule-atom.xsd
               http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
               http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/current/mule-test.xsd">

    <jms:activemq-connector name="actveMq"/>

    <flow name="feedConsumer">
        <jms:inbound-endpoint queue="feed.in"/>
        <atom:object-to-feed-transformer/>
        <component class="com.javarticles.mule.FeedReceiver"/>
    </flow>

    <flow name="feedEntryConsumer">
        <jms:inbound-endpoint queue="feed.entry.in">
            <atom:feed-splitter/>
        </jms:inbound-endpoint>

        <component class="com.javarticles.mule.EntryReceiver"/>
    </flow>

</mule>

In order to access to the feed itself we parse it using the object-to-feed transformer which in turn transforms it into an instance of org.apache.abdera.model.Feed.

We use feed-splitter to split the entries of a feed into single entry objects.

FeedReceiver:

package com.javarticles.mule;

import org.apache.abdera.model.Feed;

public class FeedReceiver {
    public void processFeed(Feed feed) throws Exception {
        System.out.println("Received " + feed.getEntries().size() + " events");
    }
}

Use an atom feed splitter to split an incoming feed into individual messages so the component method will be invoked for each entry in the feed.

EntryReceiver:

package com.javarticles.mule;

import org.apache.abdera.model.Entry;

public class EntryReceiver {
    public void processEntry(Entry entry) throws Exception {
        System.out.println("Received " + entry);
    }
}

Here is the feed that we consume.

sampleFeed.atom:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0"
	xml:lang="en" xml:base="http://www.javarticles.com/wp-atom.php">
	<title type="text">Java Articles</title>
	<subtitle type="text">Beauty lies in simplifying things...</subtitle>

	<updated>2018-05-11T16:32:49Z</updated>

	<link rel="alternate" type="text/html" href="http://www.javarticles.com" />
	<id>http://www.javarticles.com/feed/atom</id>
	<link rel="self" type="application/atom+xml" href="http://www.javarticles.com/feed/atom" />

	<generator uri="https://wordpress.org/" version="4.9.5">WordPress
	</generator>
	<entry>
		<author>
			<name>Ram Satish</name>
		</author>
		<title type="html"><![CDATA[Mule message-properties-transformer example]]></title>
		<link rel="alternate" type="text/html"
			href="http://www.javarticles.com/2018/05/mule-message-properties-transformer-example.html" />
		<id>http://www.javarticles.com/?p=7004</id>
		<updated>2018-05-11T16:32:49Z</updated>
		<published>2018-05-11T16:26:12Z</published>
		<category scheme="http://www.javarticles.com" term="Mule" />
		<summary type="html"><![CDATA[In this article we will see how to explicitly add an outbound propertie. Before we start we must first understand the definition of a message property and how it fits in the overall message structure. A Mule message contains the following parts: Metadata – Scope based properties Payload – Main data Named attachments (inbound, outbound)- [...]]]></summary>
	</entry>
	<entry>
		<author>
			<name>Ram Satish</name>
		</author>
		<title type="html"><![CDATA[Mule composite source example]]></title>
		<link rel="alternate" type="text/html"
			href="http://www.javarticles.com/2018/05/mule-composite-source-example.html" />
		<id>http://www.javarticles.com/?p=7846</id>
		<updated>2018-05-10T09:27:50Z</updated>
		<published>2018-05-10T09:27:50Z</published>
		<category scheme="http://www.javarticles.com" term="Mule" />
		<summary type="html"><![CDATA[When a message source contains more than one inbound endpoint to feed messages we use <composite-source>. In our example, we have combined a vm with an http inbound-endpoint. Each endpoint in a composite message source will start a new flow execution as soon as it receives a message. Both vm and http endpoints use request-response exchange [...]]]></summary>
	</entry>
	<entry>
		<author>
			<name>Ram Satish</name>
		</author>
		<title type="html"><![CDATA[Mule Component Binding Example]]></title>
		<link rel="alternate" type="text/html"
			href="http://www.javarticles.com/2018/05/mule-component-binding-example.html" />
		<id>http://www.javarticles.com/?p=7855</id>
		<updated>2018-05-08T18:21:01Z</updated>
		<published>2018-05-08T18:19:21Z</published>
		<category scheme="http://www.javarticles.com" term="Mule" />
		<summary type="html"><![CDATA[In this article we will show how a mule component can be bound to an outbound endpoint thru an interface. The actual implementation of the interface would be some proxy class set using reflection. Here for simplicity sake, we bind to a vm outbound endpoint else it can be any outbound endpoint that gets the response [...]]]></summary>
	</entry>
</feed>

Same feed is sent over JMS to two different flows to show how we consume a feed as it is and the other one to show how we consume the individual entries.

MuleAtomFeedExample:

package com.javarticles.mule;

import org.mule.api.MuleContext;
import org.mule.api.client.LocalMuleClient;
import org.mule.api.context.MuleContextBuilder;
import org.mule.api.context.MuleContextFactory;
import org.mule.config.DefaultMuleConfiguration;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextBuilder;
import org.mule.context.DefaultMuleContextFactory;
import org.mule.util.IOUtils;

public class MuleAtomFeedExample {
    public static void main(String[] args) throws Exception {
        DefaultMuleConfiguration dmc = new DefaultMuleConfiguration();
        dmc.setId("muleexample");
        dmc.setWorkingDirectory("/esb/mule");
        SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(
                "muleContext.xml");
        MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
        contextBuilder.setMuleConfiguration(dmc);
        MuleContextFactory contextFactory = new DefaultMuleContextFactory();
        MuleContext ctx = contextFactory.createMuleContext(configBuilder,
                contextBuilder);
        ctx.start();
        try {
            LocalMuleClient muleClient = ctx.getClient();
            String feed = loadResourceAsString("sampleFeed.atom");
            muleClient.dispatch("jms://feed.in", feed, null);            
            Thread.sleep(3000);
            muleClient.dispatch("jms://feed.entry.in", feed, null);
        } finally {
            ctx.dispose();
        }
    }

    private static String loadResourceAsString(String resourceName) throws Exception {
        return IOUtils.getResourceAsString(resourceName, MuleAtomFeedExample.class);
    }
}

Output:

Received 3 events
Received <entry xmlns="http://www.w3.org/2005/Atom">
		<author>
			<name>Ram Satish</name>
					</author>
		<title type="html"><![CDATA[Mule composite source example]]></title>
		<link rel="alternate" type="text/html" href="http://www.javarticles.com/2018/05/mule-composite-source-example.html"/>
		<id>http://www.javarticles.com/?p=7846</id>
		<updated>2018-05-10T09:27:50Z</updated>
		<published>2018-05-10T09:27:50Z</published>
		<category scheme="http://www.javarticles.com" term="Mule"/>		<summary type="html"><![CDATA[When a message source contains more than one inbound endpoint to feed messages we use <composite-source>. In our example, we have combined a vm with an http inbound-endpoint. Each endpoint in a composite message source will start a new flow execution as soon as it receives a message. Both vm and http endpoints use request-response exchange [...]]]></summary>
		</entry>
Received <entry xmlns="http://www.w3.org/2005/Atom">
		<author>
			<name>Ram Satish</name>
					</author>
		<title type="html"><![CDATA[Mule Component Binding Example]]></title>
		<link rel="alternate" type="text/html" href="http://www.javarticles.com/2018/05/mule-component-binding-example.html"/>
		<id>http://www.javarticles.com/?p=7855</id>
		<updated>2018-05-08T18:21:01Z</updated>
		<published>2018-05-08T18:19:21Z</published>
		<category scheme="http://www.javarticles.com" term="Mule"/>		<summary type="html"><![CDATA[In this article we will show how a mule component can be bound to an outbound endpoint thru an interface. The actual implementation of the interface would be some proxy class set using reflection. Here for simplicity sake, we bind to a vm outbound endpoint else it can be any outbound endpoint that gets the response [...]]]></summary>
		</entry>
Received <entry xmlns="http://www.w3.org/2005/Atom">
		<author>
			<name>Ram Satish</name>
					</author>
		<title type="html"><![CDATA[Mule message-properties-transformer example]]></title>
		<link rel="alternate" type="text/html" href="http://www.javarticles.com/2018/05/mule-message-properties-transformer-example.html"/>
		<id>http://www.javarticles.com/?p=7004</id>
		<updated>2018-05-11T16:32:49Z</updated>
		<published>2018-05-11T16:26:12Z</published>
		<category scheme="http://www.javarticles.com" term="Mule"/>		<summary type="html"><![CDATA[In this article we will see how to explicitly add an outbound propertie. Before we start we must first understand the definition of a message property and how it fits in the overall message structure. A Mule message contains the following parts: Metadata – Scope based properties Payload – Main data Named attachments (inbound, outbound)- [...]]]></summary>
		</entry>

Download the source code

This was an example about Mule Atom Feed.

You can download the example here: muleAtomFeed.zip

Share.

Comments are closed.