简体   繁体   English

WSO2 ESB定制连接器

[英]WSO2 ESB custom connector

I'm confusing with creating a custom connector for WSO2 ESB 5.0.0. 我为WSO2 ESB 5.0.0创建自定义连接器感到困惑。 I need a custom connector for legacy device (thermometer). 我需要旧设备(温度计)的自定义连接器。 This connector will be called via ESB REST API. 该连接器将通过ESB REST API调用。 The only thing the connector should do is to create socket connection to given IP address (connector input parameter) and then parse the response data. 连接器唯一要做的就是创建到给定IP地址(连接器输入参数)的套接字连接,然后解析响应数据。 The program works perfectly alone. 该程序可以完美运行。 However, I don't know how to integrate it to custom connector. 但是,我不知道如何将其集成到自定义连接器。 Especially how to send data from the connector as a response to API call. 特别是如何从连接器发送数据作为对API调用的响应。

My connector code: 我的连接器代码:

@Override
public void connect(MessageContext messageContext) throws ConnectException {
    Object templateParam = getParameter(messageContext, "generated_param");
    try {
        log.info("sample connector received message :" + templateParam);
        /**Add your connector code here 
        **/
        Socket socket = new Socket("172.16.xxx.xxx", 2000);

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        out.write("*SRTC\r");
        out.flush();

        System.out.println(in.readLine());

        out.close();
        in.close();
        socket.close();

    } catch (Exception e) {
    throw new ConnectException(e);  
    }
}

Where the message in in.readLine() should be send ??? in.readLine()中的消息应该发送到哪里?

SOLVED: I stored the response in messageContext: 已解决:我将响应存储在messageContext中:

messageContext.setProperty("temperature", Double.parseDouble(in.readLine()));

and then process with next mediator. 然后与下一个调解员处理。 Thanks 谢谢

I guess you are confused connector with the class mediator. 我想您对类调解器感到困惑。 Basically, A connector is a collection of templates that define operations users can call from their ESB configurations to easily access specific logic for processing messages. 基本上,连接器是模板的集合,这些模板定义了用户可以从其ESB配置调用的操作,以轻松访问用于处理消息的特定逻辑。 Typically, connectors are used to wrap the API of an external service. 通常,连接器用于包装外部服务的API。 For example, there are several default connectors provided with the ESB that call the APIs of services like Twitter and JIRA. 例如,ESB提供了几个默认的连接器,它们调用诸如Twitter和JIRA之类的服务的API。 You can also create your own connector to provide access to other services. 您也可以创建自己的连接器以提供对其他服务的访问。

We can't create a java functions within the connector templates. 我们无法在连接器模板中创建Java函数。 So that we create a java class and call that custom class within the connector template using class mediator like, 因此,我们创建了一个Java类,并使用类中介器在连接器模板中调用该自定义类,

<class name="class-name"/>

Refer, 参考,

https://docs.wso2.com/display/ESBCONNECTORS/Writing+a+Connector https://docs.wso2.com/display/ESBCONNECTORS/Writing+a+Connector

https://docs.wso2.com/display/ESB500/Class+Mediator https://docs.wso2.com/display/ESB500/Class+Mediator

You should be able to invoke your custom mediator with the mediator. 您应该能够使用调解器调用自定义调解器。 Below is an example of how to do it. 以下是如何执行此操作的示例。

<class name="org.wso2.esb.tutorial.mediators.SurchargeStockQuoteMediator">
<property name="defaultPercentage" value="10"/>

You'll find usefull information under the following URLs. 您可以在以下URL下找到有用的信息。

http://wso2.com/library/2898/ http://wso2.com/library/2936/ http://wso2.com/library/2898/ http://wso2.com/library/2936/

Hope that helps. 希望能有所帮助。

You can add the response in messageContext in the connector code and later you can call the value in proxy using the name you provide while add the value in properties[1]. 您可以在连接器代码的messageContext中添加响应,然后可以在提供的名称添加到properties [1]时使用提供的名称在proxy中调用该值。 Now you can pass the value to any API. 现在,您可以将值传递给任何API。

[1] https://github.com/wso2-extensions/esb-connector-ejb2.0/blob/master/src/main/java/org/wso2/carbon/custom/connector/CallEJBStatelessBean.java#L41 [1] https://github.com/wso2-extensions/esb-connector-ejb2.0/blob/master/src/main/java/org/wso2/carbon/custom/connector/CallEJBStatelessBean.java#L41

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM