Connecting Java-based SwitchYard Composites with SOAP Web Services (Tutorial)

Version 3

    Overview

    In this tutorial we will create two SwitchYard composites, a provider and a consumer. We will publish a SOAP web service from the provider, and invoke it from the consumer.

    The implementation of both the provider and the consumer will be plain old Java beans.

    consumer.pngprovider.png

    Prerequisites

    • JBoss Developer Studio 7.1
    • JBoss EAP 6.1 server with SwitchYard 1.1 installed
    • Basic knowledge of SwitchYard and the JBDS SwitchYard tooling

    Creating the Provider Application

    Provider Service Implementation

    1. In the JBDS project explorer, right click and select "New SwitchYard Project"
      Use the project name "soap-provider"
    2. In the project configuration page...
      • Accept the auto-generated defaults for group id, package name and target namespace
      • Select a JBoss EAP 6.1 with SwitchYard runtime (see prerequisites)
      • In components, select "Implementation Support > Bean" and "Gateway Bindings > SOAP"
        • This project will have a service implementation as a Java Bean, and a SOAP service interface
      • Finish the project creation wizard; you should see a blank SCA diagram
    3. Add a Bean implementation to the composite
    4. Click the “Interface” link to create a new Java interface with the name "HelloProvider"
      • The bean name (HelloProviderBean) and service name (HelloProvider) should be filled in automatically
    5. Add a method String greet(String name); to the HelloProvider interface
    6. Add the corresponding method & implementation to HelloProviderBean...
      return "Hello " + name;
    7. Back on the SwitchYard diagram, hover over the component interface and click the green "Promote Service" button
    8. Select interface type "WSDL" and create a new interface by clicking the "Interface" hyperlink
      • Use the name "HelloService.wsdl" for the WSDL
      • Replace the service name with "HelloService"
      • Uncheck "wrapped messages"
      • Accept defaults for the rest of the wizard
    9. Make sure “Create required transformers” is checked
      • Accept the defaults on the Transformers page (should be Java transformers)
      • Use "ProviderTransformers" as the name of the Transformer class
    10. Add a SOAP binding to the new composite service
    11. Edit the new ProviderTransformers class...
      • Change the return type of transformStringToString(String) to String
      • Change the method body to...
        return "<string>" + from + "</string>";
      • Change the body of transformStringToString1() to...
        return from.getTextContent();


    Provider Test Client

    1. In project explorer, right click on the "soap-provider" project and select "New > SwitchYard Test"
      • Use "HelloProviderTest" as the name, "HelloProvider" as the service
    2. Edit the HelloProviderTest unit test...
      • Change the parameter to sendInOut() to "Fred"
      • Change the assertion at the end to
        Assert.assertEquals("Hello Fred", result);
    3. Run the HelloProviderTest unit test and make sure it passes

    Provider Deployment

    1. Deploy the composite to your EAP server
    2. Open http://localhost:8080/HelloProvider/HelloProvider?wsdl in your web browser to confirm deployment.
      • Adjust the URL as appropriate if your JBoss/SwitchYard server isn’t running on localhost:8080.
    3. Test the service with SoapUI if possible

    Creating the Consumer Application

    Consumer Service Implementation

    In the first part of the consumer implementation we create the component & service that will eventually consume the external service reference, but without actually using that external service.

    1. In the JBDS project explorer, right click and select "New SwitchYard Project"
      • Use the project name "soap-consumer"
    2. In the project configuration page...
      • Use the default group ID, package name and target namespace
      • In components, select "Implementation Support > Bean", "Gateway Bindings > SOAP" and "Gateway Bindings > SCA"
    3. Finish the project creation wizard; you should see a blank SCA diagram
    4. Add a Bean implementation to the composite
    5. Create a new Java interface (click the "Interface" link) with the name "HelloConsumer"
    6. The bean name and service name should be filled in automatically
    7. Add a method String greet(); to the HelloConsumer interface
    8. Add the corresponding method & implementation to HelloConsumerBean
      return "Hello world";
    9. Back on the SwitchYard diagram, hover over the component interface and click the green "Promote Service" button
    10. Select interface type "Java" and accept the defaults to re-use the existing Java interface
    11. Add an SCA binding to the promoted service interface, accepting the defaults


    Consumer Test Client

    1. In project explorer, right click on "soap-consumer" project and select "New > SwitchYard Test"
      • Use "HelloConsumerTest" as the name, "HelloConsumer" as the service
    2. Edit the HelloConsumerTest unit test
      • Change the assertion at the end to...
        Assert.assertEquals("Hello world", result);
    3. Run the HelloConsumerTest unit test and make sure it passes

    Adding the Provider Service Reference

    1. Download the published HelloService.wsdl into src/main/resources in soap-consumer project
      • Make sure it is named “HelloService.wsdl”
      • Refresh the project to make the file visible in JBDS
    2. Back in the service-consumer SwitchYard SCA composite diagram, drag a "Core > Reference" onto the composite (not onto a component)
      • Select "WSDL" as the interface type
      • Click "Browse" and select the HelloService.wsdl file
    3. Right-click on the reference and select "Generate Java interface"
    4. Delete the HelloProviderPortType reference you just created
      • We'll re-create it later by promoting a component reference
    5. Add a new reference to the HelloConsumer component
      • Interface Type is Java
      • Browse and select "HelloProviderPortType"
    6. Hover over the component reference and click the green "promote" button
      • Select interface type "WSDL" and browse for "HelloService.wsdl"
      • Enter "HelloService" as the service name
      • Make sure “Create required transformers” is checked
    7. Select defaults for transformers, and "ConsumerTransformers" for the transformer class name
    8. Edit the ConsumerTransformers class
      • Change the return type of transformStringToString(String) to String
      • Change the body to...
        return "<string xmlns=\"urn:com.example.switchyard:soap-provider:1.0\">" + from + "</string>" ;
        • Confirm the namespace matches the one in the @Transform annotation
      • Change the transformStringToString1 method body to
        return from.getTextContent();
    9. Back in the SwitchYard SCA diagram, add a SOAP binding to the HelloService reference, accepting all the defaults
    10. Edit the HelloConsumerBean class, adding a reference to the provider service...
      @Reference @Inject
      HelloProviderPortType provider;
    11. Change the body of the HelloConsumerBean.test() method to invoke the provider...
      return provider.greet("world");
    12. Run the HelloConsumerTest unit test again and make sure it still passes
    13. Change the HelloConsumerBean and HelloConsumerTest implementations to use your own name, and make sure the test still passes.