现在的位置: 首页 > 综合 > 正文

开发第一个Mule项目——Hello World

2014年12月10日 ⁄ 综合 ⁄ 共 7476字 ⁄ 字号 评论关闭

3. 开发第一个Mule项目——Hello World

3.1 创建工程

- 创建一个Mule Project

- IDE会自动会把以下目录中所有的JAR包复制到lib文件夹中

-- %Mule_Home%/lib/boot

-- %Mule_Home%/lib/mule

-- %Mule_Home%/lib/opt

并将这些JAR包添加到项目的classpath

 

3.2 编写Java文件

3.2.1 创建接口

Java代码

  1. /***********************************************************************  
  2.  * <p>Project Name: test</p>  
  3.  * <p>File Name: com.thu.afa.mule.demo.Hello.java</p>  
  4.  * <p>Copyright: Copyright (c) 2010</p>  
  5.  * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>  
  6.  ***********************************************************************/  
  7. package com.thu.afa.mule.demo;   
  8.   
  9. /**  
  10.  * <p>Class Name: Hello</p>  
  11.  * <p>Description: </p>  
  12.  * @author Afa  
  13.  * @date Jun 26, 2010  
  14.  * @version 1.0  
  15.  */  
  16. public interface Hello   
  17. {   
  18.     public String hello(String name);   
  19. }  

/***********************************************************************

 * <p>Project Name: test</p>

 * <p>File Name: com.thu.afa.mule.demo.Hello.java</p>

 * <p>Copyright: Copyright (c) 2010</p>

 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>

 ***********************************************************************/

package com.thu.afa.mule.demo;

 

/**

 * <p>Class Name: Hello</p>

 * <p>Description: </p>

 * @author Afa

 * @date Jun 26, 2010

 * @version 1.0

 */

public interface Hello

{

         public String hello(String name);

}

3.2.2 创建实现类

Java代码

  1. /***********************************************************************  
  2.  * <p>Project Name: test</p>  
  3.  * <p>File Name: com.thu.afa.mule.demo.HelloImpl.java</p>  
  4.  * <p>Copyright: Copyright (c) 2010</p>  
  5.  * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>  
  6.  ***********************************************************************/  
  7. package com.thu.afa.mule.demo;   
  8.   
  9. /**  
  10.  * <p>Class Name: HelloImpl</p>  
  11.  * <p>Description: </p>  
  12.  * @author Afa  
  13.  * @date Jun 26, 2010  
  14.  * @version 1.0  
  15.  */  
  16. public class HelloImpl implements Hello   
  17. {   
  18.     /* (non-Javadoc)  
  19.      * <p>Title: </p>  
  20.      * <p>Method Name: hello</p>  
  21.      * <p>Description: </p>  
  22.      * @author: Afa  
  23.      * @date: Jun 26, 2010  
  24.      * @see com.thu.afa.mule.demo.Hello#hello(java.lang.String)  
  25.      *  
  26.      * @param name  
  27.      * @return  
  28.      */  
  29.     public String hello(String name)   
  30.     {   
  31.         return name;   
  32.     }   
  33.   
  34. }  
/***********************************************************************
 * <p>Project Name: test</p>
 * <p>File Name: com.thu.afa.mule.demo.HelloImpl.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.mule.demo;
 
/**
 * <p>Class Name: HelloImpl</p>
 * <p>Description: </p>
 * @author Afa
 * @date Jun 26, 2010
 * @version 1.0
 */
public class HelloImpl implements Hello
{
 /* (non-Javadoc)
  * <p>Title: </p>
  * <p>Method Name: hello</p>
  * <p>Description: </p>
  * @author: Afa
  * @date: Jun 26, 2010
  * @see com.thu.afa.mule.demo.Hello#hello(java.lang.String)
  *
  * @param name
  * @return
  */
 public String hello(String name)
 {
  return name;
 }
 
}

 

3.3 编写配置文件

在项目目录下创建conf文件夹,并创建文件:hello-config.xml。当然,你也可以从mule的例子中复制一个配置文件到此,然后做适当的修改。下面的本例子的配置:

