我在寫我的第一個Mulesoft Connector。我正在嘗試實施@ConnectionManagementStrategy。我曾經引用:具有附加參數的Mulesoft ConnectionManagementStrategy類
https://developer.mulesoft.com/docs/display/current/Connection+Management
我想實現一個自定義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;
}
}
}
編輯 脂肪中的示例代碼
的解決辦法,我想出了PER手指字符答案中提供的文件
在@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"/>
我給出了一個提示,因爲這是很好的文檔,但它不回答我的具體問題(或者至少我沒有找到它的文檔)。我希望能夠向連接管理策略類發送附加參數,而不僅僅是創建連接。 – MonomiDev
進一步看,我在你的文檔中找到了答案。我能夠通過向ConnectionManagement類添加@Configurable元素來完成我所需的操作。我可以在配置文件的配置元素中設置這個值。我將用一個例子來編輯我的文章,但是感謝文檔,它回答了這個問題! – MonomiDev