Mule ESB Primer, the simplest Hello instance

Source: Internet
Author: User
Tags xmlns mule esb

This is one of the simplest examples of mule ESB entry, with Flow, endpoint, transformer,component these components, which are also the simplest to configure.

Scene:

1. Enter http://localhost:9090/hello-mule?name=oliver&age=20 in the browser address bar to return "Hello Oliver,your age is 10".

1. Enter HTTP://LOCALHOST:9090/LOVE-MULE?NAME=OLIVER&AGE=20 in the browser's address bar to return "Love Oliver,your-is 10".

Configuration file:[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   &NBSP;&NBSP;&NBSP;&NBsp;    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 ">                <!--  Convert HTTP data sent by clients to person objects  -->            <custom-transformer name= "Httpdatatopersontransformer"  class= "Com.mule.transformer.HttpDataToPersonTransformer"/>                 <!--  Encapsulate service operation results in HTML language  -->         <custom-transformer name= "Stringtohtmltransformer"  class= " Com.mule.transformer.StringToHtmlTransformer "/>                 <! --  set the corresponding content to text/html format  -->        < 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>                 <!--  Return the appropriate Hello statement based on what the client has passed  -->         <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>                 <!--  Eradicate content from the client, return the appropriate love statement  -->         <flow name= "Love" >            <inbound-endpoint address= "Http://localhost:9090/love-mule?method=sayLove"  exchange-pattern= " Request-response " >                <transformer ref= "Httpdatatopersontransformer"/>                <response>                    <transformer ref= "StringToHtmlTransformer" />    &nbsP;              <transformer ref = "Sethtmlcontenttype"/>                </response>           </inbound-endpoint>                        <component class= "Com.mule.component.WelcomService"/>                    </flow>      </ mule>  


Transformer:

1.HttpDataToperson convert the HTTP data passed by the client to the person type required by component[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  =  "Ten";           }                                return new person (Name,integer.parseint (age));        }     }   2.StringToHtmlTransformer will return the string type data after component processing, Encapsulate as HTML data

[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,&NBSP;STRING&NBSP;ENC)                 throws transformerexception {           string  str =  (String) src;                      return htmltemplateutil.getsimplehtml (str);       }     }   Component:

Welcomservice Back to welcome content[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.getna       Me () + ", your Age is" +person.getage (); } public String Saylove {return ' Love You ' +person.getname () + ", Your is" +per       Son.getage (); }} Person Class:[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 Tool Class: Marshaling a given text with an HTML statement[Java] View Plain copy print? package com.mule.util;      public class htmltemplateutil {              public static string getsimplehtml (  String text ) {           string output  =  "";                       output +=  "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.