gpt4 book ai didi

java - 带有附加参数的 Mulesoft ConnectionManagementStrategy 类

转载 作者:行者123 更新时间:2023-11-29 03:08:10 25 4
gpt4 key购买 nike

我正在编写我的第一个 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;
}
}

}

编辑胖手指示例代码中的一个字符

我根据答案中提供的文档提出的解决方案

在@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"/>

最佳答案

Here您可以找到一个如何实现连接管理的简单示例。此外,本文档提供了有关如何从头到尾构建连接器的简单分步指南。 HTH.

关于java - 带有附加参数的 Mulesoft ConnectionManagementStrategy 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30920502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com