Mule 3 java调用以连接到unix



我正在mule3中开发一个新的流,在那里我使用java组件连接到unix服务器并从服务器启动一些查询。

Java代码正在按预期工作,但如果我在mule中的Java组件中放置相同的代码,我会得到下面提到的错误:

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);
}
}
}

感谢有人会调查并帮助解决问题。

谢谢,Nikhil

问题是Mule不知道要调用哪个方法。在Mule中解决这个问题的标准方法是创建一个实现Callable接口的类,并从onCall((方法调用自定义代码。这样就更有可能不需要修改现有的Java代码。

这篇博客文章有一个如何做到这一点的例子:https://www.ricston.com/blog/using-java-code-mule/.提示:不要像示例那样使用System.out。

更新时间:我看到Java代码有一个main((方法,可以从命令行使用。对于重用代码来说,这不是一个好的设计。您应该将其重构为一个方法,其中包含数据的参数。这些数据是硬编码的(主机、用户等(。然后你就可以实现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, ...)
}
}

最新更新