Mule ESB 学习笔记(18)Mule和ATOM的整合

阅读更多

 

定时读取特定文件的rss文件:




    
       
       
       
    

 

 

定时读取特定网址的新rss信息:




    
        
            
        
        
    

 

 

jms将消息发送特定的组件




   
   
   
     
       
   
    
        

        
            
        
    

    
        
            
        
        
        
        
    


 

 

 

 

 

package com.easyway.esb.mule.rss;

import org.mule.api.annotations.expressions.Expr;
import org.mule.api.annotations.param.Payload;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;

public class EntryReceiver
{

    private AtomicInteger receivedEntries = new AtomicInteger(0);

    public void processEntry(@Payload Entry entry, @Expr("#[header:invocation:feed.object]") Feed feed) throws Exception
    {
        System.out.println("Received " + receivedEntries.incrementAndGet() + " of " + feed.getEntries().size() + " entries");
    }

    public int getCount()
    {
        return receivedEntries.get();
    }

    public AtomicInteger getReceivedEntries()
    {
        return receivedEntries;
    }
}

 

 

package com.easyway.esb.mule.rss;

import org.mule.api.annotations.param.Payload;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.abdera.model.Feed;

public class FeedReceiver
{

    private final AtomicInteger receivedEntries = new AtomicInteger(0);

    public void processFeed(@Payload Feed feed) throws Exception
    {
        receivedEntries.set(0);
        System.out.println("Received " + feed.getEntries().size() + " events");
        receivedEntries.set(feed.getEntries().size());
    }

    public int getCount()
    {
        return receivedEntries.get();
    }

    public AtomicInteger getReceivedEntries()
    {
        return receivedEntries;
    }
}

 

 

测试类:

import java.io.File;
import java.io.InputStream;
import java.io.StringReader;

import org.apache.commons.io.FileUtils;
import org.mule.api.MuleContext;
import org.mule.api.client.MuleClient;
import org.mule.api.context.MuleContextFactory;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextFactory;

public class MuleJmsAtomMain {
	public static void main(String[] args) {
     try {
	       String configFile = "jms-atom-consume.xml";
	        System.setProperty("mule.verbose.exceptions","true");
	        String[] configFileArr = new String[] {configFile };
	        MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
	        MuleContext muleContext = muleContextFactory
	                .createMuleContext(new SpringXmlConfigurationBuilder(configFileArr));
	        muleContext.start();
	        
		    MuleClient client = muleContext.getClient();
	        FeedReceiver component = (FeedReceiver)muleContext.getRegistry().get("feedConsumer");
	        
	        String path=MuleJmsAtomMain.class.getClassLoader().getResource("./sample-feed.atom").getFile();
	       
	        String feed=FileUtils.readFileToString(new File(path));
	        client.dispatch("jms://feed.in", feed, null);
	        Thread.sleep(2000);
	        System.out.println(component.getCount());
	        
	         client = muleContext.getClient();
	         component = (FeedReceiver)muleContext.getRegistry().get("feedConsumer");
	        component.getReceivedEntries().set(0); //reset since the build reports that it's getting incremented someplace else
	        client.dispatch("jms://feed.split.in", feed, null);
	        Thread.sleep(5000);                
	        System.out.println(component.getCount());
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}
	}

}

 

你可能感兴趣的:(mule,ESB,SOA,RSS,ATOM)