我正在编写我的第一个Mulesoft连接器。我正在尝试实现@ConnectionManagementStrategy。我引用了:
我正在尝试实现与自定义Web服务的连接。对于我的客户端,我需要指定用户名,密码和端点。如果我对端点进行硬编码,我能够使连接正常工作。但是,我们可能会将此连接器重新用于其他服务,并且我希望能够配置端点。根据文档,devkit会自动调用/创建类实例。我希望能够基于@Connector中的可配置属性将构造函数传递给构造函数,或者将其他参数传递给@Connect方法。
我没有在文档中找到如何自定义这些方法。
我的@Connector:
@Connector(name="testContactConnector", friendlyName = "Test Contact Connector")
public class Contact {
@Configurable
@Default("https://my.cool.service")
public String webServiceEndpoint;
@ConnectionStrategy
private Connection connection;
public void setConnection(Connection connection) {
this.connection = connection;
}
public Connection getConnection() {
return this.connection;
}
@Processor
public String getSessionID() throws Exception {
return this.connection.connectionID();
}
}
我的@ConnectionManagementStrategy:
@ConnectionManagement(friendlyName = "Contact Service Connection")
public class Connection {
private String serviceEndpoint, username, password, sessionID;
private Service service;
public Connection() {
}
@Connect
@TestConnectivity
public void connect(@ConnectionKey String username, @Password String password)
throws ConnectionException {
this.username = username;
this.password = password;
try {
this.service = Service.Logon(this.username, this.password, this.serviceEndpoint);
this.sessionID = this.service.getSession();
} catch (Exception error) {
throw new ConnectionException(ConnectionExceptionCode.INCORRECT_CREDENTIALS, null, error.getMessage(), error);
}
}
@Disconnect
public void disconnect() {
if(this.service != null) {
try {
this.service.killSession();
} catch (Exception error) {
error.printStackTrace();
}
finally {
this.service = null;
}
}
}
@ValidateConnection
public boolean isConnected() {
try {
return (this.service != null && this.service.getSession() != null);
} catch (Exception error) {
error.printStackTrace();
return false;
}
}
@ConnectionIdentifier
public String connectionID() {
try {
return this.service.getSession();
} catch (Exception error) {
error.printStackTrace();
return null;
}
}
}
修改 脂肪在示例代码中找到了一个字符
我根据答案中提供的文件提出的解决方案
在@ConnectionManagement类中,我添加了以下属性:
@Configurable
@Default("https://your.web.service")
public String webServiceEndpoint;
在@Connect方法中,我引用了该属性:
@Connect
@TestConnectivity
public void connect(@ConnectionKey String username, @Password String password) throws ConnectionException {
this.username = username;
this.password = password;
try {
this.service = Service.Logon(this.username, this.password, this.webServiceEndpoint);
this.sessionID = this.service.getSessionID();
} catch (Exception error) {
throw new ConnectionException(ConnectionExceptionCode.INCORRECT_CREDENTIALS, null, error.getMessage(), error);
}
}
在我的配置文件中,我有以下设置值:
<myconnector:config name="config" webServiceEndpoint="http://my.custom.service" username="foo" password="foo"/>
答案 0 :(得分:2)
Here您可以找到一个简单的示例,了解如何实现连接管理。此外,本文档提供了有关如何从头到尾构建连接器的简单分步指南。 HTH。
答案 1 :(得分:2)
您可以拥有多个@ConnectionKey元素,名称可以是任何名称。
这是完全有效的:
// This defines the 'delegate'.
public interface IA {
int f(int a);
}
public class MyClass {
// f1 and f2 have the same signature as 'IA.f'.
private int f1(int a) {
return a + 1;
}
private int f2(int a) {
return 2 * a;
}
// These wrappers are one way to return a 'delegate'.
// Each wrapper creates one instance of an anonymous class.
// Notice that we did not have to declare MyClass as implementing IA,
// and that we can wrap different methods of MyClass into 'IA's.
// Contrast this with 'MyClass implements IA', which would require
// a method 'f' in 'MyClass', and would not provide a way to
// delegate to different methods of 'MyClass'.
public IA wrapF1() {
return (new IA(){
public int f(int a) {
return f1(a);
}
});
}
public IA wrapF2() {
return (new IA(){
public int f(int a) {
return f2(a);
}
});
}
// returns a 'delegate', either to 'f1' or 'f2'.
public IA callMe(boolean useF2) {
if (!useF2)
return wrapF1();
else
return wrapF2();
}
}
您也可以使用@Configurable元素,因为它们将在调用@Connect之前初始化。