我在mule 3中做一个新的流程,我使用java组件连接到unix服务器,并从服务器发送一些查询。
Java代码的工作是预期的,但如果我把同样的代码在java组件在mule中,我得到下面提到的错误。
org.mule.model.resolvers.EntryPointNotFoundException: Failed to find entry point for component, the following resolvers tried but failed: [
MethodHeaderPropertyEntryPointResolver: The required property "method" is not set on the event
CallableEntryPointResolver: Object "connectionpkg.Connection@1deb78e1" does not implement required interface "interface org.mule.api.lifecycle.Callable"
AnnotatedEntryPointResolver: Component: connectionpkg.Connection@1deb78e1 doesn't have any annotated methods, skipping.
ReflectionEntryPointResolver: Could not find entry point on: "connectionpkg.Connection" with arguments: "{}"
]. Component that caused exception is: DefaultJavaComponent{newconFlow.component.1397643446}.
附加mule代码供参考。
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="7007" doc:name="HTTP Listener Configuration"/>
<flow name="newconFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/conn" allowedMethods="get" doc:name="HTTP"/>
<component doc:name="Java" class="connectionpkg.Connection"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
Java源码是。
package connectionpkg;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class Connection {
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
String user = "xxxx";
String host = "xxxxx";
int port = 22;
String privateKey = "xxxxxxxx";
jsch.addIdentity(privateKey);
System.out.println("identity added ");
Session session = jsch.getSession(user, host, port);
System.out.println("session created.");
// disabling StrictHostKeyChecking may help to make connection but makes it insecure
// see http://stackoverflow.com/questions/30178936/jsch-sftp-security-with-session-setconfigstricthostkeychecking-no
//
// java.util.Properties config = new java.util.Properties();
// config.put("StrictHostKeyChecking", "no");
// session.setConfig(config);
session.connect();
System.out.println("session connected.....");
Channel channel = session.openChannel("sftp");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
System.out.println("shell channel connected....");
ChannelSftp c = (ChannelSftp) channel;
String fileName = "test.txt";
c.put(fileName, "./in/");
c.exit();
System.out.println("done");
} catch (Exception e) {
System.err.println(e);
}
}
}
Appreciate someone will Look into it and help to resolve the issue.
谢谢,Nikhil
Hi Attaching the piece of java code for reference.
package connectionpkg;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class Connection {
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
String user = "xxxx";
String host = "xxxxx";
int port = 22;
String privateKey = "xxxxxxxx";
jsch.addIdentity(privateKey);
System.out.println("identity added ");
Session session = jsch.getSession(user, host, port);
System.out.println("session created.");
// disabling StrictHostKeyChecking may help to make connection but makes it insecure
// see http://stackoverflow.com/questions/30178936/jsch-sftp-security-with-session-setconfigstricthostkeychecking-no
//
// java.util.Properties config = new java.util.Properties();
// config.put("StrictHostKeyChecking", "no");
// session.setConfig(config);
session.connect();
System.out.println("session connected.....");
Channel channel = session.openChannel("sftp");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
System.out.println("shell channel connected....");
ChannelSftp c = (ChannelSftp) channel;
String fileName = "test.txt";
c.put(fileName, "./in/");
c.exit();
System.out.println("done");
} catch (Exception e) {
System.err.println(e);
}
}
}
问题是Mule不知道你要调用哪个方法。在Mule中解决这个问题的标准方法是,你创建一个类,这个类的功能是 实现了Callable接口并从onCall()方法中调用你的自定义代码。这样一来,你就更不需要修改现有的Java代码了。
这篇博客文章有一个如何做的例子。https:/www.ricston.comblogusing-java-code-mule. 提示:不要像例子那样使用System.out。
更新:我看到Java代码中有一个main()方法,要在命令行中使用。这不是一个好的重用代码的设计。你应该把它重构为一个方法,其参数为硬编码的数据(host,user等)。然后你就可以实现Callable接口,它只是有一个onCall()方法,它应该调用你的新方法。
只是为了给你一个概念,这是一个伪代码的例子。
public class Connection implements Callable {
public static void main(String[] arg) {
execute(args[1], args[2],...);
}
public void execute(String host, String user, ...) {
// Most of the code that was previously in the main method.
}
public Object onCall(MuleEventContext event) {
// get parameters somehow
execute(host, user, ...)
}
}