Monday, October 3, 2011

Mule - Expression Splitter Router

The following example is used to simulate the functionality of the expression-splitter-router in mule.

Java Classes:

Fruit:

package com.expressionsplitterrouter;

public abstract class Fruit {

    private String shape;

    public String getShape() {
        return shape;
    }

    public void setShape(String shape) {
        this.shape = shape;
    }

} 


Apple:

package com.expressionsplitterrouter;

public class Apple extends Fruit {

    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}


Banana:

package com.expressionsplitterrouter;

public class Banana extends Fruit {

    private String form;

    public String getForm() {
        return form;
    }

    public void setForm(String form) {
        this.form = form;
    }

}


FruitBowl:

package com.expressionsplitterrouter;

import java.util.ArrayList;
import java.util.List;

public class FruitBowl {

    private List<Fruit> fruit;

    public List<Fruit> getFruit() {
        return fruit;
    }

    public void setFruit(List<Fruit> fruit) {
        this.fruit = fruit;
    }
    
    public void addFruit(Fruit fruit){
        if (this.fruit == null) {
            this.fruit = new ArrayList<Fruit>();
        }
        this.fruit.add(fruit);
    }

} 


CreateFruitComponent:

package com.expressionsplitterrouter;

public class CreateFruitComponent {

    public FruitBowl createFruit(String start) {
        
        FruitBowl bowl = new FruitBowl();
        
        Apple apple = new Apple();
        apple.setShape("Round");
        apple.setType("Green");
        
        Banana banana = new Banana();
        banana.setShape("Lengthy");
        banana.setForm("Yellow");
        
        bowl.addFruit(apple);
        bowl.addFruit(banana);
        
        apple = new Apple();
        apple.setShape("Rectangular Square");
        apple.setType("Red");
        
        bowl.addFruit(apple);
        return bowl;
    }
} 


AppleComponent:

package com.expressionsplitterrouter;

public class AppleComponent {

    public void displayApple(Apple apple) {

        System.out.println("Apple Component");
        System.out.println(apple.getShape() + "," + apple.getType());
    }
} 


BananaComponent:

package com.expressionsplitterrouter;

public class BananaComponent {

    public void displayBanana(Banana banana) {

        System.out.println("BananaComponent");
        System.out.println(banana.getShape() + "," + banana.getForm());
    }
} 


Mule-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2"
    xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2"
    xsi:schemaLocation="
          http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
          http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd
          http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd">

    <stdio:connector name="stdioIN" promptMessage="Press any key to continue" />

    <model name="ExpressionSplitterRouterModel">
        <service name="CreateMsgService">
            <inbound>
                <stdio:inbound-endpoint
                    connector-ref="stdioIN" system="IN" />
            </inbound>
            <component
                class="com.expressionsplitterrouter.CreateFruitComponent" />
            <outbound>
                <expression-splitter-router
                    expression="fruit" evaluator="bean">
                    <vm:outbound-endpoint path="banana-channel">
                        <payload-type-filter
                            expectedType="com.expressionsplitterrouter.Banana" />
                    </vm:outbound-endpoint>
                    <vm:outbound-endpoint path="apple-channel">
                        <payload-type-filter
                            expectedType="com.expressionsplitterrouter.Apple" />
                    </vm:outbound-endpoint>
                </expression-splitter-router>
            </outbound>
        </service>
        
        <service name="BananaService">
            <inbound>
                <vm:inbound-endpoint path="banana-channel"/>
            </inbound>
            <component class="com.expressionsplitterrouter.BananaComponent"/>
        </service>
        
        <service name="AppleService">
            <inbound>
                <vm:inbound-endpoint path="apple-channel"/>
            </inbound>
            <component class="com.expressionsplitterrouter.AppleComponent"/>
        </service>
    </model>
</mule> 

No comments:

Post a Comment