SwitchYard JSF Integration tutorial

In this tutorial we will discuss about JSF integration with SwitchYard using the Order example contained in the distribution.

The Bean Component is the core SwitchYard component. Built as an extension to Weld component it allows you to use the same programming model of CDI beans, with a few extra annotations. By adding Switchyard annotations you are able to expose your existing CDI beans as services to the outside world, or consume as well services from your beans.

How to expose SwitchYard Services ?

Providing a service using Switchyard's Bean component just requires adding a @Service annotation to your bean.


import org.switchyard.component.bean.Service;

 

@Service(Simple.class)

public class SimpleService implements Simple {

    public void doSomething() {

       .....

    }

}

How to consume a SwitchYard Service from a CDI Bean?

In order to consume a SwitchYard service from within a CDI bean you need to apply a @Reference annotation to it. Supposing that we want to use from our SimpleService an external Service:


import org.switchyard.component.bean.Service;

import org.switchyard.component.bean.Reference;

 

@Service(Simple.class)

public class SimpleService implements Simple {

   @Inject @Reference ExternalService extService;

    public void doSomething() {

        extService.execute();

    }

}

The reference can point to any service hosted outside of Switchyard, for example an external JMS Provider.

The reference to the external service via @Reference annotation, is not a reference to the actual Service implementation. Instead, it is an Exchange proxy that is provided by Switchyard to contact  that Service implementation.

So let's see a concrete example of a JSF application which will use SwitchYard Service. This application is contained in Switchyard distribution and you can check for more information about it on GitHub: https://github.com/jboss-switchyard/quickstarts/tree/master/demos/orders

So we will start from the view which is a simple JSF pages referencing some attributes and the create method of the Order bean


<div id="content">
    <h1>New Order</h1>
 
    <div style="color: red">
       <h:messages id="messages" globalOnly="false" />
    </div>
 
    <h:form id="newOrder">
        <div>
            Order ID:
            <h:inputText id="orderID" value="#{order.orderId}" required="true"/>
            <br/>
            Item ID:
            <h:inputText id="itemID" value="#{order.itemId}" required="true"/>
            <br/>
            Quantity:
            <h:inputText id="quantity" value="#{order.quantity}" required="true"/>
            <p/>
            <h:commandButton id="createOrder" value="Create" action="#{order.create}"/>
        </div>
    </h:form>
 
</div>

The Order bean is a plain CDI bean annotated with @Named which is used to mediate between the JSF page and the SwitchYard Service:


@Named
@RequestScoped
public class Order implements Serializable {
 
    @Inject
    @Reference
    private OrderService orderService;
 
    private String orderId;
    private String itemId;
    private int quantity = 1;
 
    // Getters and Setters here
 
    public void create() {
        OrderAck serviceAck = orderService.submitOrder(this);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(serviceAck.toString()));
    }
}

Notice the @Reference annotation which is used to consume the SwitchYard service.

More interesting for us is the OrderServiceBean which contains the Service to be exposed via JSF. It uses in turn another Service named InventoryService which barely contains a Collection of Items which can be ordered:


@Service(OrderService.class)
@ApplicationScoped
public class OrderServiceBean implements OrderService {
    
    @Inject @Reference
    private InventoryService _inventory;
    
    @Override
    public OrderAck submitOrder(Order order) {
        // Create an order ack
        OrderAck orderAck = new OrderAck().setOrderId(order.getOrderId());
        // Check the inventory
        try {
            Item orderItem = _inventory.lookupItem(order.getItemId());
            // Check quantity on hand and generate the ack
            if (orderItem.getQuantity() >= order.getQuantity()) {
                orderAck.setAccepted(true).setStatus("Order Accepted");
            } else {
                orderAck.setAccepted(false).setStatus("Insufficient Quantity");
            }
        } catch (ItemNotFoundException infEx) {
            orderAck.setAccepted(false).setStatus("Item Not Available");
        }
        return orderAck;
    }

}

Here's a picture which summarizes the relation between the components:

switchyard tutorial jboss as 7 switchyard tutorial jboss as 7

 

Follow us on Twitter