Thursday, May 7, 2015

JBoss FSW Switchyard Tutorial 1: HelloWorld

Introduction

The video version is available at: https://youtu.be/FShVH3GM2Hs
The video for the blog is availabe at youtube now.

In my earlier blog, I have documented how to setup JBoss FSW development environment using JBoss Developer studio 7.1. The blog can be refered at http://ggl-consulting.blogspot.com/2014/10/build-deploy-test-and-debug-jboss-fuse.html

The purpose of this blog is to demonstrate the development process of switchyard application using JBoss Developer Studio 8.1. I will create a Hello World application from scratch, deploy it, and test it. Thus, people can learn the stuff in a ground-up style.

Now, JBDS 8.1 is available. The setup procedure is the same. Make update the Integration Stack from the Software/Update panel in the JBoss Central page as shown below

Procedures To Build The Project

Create Project

Create New Switchyard Project:
Give the project name and Click Next:
Configure the SwitchYard component:
Click Finish. You will see the following:
Drag the Bean Implementation from the Pallet to the Canvas. Fill the HelloWorldBean and HellowWorldService, click finish on both:
Modify the HelloWorldService.java like the following:
package switchyard_helloworld_demo;

public interface HelloWorldService {
 public String sayHello( String name);
}

Modify the HelloWorldServiceBean.java as the following:
package switchyard_helloworld_demo;

import org.switchyard.component.bean.Service;

@Service(HelloWorldService.class)
public class HelloWorldServiceBean implements HelloWorldService {

 @Override
 public String sayHello(String name) {
  
  return "Hello " + name + "!";
 }

}
Now the JBDS looks like this:
Promote The Service:
Click Finish in the new Promote service panel and you will see this:
Bind to SCA:
At this time, we are done with the HelloWorldService.

Test The HelloWorld Service

Now we need to create a test class as the following:
package com.ggl.switchyard_helloworld_demo;

import static java.lang.System.out;
import javax.xml.namespace.QName;

import org.switchyard.remote.RemoteInvoker;
import org.switchyard.remote.RemoteMessage;
import org.switchyard.remote.http.HttpInvoker;

/**
 * Test client which uses RemoteInvoker to invoke a service with an SCA binding.
 */
public final class RemoteClient {

    private static final QName SERVICE = new QName(
            "urn:com.ggl.switchyard_helloworld_demo.HelloWorldService:switchyard-helloworld-demo:1.0",
            "HelloWorldService");
    private static final String URL = "http://localhost:8080/switchyard-remote";

    /**
     * Private no-args constructor.
     */
    private RemoteClient() {
    }

    /**
     * Only execution point for this application.
     * @param ignored not used.
     * @throws Exception if something goes wrong.
     */
    public static void main(final String[] ignored) throws Exception {
        // Create a new remote client invoker
        RemoteInvoker invoker = new HttpInvoker(URL);


        RemoteMessage message = new RemoteMessage();
        message.setService(SERVICE).setOperation("sayHello").setContent("My Dear Earth");

        // Invoke the service
        RemoteMessage reply = invoker.invoke(message);
        if (reply.isFault()) {
            System.err.println("Oops ... something bad happened.  "
                    + reply.getContent());
        } else {
            String greeting = (String) reply.getContent();
            out.println("==================================");
            out.println("Got reply from HelloWorld Service: " + greeting);
            out.println("==================================");
        }
    }


}

Deploy the service and start the runtime. You can right click the the RemoteClient.java and run as java application. You should see the following:
log4j:WARN Continuable parsing error 2 and column 69
log4j:WARN Document root element "log4j:configuration", must match DOCTYPE root "null".
log4j:WARN Continuable parsing error 2 and column 69
log4j:WARN Document is invalid: no grammar found.
==================================
Got reply from HelloWorld Service: Hello My Dear Earth!
==================================

3 comments:

  1. Hi Gary, Thanks for your tutorials and videos very interesting... I was trying to test the hello world sample from the scratch and got some weird error, then I use your git code and have the same:

    The referenced Java interface for service "HelloWorldService" specifies more than one operation and the binding "HelloServiceScaBinging" does not specify which to use in the operation selector.

    Not sure why because the Interface only has one method. The switchyard impl. I am using is 1.1.

    Any Ideas?

    Thanks!

    ReplyDelete

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...