| « | February 2026 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | |
| 公告 |
| 本博客在此声明所有文章均为转摘,只做资料收集使用。并无其他商业用途。 |
| Blog信息 |
|
blog名称: 日志总数:210 评论数量:205 留言数量:-19 访问次数:937808 建立时间:2007年5月10日 |

| |
|
[Mule]实战mule(例) 文章收藏, 网上资源, 软件技术, 电脑与网络
李小白 发表于 2007/9/12 20:43:26 |
| 首先到 http://mule.codehaus.org/ 网站下载mule解压安装到本地硬盘,例如E:\java\mule-1.3-rc2其次我们在这个目录下创建一个目录testmule用于测试然后我们在testmule下创建如下目录src :存放源文件classes:存放java class文件conf:存放xml配置文件bin:存放测试执行的批处理(*.bat)文件1、通用环境的搭建以为为监听程序服务的接口和实现Test.javapackage org.lyj.mule;public interface Test { public void echo(String name);}Service.javapackage org.lyj.mule;public class Service implements Test { public void echo(String name) { System.out.println("called="+name); }}从mule的sample下的任意一个例子的conf里把 log4j.properties 复制到 testmule/conf下,其中的内容如下#Mule default logging configuration#Logs to stdout and file. the log file is called mule.log and is stored in the directory#that the mule server is executed inlog4j.rootCategory=error, stdout, filelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%-5p %d [%t] %c: %m%n# Maintain alog file with a single file backuplog4j.appender.file=org.apache.log4j.RollingFileAppenderlog4j.appender.file.File=mule.loglog4j.appender.file.MaxFileSize=10MB# Keep one backup filelog4j.appender.file.MaxBackupIndex=1log4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%-5p %d [%t] %c: %m%n#You can set custom log levels per package herelog4j.logger.org.apache=WARN#Switch to info to see Test suite lifecycle infolog4j.logger.org.mule.tck=WARN2、File Provider的实现在conf下新建一个配置文件 test-file-config.xml,内容如下<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mule-configuration PUBLIC "-//SymphonySoft //DTD mule-configuration XML V1.0//EN" "http://www.symphonysoft.com/dtds/mule/mule-configuration.dtd"><mule-configuration id="file" version="1.0"> <description> 本测试由刘玉军完整测试,如果有问题请访问http://blog.csdn.net/lyj_china </description> <model name="test file"> <mule-descriptor name="Service1" implementation="org.lyj.mule.Service"> <inbound-router> <endpoint address="file:///D:/temp/"> <properties> <property name="pollingFrequency" value="30000"/> <property name="moveToDirectory" value="/D:/done"/> </properties> </endpoint> </inbound-router> </mule-descriptor> </model></mule-configuration>意思就是每隔30秒把文件从 d:/temp下移动到d:/done目录下在bin下面创建文件 testfile.bat,内容如下:@echo offREM There is no need to call this if you set the MULE_HOME in your environment propertiesif "%MULE_HOME%" == "" SET MULE_HOME=..\..REM Set your application specific classpath like thisSET CLASSPATH=%MULE_HOME%\testmule\conf;%MULE_HOME%\testmule\classes;call %MULE_HOME%\bin\mule.bat -config ../conf/test-file-config.xmlSET MULE_MAIN=SET CLASSPATH=然后我们运行testfile.bat 启动,正常启动mule后,就可以定时把 temp目录下的文件复制到done下了。并且在mule运行界面会打印出文件的内容,也就是说我们在mule里可以监控到整个文件流。
3、http provider首先把mule源码中的一个transform文件复制过来并编译到classes下面HttpRequestToString.java :package org.mule.samples.hello;import org.mule.config.i18n.Message;import org.mule.transformers.AbstractTransformer;import org.mule.umo.transformer.TransformerException;import java.io.UnsupportedEncodingException;/** * <code>NameStringToChatString</code> This is test class only for use with the Hello world * test application. * * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a> * @version $Revision: 1.6 $ */public class HttpRequestToString extends AbstractTransformer{ /** * */ public HttpRequestToString() { super(); this.registerSourceType(String.class); this.registerSourceType(byte[].class); } /* (non-Javadoc) * @see org.mule.transformers.AbstractTransformer#doTransform(java.lang.Object) */ public Object doTransform(Object src, String encoding) throws TransformerException { String param = null; if (src instanceof byte[]) { if (encoding != null) { try { param = new String((byte[]) src, encoding); } catch (UnsupportedEncodingException ex){ param = new String((byte[]) src); } } else { param = new String((byte[]) src); } } else { param = src.toString(); } int equals = param.indexOf("="); if(equals > -1) { return param.substring(equals + 1); } else { throw new TransformerException(Message.createStaticMessage("Failed to parse param string: " + param), this); } }}在conf下编写相应的配置文件 test-http-config.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mule-configuration PUBLIC "-//SymphonySoft //DTD mule-configuration XML V1.0//EN" "http://www.symphonysoft.com/dtds/mule/mule-configuration.dtd"><mule-configuration id="file" version="1.0"> <description> 本测试由刘玉军完整测试,如果有问题请访问http://blog.csdn.net/lyj_china </description> <mule-environment-properties synchronous="true" serverUrl=""/> <transformers> <transformer name="HttpRequestToString" className="org.mule.samples.hello.HttpRequestToString"/> </transformers> <model name="test http"> <mule-descriptor name="Service2" implementation="org.lyj.mule.Service"> <inbound-router> <endpoint address="http://localhost:4567" transformers="HttpRequestToString"> <properties> <property name="Content-Type" value="text/plain"/> </properties> </endpoint> </inbound-router><!-- <outbound-router> <router className="org.mule.routing.outbound.FilteringOutboundRouter"> <endpoint address="stream://System.out"> </endpoint> </router> </outbound-router>--> </mule-descriptor> </model></mule-configuration>在bin目录下创建testhttp.bat文件,内容如下@echo offREM There is no need to call this if you set the MULE_HOME in your environment propertiesif "%MULE_HOME%" == "" SET MULE_HOME=..\..REM Set your application specific classpath like thisSET CLASSPATH=%MULE_HOME%\testmule\conf;%MULE_HOME%\testmule\classes;call %MULE_HOME%\bin\mule.bat -config ../conf/test-http-config.xmlSET MULE_MAIN=SET CLASSPATH=然后执行testhttp.bat,正常启动mule后,就可以看到效果了通过 http://localhost:4567/name=success就可以看到页面上打印出success,mule后台打印出success;如果把<mule-environment-properties synchronous="true" serverUrl=""/>去掉,就会发现页面是空白的,仅在后台打印出信息.3、 jms provider因为首先需要一个jms服务器,所以首先我们需要搭建一个jms服务器,这里我们选用了openjms其使用参考 JMS服务器openJms入门在conf下创建 配置文件 test-jms-config.xml ,内容如下<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mule-configuration PUBLIC "-//SymphonySoft //DTD mule-configuration XML V1.0//EN" "http://www.symphonysoft.com/dtds/mule/mule-configuration.dtd"><!-- This is the configuration for the Loan Broker ESB example --><!-- See http://mule.codehaus.org/LoanBroker for more information, diagrams and even a presentation! --><mule-configuration id="Mule_JMS" version="1.0"> <description> Mule_JMS </description><connector name="jmsConnector" className="org.mule.providers.jms.JmsConnector"> <properties> <property name="specification" value="1.1"/> <property name="jndiDestinations" value="true"/> <property name="forceJndiDestinations" value="true"/> <property name="connectionFactoryJndiName" value="ConnectionFactory"/> <property name="jndiInitialFactory" value="org.exolab.jms.jndi.InitialContextFactory"/> <property name="jndiProviderUrl" value="tcp://localhost:3035"/> </properties> </connector><transformers> <transformer name="JMSMessageToString" className="org.mule.providers.jms.transformers.JMSMessageToObject" returnClass="java.lang.String" /> <transformer name="HttpRequestToString" className="org.mule.samples.hello.HttpRequestToString"/></transformers> <mule-descriptor name="jmsConnector" implementation="test.Service"> <inbound-router> <endpoint address="jms://queue1" transformers="JMSMessageToString"/> </inbound-router> </mule-descriptor></mule-configuration>在bin下创建批处理文件 testjms.bat ,其内容如下:@echo offREM There is no need to call this if you set the MULE_HOME in your environment propertiesif "%MULE_HOME%" == "" SET MULE_HOME=..\..REM Set your application specific classpath like thisSET CLASSPATH=%MULE_HOME%\samples\testmule\conf;%MULE_HOME%\samples\testmule\WebRoot\WEB-INF\classes;SET CUSTOM_LIB=E:\java\openjms-0.7.7-alpha-3\libcall %MULE_HOME%\bin\mule.bat -config ../conf/test-jms-config.xmlSET CLASSPATH=特别注意红字部分,需要指定openjms的lib地址然后我们启动openjms,再执行 testjms.bat ,然后用openjms的客户端测试工具发送消息,则能看到mule后台监测到的信息,会自动接收到mule里。其他的如 jdbc,mail,xxmp基本上都是这种思路来实现的,当然要注意transform的配置,因为很多信息需要转换才能正常显示接收的 |
|
|