Tuesday, July 6, 2010

Using Defne Framework with Java Server Faces

Using Defne Service Framework with Java Server Faces is very easy. Defne provides "JSFEjbServiceExecutorProxy" managed bean instance to invoke the Defne services. We will see an example just in a moment.

Writing Defne Service

Writing a defne service without dependent on any technology is very very easy. Just annotate your class and  its methods. For example, we create a new Defne service, Calculator.

@Service
public class Calculator{
  
     @Operation 
     public static Message add(Message input) throws DefneException
     {
           int x = input.getMessageParameter(Integer.class,"X");
             int y = input.getMessageParameter(Integer.class,"Y");
 
             input.put("Z", x+y);
            
             return input;
     }
}

That is it! We have written our service with named (default class name) "Calculator" and operation name (default method name) "add".

Call Service From JSF Managed Bean

Now, we will call our service using JSF managed bean. Lets say that we have a JSF CalculatorBean,

@ManagedBean  --> JSF 2.0 fashion
public class CalculatorBean{
      
       @ManagedProperty(value="#{jsfServiceExecutorBean}")
       private JSFEjbServiceExecutorProxy executor; --> Injected by the Defne

       private String  x;
       private String y;

       public String addNumbers(){

           Message input = new Message("Calculator","add");
           input.putMessageParameter("X",x);
           input.putMessageParameter("Y",y);
           input.setTransactionPolicy(TransactionPolicy.NO_TRANSACTION);          

           Message output =  executor.execute(input);
           System.out.println(output.getMessageParameter(Integer.class,"Z"));

            return null;
      }
}

That is it!

Get more information on Defne Service Framework,
click http://code.google.com/p/defne/

No comments: