Skip to main content

Service implementation

  1. Create a "locator_service" project first, following the steps as above.

  2. Create a GreeterImpl.java which implements the Greeter interface defined above.

    The content of GreeterImpl.java is:

    import javax.jws.WebService;
    
    import demo.common.Greeter;
    @WebService(targetNamespace = "http://talend.org/esb/examples/", 
            serviceName = "GreeterService")
    public class GreeterImpl implements Greeter {
    
        public String greetMe(String me) {
            System.out.println("Executing operation greetMe");
            System.out.println("Message received: " + me + "\n");
            return "Hello " + me;
        }
    }

    For each input, a statement "'Hello '+input" will be returned.

    This is where the Service Locator becomes useful, because as mentioned at the beginning of this section, the Service Locator is a mechanism to discover service endpoints at runtime.

  3. In order to make the Implementation discoverable, you need to register it first.

    There are two ways to register a service: by Spring configuration or by code directly.

    • 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" />.

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:jaxws="http://cxf.apache.org/jaxws"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
          <import resource="classpath:META-INF/cxf/cxf.xml" />
          <import resource="classpath:META-INF/tesb/locator/beans.xml" />
      
          <jaxws:endpoint xmlns:tns="http://talend.org/esb/examples/"
              id="greeter" implementor="demo.service.GreeterImpl" 
              serviceName="tns:GreeterService"
              address="/GreeterService">
              <jaxws:features>
                  <ref bean="locatorFeature"/>
              </jaxws:features>
          </jaxws:endpoint>
      </beans>

      In the Spring file example above, the OSGi import line (classpath:META-INF/tesb/locator/beans.xml) is the only difference from a standard Spring configuration file.

      And to add the Locator feature to a CXF service provider, use the <jaxws:features> including the org.talend.esb.servicelocator.cxf.LocatorFeature.

      Then load it by using "ClassPathXmlApplicationContext". It is important to include the configuration file in exported bundle and also add the necessary dependencies for Spring configuration.

    • The alternative code version is:

      LocatorFeature locatorFeature = new LocatorFeatureImpl();
      Greeter greeterService = new GreeterImpl();
      svrFactory = new JaxWsServerFactoryBean();
      // WSDL operations that service will implement
      svrFactory.setServiceClass(Greeter.class); 
      // endpoint service will listen on
      svrFactory.setAddress("http://localhost:8082/services/Greeter"); 
      // implementation of WSDL operations
      svrFactory.setServiceBean(greeterService); 
      // attach LocatorFeature to web service provider
      svrFactory.getFeatures().add(locatorFeature); 
      svrFactory.create();
  4. Similar to , export "locator_service" as a bundle, so the BundleActivator is the best place to register or remove this service:

    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
    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 Activator implements BundleActivator {
    
        private JaxWsServerFactoryBean svrFactory;
    
        public void start(BundleContext context) throws Exception {
            LocatorFeature locatorFeature = new LocatorFeatureImpl();
            Greeter greeterService = new GreeterImpl();
            svrFactory = new JaxWsServerFactoryBean();
            svrFactory.setServiceClass(Greeter.class);
            svrFactory.setAddress("http://localhost:8082/services/Greeter");
            svrFactory.setServiceBean(greeterService);
            svrFactory.getFeatures().add(locatorFeature);
            svrFactory.create();
        }
    
        public void stop(BundleContext context) throws Exception {
            svrFactory.destroy();
        }
    }

    There is all the code you need to provide.

  5. Configure the pom.xml, add the necessary dependencies, and configure the exported bundle information.

    Finally, the content of pom.xml is:

    <groupId>com.talend.liugang.cxf</groupId>
    <artifactId>locator_service</artifactId>
    <version>1.0.0</version>
    <packaging>bundle</packaging>
    
    <name>locator_service</name>
    <url>http://maven.apache.org</url>
    <properties>
        <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>
        
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>1.4.0</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>
                        <Import-Package>demo.common,javax.jws,
                            org.apache.cxf.endpoint,org.apache.cxf.jaxws,
                            org.osgi.framework,org.talend.esb.locator
                        </Import-Package>
                        <Bundle-Activator>demo.service.Activator</Bundle-Activator>
                        <Require-Bundle>
                            org.apache.cxf.bundle;version=""
                        </Require-Bundle>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
  6. Install it into Maven by running Maven Install.

    Now the Service interface is defined, and implemented. It is time to write a client which will consume the service.

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!