Xml代码

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <mule xmlns="http://www.mulesource.org/schema/mule/core/2.1"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:spring="http://www.springframework.org/schema/beans"  
  5.        xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.1"  
  6.        xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.1"  
  7.        xsi:schemaLocation="   
  8.                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  9.                http://www.mulesource.org/schema/mule/core/2.1 http://www.mulesource.org/schema/mule/core/2.1/mule.xsd   
  10.                http://www.mulesource.org/schema/mule/stdio/2.1 http://www.mulesource.org/schema/mule/stdio/2.1/mule-stdio.xsd   
  11.                http://www.mulesource.org/schema/mule/vm/2.1 http://www.mulesource.org/schema/mule/vm/2.1/mule-vm.xsd">  
  12.        
  13.     <!--   
  14.         The system stream connector is used to send and receive information via the   
  15.         System.in and System.out. Note this connector is only really useful for testing   
  16.         purposes.   
  17.         promptMessage - is what is written to the console   
  18.         messageDelayTime - is the time in milliseconds before the user is prompted again.   
  19.         These properties are set as bean properties on the connector.   
  20.     -->  
  21.     <stdio:connector name="SystemStreamConnector"  
  22.         promptMessage="Please enter yout name: "  
  23.         messageDelayTime="1000"/>  
  24.     <!--  
  25.         The Mule model initialises and manages your UMO components  
  26.     -->  
  27.     <model name="HelloSample">  
  28.         <!--   
  29.             A Mule service defines all the necessary information about how your components will   
  30.             interact with the framework, other components in the system and external sources.   
  31.             Please refer to the Configuration Guide for a full description of all the parameters.   
  32.         -->  
  33.         <service name="HelloUMO">  
  34.             <inbound>  
  35.                 <stdio:inbound-endpoint system="IN" />  
  36.             </inbound>  
  37.                
  38.             <component class="com.thu.afa.mule.demo.HelloImpl"/>  
  39.   
  40.             <outbound>  
  41.                 <pass-through-router>  
  42.                     <stdio:outbound-endpoint system="OUT" />  
  43.                 </pass-through-router>  
  44.             </outbound>  
  45.         </service>        
  46.     </model>  
  47. </mule>  
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.1"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:spring="http://www.springframework.org/schema/beans"
       xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.1"
       xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.1"
       xsi:schemaLocation="
               http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.mulesource.org/schema/mule/core/2.1 http://www.mulesource.org/schema/mule/core/2.1/mule.xsd
               http://www.mulesource.org/schema/mule/stdio/2.1 http://www.mulesource.org/schema/mule/stdio/2.1/mule-stdio.xsd
               http://www.mulesource.org/schema/mule/vm/2.1 http://www.mulesource.org/schema/mule/vm/2.1/mule-vm.xsd">
    
    <!--
        The system stream connector is used to send and receive information via the
        System.in and System.out. Note this connector is only really useful for testing
        purposes.
        promptMessage - is what is written to the console
        messageDelayTime - is the time in milliseconds before the user is prompted again.
        These properties are set as bean properties on the connector.
    -->
    <stdio:connector name="SystemStreamConnector"
        promptMessage="Please enter yout name: "
        messageDelayTime="1000"/>
    <!--
        The Mule model initialises and manages your UMO components
    -->
    <model name="HelloSample">
        <!--
            A Mule service defines all the necessary information about how your components will
            interact with the framework, other components in the system and external sources.
            Please refer to the Configuration Guide for a full description of all the parameters.
        -->
        <service name="HelloUMO">
            <inbound>
                <stdio:inbound-endpoint system="IN" />
            </inbound>
            
            <component class="com.thu.afa.mule.demo.HelloImpl"/>
 
            <outbound>
                <pass-through-router>
                 <stdio:outbound-endpoint system="OUT" />
                </pass-through-router>
            </outbound>
        </service>     
    </model>
</mule>

  

3.5 运行

右键点击hello-config.xmlRun As -> Mule Server,即可运行,运行结果如下:

抱歉!评论已关闭.