应用程序可以调用通过非Mule 线程(即应用程序创建的线程)上的组件绑定创建的代理吗?我正在尝试这样做,但我在 org.mule.DefaultMuleEvent:268 上得到了 NullPointerException。
在 Mule EE 3.3.0 上
谢谢。
更新:
骡代码
<mule ...>
<vm:endpoint path="entryPoint" name="entryPoint" />
<flow name="entryPoint.Flow">
<inbound-endpoint ref="entryPoint" exchange-pattern="request-response" />
<component class="foo.Component">
<binding interface="foo.Interface" method="echo">
<vm:outbound-endpoint path="foo.Interface.echo" exchange-pattern="request-response" />
</binding>
</component>
</flow>
<flow name="foo.Interface.echo">
<vm:inbound-endpoint path="foo.Interface.echo" exchange-pattern="request-response" />
<logger level="INFO" />
</flow>
</mule>
Java 组件
package foo;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class Component {
private Interface i;
public String foo(final String input) {
return callInterfaceOnAWorkerThreadWith(input);
}
public void setInterface(final Interface i) {
this.i = i;
}
private String callInterfaceOnAWorkerThreadWith(final String input) {
ExecutorService executorService = newSingleThreadExecutor();
Future<String> future = executorService.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return i.echo(input);
}
});
executorService.shutdown();
try {
executorService.awaitTermination(60, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
try {
return future.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
}
Java 接口
package foo;
public interface Interface {
String echo(String input);
}
执行 mule 应用程序的测试夹具
package foo;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.api.client.MuleClient;
import org.mule.tck.junit4.FunctionalTestCase;
@RunWith(MockitoJUnitRunner.class)
public class ATest extends FunctionalTestCase {
@Test
public void echo() {
final MuleClient client = muleContext.getClient();
MuleMessage reply = send(client, "entryPoint", "a string");
assertEquals("a string", reply.getPayload());
}
@Override
protected String getConfigResources() {
return "app/componentbindingonanotherthread.xml";
}
private MuleMessage send(final MuleClient client, final String url, final Object payload) {
try {
return client.send(url, payload, null, RECEIVE_TIMEOUT);
} catch (final MuleException e) {
throw new RuntimeException(e);
}
}
}
执行上面的代码会在日志中显示以下异常:
Root Exception stack trace:
java.lang.NullPointerException
at org.mule.DefaultMuleEvent.<init>(DefaultMuleEvent.java:268)
at org.mule.component.BindingInvocationHandler.invoke(BindingInvocationHandler.java:96)
at $Proxy14.echo(Unknown Source)