天天看点

MULE学习笔记1

版本:mule 2.2.1

操作:

1) 下载、解压

2)配置环境变量:mule_home、path

3)如果网络环境使用代理,找到%mule_home%/conf目录下,找到wrapper.conf,修改

#wrapper.java.additional.<n>=-dhttp.proxyhost=your_host 

#wrapper.java.additional.<n>=-dhttp.proxyport=your_port 

#wrapper.java.additional.<n>=-dhttp.proxyusername=your_user_name 

#wrapper.java.additional.<n>=-dhttp.proxypassword=your_password 

#wrapper.java.additional.<n>=-dhttp.proxyhost=your_host

#wrapper.java.additional.<n>=-dhttp.proxyport=your_port

#wrapper.java.additional.<n>=-dhttp.proxyusername=your_user_name

#wrapper.java.additional.<n>=-dhttp.proxypassword=your_password

根据需要修改,其中的<n>是按顺序修改,注意wrapper.conf全文中wrapper.java.additional.后面的序号写到第几,然后继续往下写。

打开cmd输入mule,同意他的声明,然后到example目录下找到echo目录,双击echo.bat,能运行就是安装成功了。

惯例,第一个mule程序

目标:类似example中的echo,从终端输入信息,返回“hello,[输入的信息]”.

两种方法:

这种方法比较简单。

1)打开eclipse,更新http://dist.muleforge.org/mule-ide/updates/3.4/,重启,mule ide就安装好了。

2)window-》preferences-》mule-》add-》找到你的mule目录-》apply-》ok

3)新建一个mule project

4)新建一个interface:

package demo.mule.umo; 

public interface helloworld { 

    public string sayhello(string str); 

package demo.mule.umo;

public interface helloworld {

public string sayhello(string str);

}

5)实现类:

package demo.mule.umo.impl; 

import demo.mule.umo.helloworld; 

public class helloworldimpl

implements helloworld { 

    @override 

    public string sayhello(string str) { 

        return "hello," + str; 

    } 

package demo.mule.umo.impl;

import demo.mule.umo.helloworld;

public class helloworldimpl implements helloworld {

@override

public string sayhello(string str) {

return "hello," + str;

6)配置config文件

在项目的conf目录中添加xxxx.xml(文件名依照实际),mule的配置文件和spring非常类似,所以配置起来非常方便,ide的提示功能也能很好的实现。由于需要重终端输入,因此,需要配置标准的使用stdio。如下配置:

<?xml version="1.0" encoding="utf-8"?> 

<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" 

       xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 

       xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" 

    xsi:schemalocation=" 

       http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd 

       http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd"> 

    <stdio:connector name="systemstreamconnector"     

        promptmessage="please enter yout name: " messagedelaytime="1000"/>  

    <model name="hellosample">       

        <service name="helloumo">     

            <inbound>     

                <stdio:inbound-endpoint system="in" />    

            </inbound>                

            <component class="demo.mule.umo.impl.helloworldimpl"/>     

            <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.2"

xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"

xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2"

xsi:schemalocation="

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd">

<stdio:connector name="systemstreamconnector"

promptmessage="please enter yout name: " messagedelaytime="1000"/>

<model name="hellosample">

<service name="helloumo">

<inbound>

<stdio:inbound-endpoint system="in" />

</inbound>

<component class="demo.mule.umo.impl.helloworldimpl"/>

<outbound>

<pass-through-router>

<stdio:outbound-endpoint system="out" />

</pass-through-router>

</outbound>

</service>

</model>

</mule>

7)运行,运行有两种方法:

(1)右击你的xxxx.xml-》run as-》mule server

(2)新建一个类,如下:

package demo.main; 

import org.mule.api.mulecontext; 

import org.mule.api.muleexception; 

import org.mule.api.config.configurationexception; 

import org.mule.api.context.mulecontextfactory; 

import org.mule.api.lifecycle.initialisationexception; 

import org.mule.config.spring.springxmlconfigurationbuilder; 

import org.mule.context.defaultmulecontextfactory; 

public class mulemain { 

    public static

void main(string[] args) throws configurationexception, 

            initialisationexception { 

        try { 

            string configfile = "sudio-config.xml"; 

            string[] configfilearr = new string[] { configfile }; 

            mulecontextfactory mulecontextfactory =

new defaultmulecontextfactory(); 

            mulecontext context = mulecontextfactory 

                    .createmulecontext(new springxmlconfigurationbuilder( 

                            configfilearr)); 

            context.start(); 

        } catch (muleexception t) { 

            t.printstacktrace(); 

        } 

package demo.main;

import org.mule.api.mulecontext;

import org.mule.api.muleexception;

import org.mule.api.config.configurationexception;

import org.mule.api.context.mulecontextfactory;

import org.mule.api.lifecycle.initialisationexception;

import org.mule.config.spring.springxmlconfigurationbuilder;

import org.mule.context.defaultmulecontextfactory;

public class mulemain {

public static void main(string[] args) throws configurationexception,

initialisationexception {

try {

string configfile = "sudio-config.xml";

string[] configfilearr = new string[] { configfile };

mulecontextfactory mulecontextfactory = new defaultmulecontextfactory();

mulecontext context = mulecontextfactory

.createmulecontext(new springxmlconfigurationbuilder(

configfilearr));

context.start();

} catch (muleexception t) {

t.printstacktrace();

不要急着运行,右击项目-》properties-》java build path-》source-》add folder-》把conf目录添加到classpath中-》ok

然后就可以运行文件了。

1)新建一个maven项目,在pom中加入以来,例如该项目中你需要使用stdio,那只要加入这个和spring-config的依赖就行了

如下配置:

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 

         xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

    <modelversion>4.0.0</modelversion> 

    <groupid>mule</groupid> 

    <artifactid>mule</artifactid> 

    <packaging>jar</packaging> 

    <version>1.0</version> 

    <properties> 

        <muleversion>2.2.1</muleversion> 

    </properties> 

    <dependencies> 

        <dependency> 

            <groupid>org.mule.modules</groupid> 

            <artifactid>mule-module-spring-config</artifactid> 

            <version>${muleversion}</version> 

        </dependency> 

            <groupid>org.mule.transports</groupid> 

            <artifactid>mule-transport-stdio</artifactid> 

    </dependencies> 

</project> 

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"

xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelversion>4.0.0</modelversion>

<groupid>mule</groupid>

<artifactid>mule</artifactid>

<packaging>jar</packaging>

<version>1.0</version>

<properties>

<muleversion>2.2.1</muleversion>

</properties>

<dependencies>

<dependency>

<groupid>org.mule.modules</groupid>

<artifactid>mule-module-spring-config</artifactid>

<version>${muleversion}</version>

</dependency>

<groupid>org.mule.transports</groupid>

<artifactid>mule-transport-stdio</artifactid>

</dependencies>

</project>

ok。其他和第一步一样。

至此,一个简单的mule应用就搞定了。

继续阅读