0
votes

I'm confusing with creating a custom connector for WSO2 ESB 5.0.0. I need a custom connector for legacy device (thermometer). This connector will be called via 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. 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.

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 ???

4
you need to specified the in/out parameters for your mediator. these parameters can be accessed inside the mediator as properties and the mediator response can be put in a property or in the message payload.Jorge Infante Osorio
thanks, moreover i'm not sure how to invoke custom connector in ESB API. What is the right API sequence?vladikk

4 Answers

2
votes

SOLVED: I stored the response in messageContext:

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

and then process with next mediator. Thanks

1
votes

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. Typically, connectors are used to wrap the API of an external service. For example, there are several default connectors provided with the ESB that call the APIs of services like Twitter and JIRA. You can also create your own connector to provide access to other services.

We can't create a java functions within the connector templates. So that we create a java class and call that custom class within the connector template using class mediator like,

<class name="class-name"/>

Refer,

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

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

0
votes

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.

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

Hope that helps.

0
votes

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]. Now you can pass the value to any API.

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