Service Locator for RESTful services - 6.5

Talend ESB Development Guide

EnrichVersion
6.5
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

The Service Locator feature can be used for both SOAP and RESTful Web Services.

The Service Locator configuration for Web services using the REST architectural style is similiar to the SOAP services configuration as described in previous sections.

To add the Locator feature to a RESTful service provider, use <jaxrs:features> including the bean org.talend.esb.servicelocator.cxf.LocatorFeature as below:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://cxf.apache.org/jaxrs
           http://cxf.apache.org/schemas/jaxrs.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/tesb/locator/beans-osgi.xml" />

    <bean id="orderService" class="demo.service.OrderServiceImpl">
    </bean>

    <jaxrs:server id="orderRESTService" address="/rest">
        <jaxrs:features>
            <bean id="orderServiceLocator" 
                  class="org.talend.esb.servicelocator.cxf.LocatorFeature"/>
        </jaxrs:features>
        <jaxrs:serviceBeans>
            <ref bean="orderService" />
        </jaxrs:serviceBeans>
    </jaxrs:server>
</beans>

To add the Locator feature to a CXF service consumer, use <jaxrs:client> including the bean org.talend.esb.servicelocator.cxf.LocatorFeature as below:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://cxf.apache.org/jaxrs 
            http://cxf.apache.org/schemas/jaxrs.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/tesb/locator/beans-osgi.xml" />

    <jaxrs:client id="restClient"
        address="locator://some_useful_information"
        serviceClass="demo.common.OrderService"
        xmlns:serviceNamespace="http://service.demo/"
        serviceName="serviceNamespace:OrderServiceImpl"
        inheritHeaders="true">
        <jaxrs:headers>
            <entry key="Accept" value="application/xml"/>
        </jaxrs:headers>
        <jaxrs:features>
            <bean class="org.talend.esb.servicelocator.cxf.LocatorFeature">
                <property name="selectionStrategy" 
				value="evenDistributionSelectionStrategy"/>
            </bean>
        </jaxrs:features>
    </jaxrs:client>
</beans>

As shown in the example above, <jaxrs:client> was configured by setting the serviceName attribute. You need this service name to discover the endpoint from the Locator server. Please note the serviceName attribute specifies a service QName, here xmlns:serviceNamespace="http://service.demo/" serviceName="serviceNamespace:OrderServiceImpl"

The locator protocol in the address attribute is used to enable the Locator feature.