这是一个最简单的mule esb入门实例,其中用了 flow ,endpoint ,transformer,component 这些组件,这些组件的配置也是最简单配置.
情景:
1.在浏览器地址栏输入http://localhost:9090/hello-mule?name=oliver&age=20,会返回"Hello oliver,your age is 10".
1.在浏览器地址栏输入http://localhost:9090/love-mule?name=oliver&age=20,会返回"Love oliver,your age is 10".
配置文件:
[html] view plain copy print ?
- <?xml version="1.0" encoding="UTF-8"?>
- <mule xmlns="http://www.mulesoft.org/schema/mule/core"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:spring="http://www.springframework.org/schema/beans"
- xmlns:http="http://www.mulesoft.org/schema/mule/http"
- xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd
- http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/3.2/mule-http.xsd
- http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.2/mule-vm.xsd">
-
-
- <custom-transformer name="HttpDataToPersonTransformer" class="com.mule.transformer.HttpDataToPersonTransformer"/>
-
-
- <custom-transformer name="StringToHtmlTransformer" class="com.mule.transformer.StringToHtmlTransformer"/>
-
-
- <message-properties-transformer name="setHtmlContentType" overwrite="true">
- <add-message-property key="Content-Type" value="text/html" />
-
- <delete-message-property key="content-type" />
- </message-properties-transformer>
-
-
- <flow name="hello">
- <inbound-endpoint address="http://localhost:9090/hello-mule?method=sayHello" exchange-pattern="request-response" >
- <transformer ref="HttpDataToPersonTransformer"/>
- <response>
- <transformer ref="StringToHtmlTransformer"/>
- <transformer ref="setHtmlContentType"/>
- </response>
- </inbound-endpoint>
-
- <component class="com.mule.component.WelcomService"/>
-
- </flow>
-
-
- <flow name="love">
- <inbound-endpoint address="http://localhost:9090/love-mule?method=sayLove" exchange-pattern="request-response" >
- <transformer ref="HttpDataToPersonTransformer"/>
- <response>
- <transformer ref="StringToHtmlTransformer"/>
- <transformer ref="setHtmlContentType"/>
- </response>
- </inbound-endpoint>
-
- <component class="com.mule.component.WelcomService"/>
-
- </flow>
-
- </mule>
Transformer:
1.HttpDataToperson 将客户端传过来的http数据转换成component所需要Person类型
[html] view plain copy print ?
- package com.mule.transformer;
-
- import org.mule.api.MuleMessage;
- import org.mule.api.transformer.TransformerException;
- import org.mule.transformer.AbstractMessageTransformer;
-
- import com.mule.domain.Person;
-
- public class HttpDataToPersonTransformer extends AbstractMessageTransformer {
-
- @Override
- public Object transformMessage(MuleMessage message, String outputEncoding)
- throws TransformerException {
- String name = message.getInboundProperty("name");
- String age = message.getInboundProperty("age");
- if( age == null ) {
- age = "10";
- }
-
- return new Person(name,Integer.parseInt(age));
- }
-
- }
2.StringToHtmlTransformer 将component处理后返回的string类型数据,封装成html数据
[java] view plain copy print ?
- package com.mule.transformer;
-
- import org.mule.api.transformer.TransformerException;
- import org.mule.transformer.AbstractTransformer;
-
- import com.mule.util.HtmlTemplateUtil;
-
- public class StringToHtmlTransformer extends AbstractTransformer {
-
- @Override
- protected Object doTransform(Object src, String enc)
- throws TransformerException {
- String str = (String)src;
-
- return HtmlTemplateUtil.getSimpleHtml(str);
- }
-
- }
Component:
WelcomService 返回欢迎内容
[java] view plain copy print ?
- package com.mule.component;
-
- import com.mule.domain.Person;
-
- public class WelcomService {
-
- public String sayHello( Person person ){
- return " Hello "+person.getName()+", your age is "+person.getAge();
- }
-
- public String sayLove( Person person ){
- return " Love you "+person.getName()+", your age is "+person.getAge();
- }
-
- }
Person类 :
[java] view plain copy print ?
- package com.mule.domain;
-
- public class Person {
-
- private String name;
- private int age;
-
- public Person(){}
-
- public Person(String name,int age ){
- this.name = name;
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- }
[java] view plain copy print ?
- HtmlTemplateUtil 工具类:用html语句封转给定的text
[java] view plain copy print ?
- package com.mule.util;
-
- public class HtmlTemplateUtil {
-
- public static String getSimpleHtml( String text ){
- String output = "";
-
- output += "<html>";
- output += "<head>";
- output += "<meta http-equiv=\"Content-Language\" content=\"en-us\"/>";
- output += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\"/>";
- output += "<title>Bookstore Administration Console</title>";
- output += "</head>";
-
- output += "<body link=\"#FFFFFF\" vlink=\"#FFFFFF\" alink=\"#FFFFFF\" bgcolor=\"#990000\" text=\"#FFFFFF\">";
- output += text;
- output += "</body>";
-
- output += "<br/><a href=\"/bookstore-admin/\">Return to Home Page</a>";
- output += "</html>";
-
- return output;
- }
-
- }