Friday, September 30, 2011

Mule Expression Transformers

Java Classes:

GameData: 

A simple POJO object.

package com.expressiontransformers;

public class GameData {

    private String gameName;
    private int year;

    public String getGameName() {
        return gameName;
    }

    public void setGameName(String gameName) {
        this.gameName = gameName;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

}

GameInfo:

Another simple POJO that contains the GameData Object.

package com.expressiontransformers;

public class GamingInfo {

    private int gameId;

    private GameData gameData;

    public int getGameId() {
        return gameId;
    }

    public void setGameId(int gameId) {
        this.gameId = gameId;
    }

    public GameData getGameData() {
        return gameData;
    }

    public void setGameData(GameData gameData) {
        this.gameData = gameData;
    }

}


Here we will be using two component classes. One is used to generate the GameInfo Object and the other is used to print the values.

CreateGamingInfoComponent:

package com.expressiontransformers;

public class CreateGamingInfoComponent {

    public GamingInfo create(String msg) {
        GamingInfo info = new GamingInfo();
        info.setGameId(1);
        
        GameData data = new GameData();
        data.setGameName("Cricket");
        data.setYear(2010);
        
        info.setGameData(data);
        return info;
    }
}


GamingComponent:

package com.expressiontransformers;

public class GamingComponent {

    public void addGame(Integer gameId, GameData data) {
        
        System.out.println(gameId);
        System.out.println(data.getGameName());
        System.out.println(data.getYear());
    }
}

Mule-config.xml:

The mule-config.xml uses the expression-transformer in the inbound-endpoint to convert the input GameInfo object to the required values of the GamingComponent.

<?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="RestaurantServiceE">
        <service name="ExpressionFilterService">
            <inbound>
                <stdio:inbound-endpoint system="IN"
                    connector-ref="stdioIN" />
            </inbound>
            <component
                class="com.expressiontransformers.CreateGamingInfoComponent" />
            <outbound>
                <pass-through-router>
                    <vm:outbound-endpoint path="displayGamingInfo-channel" />
                </pass-through-router>
            </outbound>
        </service>

        <service name="DisplayInfoService">
            <inbound>
                <vm:inbound-endpoint path="displayGamingInfo-channel">
                    <expression-transformer>
                        <return-argument expression="gameId"
                            evaluator="bean" />
                        <return-argument expression="gameData"
                            evaluator="bean" />
                    </expression-transformer>
                </vm:inbound-endpoint>
            </inbound>
            <component class="com.expressiontransformers.GamingComponent" />
        </service>
    </model>
</mule>

No comments:

Post a Comment