Translate to Different Languages!

Sunday, November 10, 2013

Your First Fuse Service Works Application

Introduction

This blog will show you how to create your first JBoss Fuse Service Works 6 Application.  The application will be a basic application that exposes a simple web service implemented in a Java Bean.  The service will accept a string and add "Hello" such as "Hello Kenny!" which is displayed in the console.  A transformer changes the response to "Hello World!".  We will use SOAPUI as the client to send the SOAP message to the service.

Preparation

Before we get started with the application, install JBoss Developer Studio 7 with the Integration Stack for JBDS 7 as well as the Fuse Service Works 6 Product.    You can find the instructions  for installation in addition to the downloads at http://www.jboss.org/products/fsw.html.  Also install SOAPUI which can be found at http://www.soapui.org/, either standalone or the eclipse plugin (http://www.soapui.org/eclipse/update).

Step Overview

The following describes the steps that we will follow to build and deploy our application:

1. Start JBoss Developer Studio
2. Create a new workspace
3. Create a new Switchyard Project
4. Create a Bean component and its contract
5. Add the Business Logic of the bean  and the details of the contract
6. Promote the Service and create the transformation
7. Create the SOAP binding
8. Deploy the Service
9. Test the Service

Step By Step

Step 1 : Start JBoss Developer Studio

I have different installs and version of JBDS so I start this particular instance from the command prompt.  The blog assumes you have the software installed as described in the Preparation section.  You can start JBDS from the command prompt, Program Menu Item or desktop launcher.


Step 2 : Create a new workspace

Create a new workspace, I used /home/kpeeples/workspaces/fsw-gettingstarted


Step 3 : Create a new Switchyard Project

Select the New Switchyard Project Wizard.

We will take the default on the first screen of switchyard-example.  Click the next button.  On the second screen, the switchyard project configuration, change the Runtime to "none" as we will deploy to the server after creation.  Select the Switchyard version of 1.0.0.Final.   Select the Bean Implementation and the SOAP Gateway binding.  Then click Finish to create the project.

The editor which consists of the canvas and palette will be opened.



  • The Canvas, shown in Blue, is our workspace where we will create and manipulate project elements.
  • The Palette, shown on the right side, contains the elements that can be added to the project.
Step 4 :  Create a Bean component and its contract

To create a new bean component, select the Bean implementation icon from the palette and drag to the canvas.  The Bean definition dialog will open.  Type HelloServiceBean in the Name field and then click on the interface link to create the corresponding contract/interface.  Enter HelloService for the interface.  Then click Finish for the interface and Finish for the bean.

Step 5 : Add the Business Logic of the bean  and the details of the contract

Add the following code to the bean and it's interface:

HelloService.java
package com.example.switchyard.switchyard_example;

public interface HelloService {
 String sayHello(String helloString);

}
HelloServiceBean.java
package com.example.switchyard.switchyard_example1;

import org.switchyard.component.bean.Service;

@Service(HelloService.class)
public class HelloServiceBean implements HelloService {

  @Override
    public String sayHello(String helloString) {
      System.out.println("Hello: " + helloString);
      return "Hello " + helloString + "!";
    }
}

Step  6 : Promote the Service and create the transformation

In this step we promote the service to make is visible by clicking on the promote service button or right click on the service's green icon and select promote service.  The service should be visable as a web service so we select WSDL as the interface type.  Then click interface to generate the WSDL based on the contract/interface that was defined earlier.
Click next to specify the WSDL options.  Uncheck the wrapped messages so transformations can access only what is inside the elements they receive.  Click finish which will show the interface  and service name.  Make sure to leave the create required transformations checked so we can define our transformer.

Click next.  Leave both transformer types checked and the type as a java transformer.  Type in the name as ExampleTransformer and click finish which will show our composite-service and our bean component.
Open the Transformer class and add the following code.
ExampleTransformer.java
package com.example.switchyard.switchyard_example;

import org.switchyard.annotations.Transformer;
import org.w3c.dom.Element;

public final class ExampleTransformer {

@Transformer(to = "{urn:com.example.switchyard:switchyard-example:1.0}string")
public String transformStringToString(String from) {
// TODO Auto-generated method stub
String to = "Hello World!";
return "<sayHelloResponse xmlns=\"urn:com.example.switchyard:switchyard-example:1.0\"><string>"
+ from + " TRANSFORMED TO " + to + "</string></sayHelloResponse>";
}

@Transformer(from = "{urn:com.example.switchyard:switchyard-example:1.0}string")
public String transformStringToString1(Element from) {
// TODO Auto-generated method stub
return new String(from.getTextContent());
}

}

Step 7 : Create the SOAP binding

Go to the canvas and click on the create binding button on the composite-service.  Select the SOAP binding from the menu.   Keep the defaults and click on Finish.

Step 8 :  Deploy the Service

Now we can deploy our service.  I already added the Fuse Service Works Server in the IDE and started the server.  Right click on the project and indicate to Run As/Run on Server.  Take the defaults to deploy to the server.  The Application should deploy as shown below.  The WSDL should be accessible at http://localhost:8080/HelloService/HelloService?wsdl

Step 9 : Test the Service

Create a new SOAPUI project with the WSDL.  Enter Kenny as the string for the request.  Submit the request.  On the console Hello: Kenny displays.  In the SOAP response Hello Kenny! TRANSFORMED TO Hello World!  because of the code in the transformer.  You have created your first simple Fuse Service Works Application.


1 comment:

  1. Excellent example, had no trouble following it using JBDS 7.1.1.GA and FSW 7.2. Tks for posting it.

    ReplyDelete