Skip to main content Skip to complementary content

Service Consumer

About this task

This time, you will try to consume the service above by using Service Locator instead of referencing the implementor directly. As for the service registration, you can use Spring configuration or code directly.

Procedure

  1. To make the consumer discoverable:
    • For Spring configuration:

      The Locator feature is enabled by declaring instances of its classes in the Spring configuration file: <import resource="classpath:META-INF/tesb/locator/beans.xml" />.

      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:jaxws="http://cxf.apache.org/jaxws"
          xmlns:util="http://www.springframework.org/schema/util" 
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/util 
          http://www.springframework.org/schema/util/spring-util-3.0.xsd ">
      
          <import resource="classpath:META-INF/cxf/cxf.xml" />
          <import resource="classpath:META-INF/tesb/locator/beans.xml" />
      
          <jaxws:client id="greeterService" address="locator://more_useful_information"
              serviceClass="demo.common.Greeter">
              <jaxws:features>
                  <ref bean="locatorFeature"/>
              </jaxws:features>
          </jaxws:client>
      </beans>
    • The alternative code version is:

      JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
      LocatorFeature locatorFeature = new LocatorFeatureImpl();
      factory.getFeatures().add(locatorFeature);
      factory.setServiceClass(Greeter.class);
      factory.setAddress("locator://more_useful_information");
      Greeter client = (Greeter) factory.create();
      String response = client.greetMe("MyName");
      
    An important point to note is that you must use the locator protocol for client address="locator://more_useful_information".
  2. As you will export the project as an OSGi bundle, so you will need to setup the test fragment in start() method of BundleActivator:
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.talend.esb.servicelocator.cxf.LocatorFeature;
    import org.talend.esb.servicelocator.cxf.internal.LocatorFeatureImpl;
    
    import demo.common.Greeter;
    
    public class Client implements BundleActivator {
    
        public void start(BundleContext context) throws Exception {
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            LocatorFeature locatorFeature = new LocatorFeatureImpl();
            factory.getFeatures().add(locatorFeature);
            factory.setServiceClass(Greeter.class);
            factory.setAddress("locator://more_useful_information");
            Greeter client = (Greeter) factory.create();
            String response = client.greetMe("MyName");
            System.out.println(response);
        }
    
        public void stop(BundleContext context) throws Exception {
        }
    }
  3. Configure the pom.xml with the following content:
    <groupId>com.talend.liugang.cxf</groupId>
    <artifactId>locator_client</artifactId>
    <version>1.0.0</version>
    <packaging>bundle</packaging>
    
    <name>locator_client</name>
    <url>http://maven.apache.org</url>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cxf.version>3.3.1</cxf.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.talend.esb</groupId>
            <artifactId>locator</artifactId>
            <version></version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>locator_common</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <version>2.3.7</version>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>
                            ${project.artifactId}
                        </Bundle-SymbolicName>
                        <Bundle-Activator>demo.client.Client</Bundle-Activator>
                        <Require-Bundle>locator_common</Require-Bundle>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
  4. Execute Run As > Maven Install to install this bundle. So far, you have finished all bundles, now you will see how to install them and also combine them with Service Locator.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!