יום שישי, 9 במאי 2014

Camel asyncRequestBody

A simple sample of using Camel asyncRequestBody method .
The sample is based on the Camel: Camel :: Example :: Cafe 2.12.1 sample.
The main :

 public void runCafeTableFinder () throws Exception {      
        DefaultCamelContext camelContext = new DefaultCamelContext();
        camelContext.setRegistry(createRegistry());
        camelContext.addRoutes(this);
        camelContext.start();
        
        ProducerTemplate template = camelContext.createProducerTemplate();
       
        Guest guest = new Guest (4);       
        
        Future<CafeTable> future = template.asyncRequestBody("direct:cafeEnterence", guest , CafeTable.class);
        
        CafeTable response = future.get(1, TimeUnit.MINUTES);
        
        System.out.println ("The table :"  + response.toString());
        
        camelContext.stop();
     }
The sample use JNDI in order to cache beans.
The hostes that finds table code:
public class hostess {
    public CafeTable getTable (Guest pGuest)
    {
       System.out.println ("No of people  :" + Integer.toString(pGuest.getmNoOfP()));
        
        return new CafeTable ("Not in the smoking Zone");
    }
}

The registration in the JNDI :

  protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry jndi = new JndiRegistry();     
        jndi.bind("hostess", new hostess());
        return jndi;
    }

The table and the guests classes are simple pojos:

public class Guest {
    
    private int mNoOfP;
    public int getmNoOfP() {
        return mNoOfP;
    }
    public void setmNoOfP(int mNoOfP) {
        this.mNoOfP = mNoOfP;
    }
    
    public Guest(int pNoOfP) {
        mNoOfP = pNoOfP;
    }
    
}

and  the CafeTable pojo

public class CafeTable {
    private String mLocation = "";
    public CafeTable (String pLocation)
    {
        mLocation = pLocation;
    }
    @Override
    public String toString() {
        return "CafeTable{" + "mLocation=" + mLocation + '}';
    }
}

The result:
No of people  :4
The table :CafeTable{mLocation=Not in the smoking Zone}

אין תגובות:

הוסף רשומת תגובה