这是一个最简单的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".
配置文件:
- <?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">
- <!-- 将客户端传过来的Http数据转化成Person对象 -->
- <custom-transformer name="HttpDataToPersonTransformer" class="com.mule.transformer.HttpDataToPersonTransformer"/>
- <!-- 将service操作结果用Html语言封装起来 -->
- <custom-transformer name="StringToHtmlTransformer" class="com.mule.transformer.StringToHtmlTransformer"/>
- <!-- 设置相应内容为text/html格式 -->
- <message-properties-transformer name="setHtmlContentType" overwrite="true">
- <add-message-property key="Content-Type" value="text/html" />
- <!-- Tomcat lowercases headers, need to drop this old one too -->
- <delete-message-property key="content-type" />
- </message-properties-transformer>
- <!-- 根据客户端传过来的内容,返回相应的Hello语句 -->
- <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>
- <!-- 根绝客户端传过来的内容,返回相应的Love语句 -->
- <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>
1.HttpDataToperson 将客户端传过来的http数据转换成component所需要Person类型
- 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));
- }
- }
- 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);
- }
- }
WelcomService 返回欢迎内容
- 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();
- }
- }
- 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;
- }
- }
- HtmlTemplateUtil 工具类:用html语句封转给定的text
- 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;
- }
- }