2015-06-18 44 views
1

我在寫我的第一個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"/> 

回答

2

Here你可以找到一個簡單的前充足的如何實現連接管理。此外,本文檔提供了一個簡單的逐步指導,介紹如何從頭到尾構建連接器。 HTH。

+0

我給出了一個提示,因爲這是很好的文檔,但它不回答我的具體問題(或者至少我沒有找到它的文檔)。我希望能夠向連接管理策略類發送附加參數,而不僅僅是創建連接。 – MonomiDev

+0

進一步看,我在你的文檔中找到了答案。我能夠通過向ConnectionManagement類添加@Configurable元素來完成我所需的操作。我可以在配置文件的配置元素中設置這個值。我將用一個例子來編輯我的文章,但是感謝文檔,它回答了這個問題! – MonomiDev

2

您可以擁有多個@ConnectionKey元素,名稱可以是任何內容。

例如,這是完全合法的:

@Connect 
@TestConnectivity 
public void connect(@ConnectionKey String composedKeyItem1, 
        @ConnectionKey String composedKeyItem2) 

您也可以使用@Configurable元素,因爲@Connect被調用之前,他們將被初始化。

+0

但是,如果devkit自動處理實例化對象,我該如何告訴連接器將自定義屬性傳遞給構造函數? – MonomiDev