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 创建接口
- /***********************************************************************
- * <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);
- }
/***********************************************************************
* <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 创建实现类
- /***********************************************************************
- * <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;
- }
- }
/***********************************************************************
* <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 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>
<?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.xml,Run As -> Mule Server,即可运行,运行结果如下: