Mule的第一个应用非常简单。却也可以看出Mule的运行机制。上例中,实现的是Hello world 的例子,过程为:用户输入内容,回车,然后显示Hello,[输入的内容]。这个实现非常简单,但是,却也存在一些问题,mule是怎么知道显示这些内容的,现在就研究这个。
也许大家觉得,在XXXX.xml中已经配置了类路径,mule当然可以找到这个类,问题就在这里,mule找到了实现类,那他是如何找到对应的方法的呢?OK,做一下尝试
将inteface修改为:
package demo.mule.umo; public interface HelloWorld { public String sayHello(String str); public String sayHello(); }
实现类修改为:
package demo.mule.umo.impl; import demo.mule.umo.HelloWorld; public class HelloWorldImpl implements HelloWorld { @Override public String sayHello(String str) { return "Hello," + str; } @Override public String sayHello() { return "Hello world"; } }
运行项目:运行成功!
修改interface为:
package demo.mule.umo; public interface HelloWorld { public String sayHello(String str); public String sayHello2(String 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; } @Override public String sayHello2(String str) { return "Hello2 ," + str; } }
运行:抛出ERROR
ERROR 2011-02-09 17:04:41,874 [HelloUMO.2] org.mule.service.DefaultServiceExceptionStrategy: ******************************************************************************** Message : [ The required property "method" is not set on the event CallableEntryPointResolver:Object "demo.mule.umo.impl.HelloWorldImpl@12f1bf0" does not implement required interface "interface org.mule.api.lifecycle.Callable" Found too many possible methods on object "demo.mule.umo.impl.HelloWorldImpl" that accept parameters "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=true, acceptVoidMethods=false}" Found too many possible methods on object "demo.mule.umo.impl.HelloWorldImpl" that accept parameters "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=false, acceptVoidMethods=false}" ] Type : org.mule.model.resolvers.EntryPointNotFoundException Code : MULE_ERROR--2 JavaDoc : http://www.mulesource.org/docs/site/current2/apidocs/org/mule/model/resolvers/EntryPointNotFoundException.html ******************************************************************************** Exception stack is: 1. [ The required property "method" is not set on the event CallableEntryPointResolver:Object "demo.mule.umo.impl.HelloWorldImpl@12f1bf0" does not implement required interface "interface org.mule.api.lifecycle.Callable" Found too many possible methods on object "demo.mule.umo.impl.HelloWorldImpl" that accept parameters "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=true, acceptVoidMethods=false}" Found too many possible methods on object "demo.mule.umo.impl.HelloWorldImpl" that accept parameters "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=false, acceptVoidMethods=false}" ] (org.mule.model.resolvers.EntryPointNotFoundException) org.mule.model.resolvers.DefaultEntryPointResolverSet:63 (http://www.mulesource.org/docs/site/current2/apidocs/org/mule/model/resolvers/EntryPointNotFoundException.html) ******************************************************************************** Root Exception stack trace: org.mule.model.resolvers.EntryPointNotFoundException: [ The required property "method" is not set on the event CallableEntryPointResolver:Object "demo.mule.umo.impl.HelloWorldImpl@12f1bf0" does not implement required interface "interface org.mule.api.lifecycle.Callable" Found too many possible methods on object "demo.mule.umo.impl.HelloWorldImpl" that accept parameters "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=true, acceptVoidMethods=false}" Found too many possible methods on object "demo.mule.umo.impl.HelloWorldImpl" that accept parameters "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=false, acceptVoidMethods=false}" ] at org.mule.model.resolvers.DefaultEntryPointResolverSet.invoke(DefaultEntryPointResolverSet.java:63) at org.mule.component.DefaultLifecycleAdapter.invoke(DefaultLifecycleAdapter.java:205) at org.mule.component.AbstractJavaComponent.invokeComponentInstance(AbstractJavaComponent.java:83) at org.mule.component.AbstractJavaComponent.doInvoke(AbstractJavaComponent.java:74) at org.mule.component.AbstractComponent.invokeInternal(AbstractComponent.java:133) at org.mule.component.AbstractComponent.invoke(AbstractComponent.java:161) at org.mule.service.AbstractService.invokeComponent(AbstractService.java:929) at org.mule.model.seda.SedaService.access$100(SedaService.java:56) at org.mule.model.seda.SedaService$ComponentStageWorker.run(SedaService.java:574) at org.mule.work.WorkerContext.run(WorkerContext.java:310) at edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1061) at edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:575) at java.lang.Thread.run(Thread.java:619) ********************************************************************************
修改interface:
package demo.mule.umo; public interface HelloWorld { public String sayHello(); public String sayHello2(); }
修改实现类:
package demo.mule.umo.impl; import demo.mule.umo.HelloWorld; public class HelloWorldImpl implements HelloWorld { @Override public String sayHello() { return "Hello world"; } @Override public String sayHello2() { return "Hello2 world"; } }
运行:抛出ERROR
ERROR 2011-02-09 17:07:27,811 [HelloUMO.2] org.mule.service.DefaultServiceExceptionStrategy: ******************************************************************************** Message : [ The required property "method" is not set on the event CallableEntryPointResolver:Object "demo.mule.umo.impl.HelloWorldImpl@1f02b85" does not implement required interface "interface org.mule.api.lifecycle.Callable" Could not find entry point on: "demo.mule.umo.impl.HelloWorldImpl" with arguments: "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=true, acceptVoidMethods=false}" Could not find entry point on: "demo.mule.umo.impl.HelloWorldImpl" with arguments: "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=false, acceptVoidMethods=false}" ] Type : org.mule.model.resolvers.EntryPointNotFoundException Code : MULE_ERROR--2 JavaDoc : http://www.mulesource.org/docs/site/current2/apidocs/org/mule/model/resolvers/EntryPointNotFoundException.html ******************************************************************************** Exception stack is: 1. [ The required property "method" is not set on the event CallableEntryPointResolver:Object "demo.mule.umo.impl.HelloWorldImpl@1f02b85" does not implement required interface "interface org.mule.api.lifecycle.Callable" Could not find entry point on: "demo.mule.umo.impl.HelloWorldImpl" with arguments: "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=true, acceptVoidMethods=false}" Could not find entry point on: "demo.mule.umo.impl.HelloWorldImpl" with arguments: "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=false, acceptVoidMethods=false}" ] (org.mule.model.resolvers.EntryPointNotFoundException) org.mule.model.resolvers.DefaultEntryPointResolverSet:63 (http://www.mulesource.org/docs/site/current2/apidocs/org/mule/model/resolvers/EntryPointNotFoundException.html) ******************************************************************************** Root Exception stack trace: org.mule.model.resolvers.EntryPointNotFoundException: [ The required property "method" is not set on the event CallableEntryPointResolver:Object "demo.mule.umo.impl.HelloWorldImpl@1f02b85" does not implement required interface "interface org.mule.api.lifecycle.Callable" Could not find entry point on: "demo.mule.umo.impl.HelloWorldImpl" with arguments: "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=true, acceptVoidMethods=false}" Could not find entry point on: "demo.mule.umo.impl.HelloWorldImpl" with arguments: "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=false, acceptVoidMethods=false}" ] at org.mule.model.resolvers.DefaultEntryPointResolverSet.invoke(DefaultEntryPointResolverSet.java:63) at org.mule.component.DefaultLifecycleAdapter.invoke(DefaultLifecycleAdapter.java:205) at org.mule.component.AbstractJavaComponent.invokeComponentInstance(AbstractJavaComponent.java:83) at org.mule.component.AbstractJavaComponent.doInvoke(AbstractJavaComponent.java:74) at org.mule.component.AbstractComponent.invokeInternal(AbstractComponent.java:133) at org.mule.component.AbstractComponent.invoke(AbstractComponent.java:161) at org.mule.service.AbstractService.invokeComponent(AbstractService.java:929) at org.mule.model.seda.SedaService.access$100(SedaService.java:56) at org.mule.model.seda.SedaService$ComponentStageWorker.run(SedaService.java:574) at org.mule.work.WorkerContext.run(WorkerContext.java:310) at edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1061) at edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:575) at java.lang.Thread.run(Thread.java:619) ********************************************************************************
比较两次抛出的ERROR,不难发现这句话“
Could not find entry point on: "demo.mule.umo.impl.HelloWorldImpl" with arguments: "{class java.lang.String}" using resolver "ReflectionEntryPointResolver{ignoredMethods=[getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll]{transformFirst=true, acceptVoidMethods=false}"
”,真相大白:mule通过<inbound></inbound>中得到的结果,作为寻找方法中参数的依据,由于该项目中,从终端输入的内容,项目接收时为String类型,因此,mule将查找参数类型为String的方法,同时,mule将抛弃所有符合“getClass, clone, equals, hashCode, getInvocationHandler, get*, wait, is*, notify, toString, notifyAll”的方法,如果符合条件的只有一个,那正好调用,否则,就抛出ERROR。
如果程序中很可以确定是哪个方法,大可让他自己去找,成人之美,若是不能确定是哪个方法,那么就只能够指腹为婚了,进行如下配置:
将尝试二的配置文件修改为:
<?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"> <method-entry-point-resolver> <include-entry-point method="sayHello2"/> </method-entry-point-resolver> </component> <outbound> <pass-through-router> <stdio:outbound-endpoint system="OUT" /> </pass-through-router> </outbound> </service> </model> </mule>
设置方法切入点,运行,成功!