Welcome to the new Gigaspaces XAP forum. To recover your account, please follow these instructions.

Ask Your Question
0

How to combine and execute the tasks?

Hi all, I have a data grid which contains 2 partitions and 2 backups. For example In each space i have 10 customer objects and 100 Call detail objects. i need to calculate the total call amount for each customer.

Initially i read all the customers from the space and pass each customer to a Distributed task(BillingTask). This distributed task will find all the call details made by this customer. The call amount of each call is calculated and summed to total amount. These total amounts are then reduced to determine the total call amount made by a particular customer. This approach iterates through a customer list obtained from the space and pass each customer to the distributed task. This seems to be a sequential and there is no performance improvement if the customer size is more than 1000?

So i thought it would be good to write multiple Distributed Tasks (CustomerTask, Billing Task). In Customer task get the customer list  from each space and then for each customer execute the billing task to find the  total amount.

My question is how to combine these two tasks so that the performance can be improved ? Here i attached the code for Two task classes.

import java.util.Arrays; import java.util.List;

import org.openspaces.core.GigaSpace; import org.openspaces.core.executor.DistributedTask; import org.openspaces.core.executor.TaskGigaSpace;

import com.gigaspaces.async.AsyncResult; import com.j_spaces.core.LeaseContext; import com.j_spaces.core.client.SQLQuery; import com.j_spaces.core.client.UpdateModifiers;

public class BillingTask implements DistributedTask<double,double>{

@TaskGigaSpace
private GigaSpace space;    


public TelecomTask(Customer custom) {

    customer = new Customer(custom);
    customerNo = customer.getCustomerNo();  

}       

@Override
public Double execute() throws Exception {

    Double totalAmount = 0.0;
    CallDetail cd = null;
    List<CallDetail> callList = getCallDetailList();        
    for(CallDetail call : callList){
        cd = new CallDetail(call);
        Double amount = BillingPricer.calculateBilling(call);
        cd.setCallAmount(amount);           
        totalAmount += amount;

        LeaseContext<CallDetail> lea = space.write(cd, 0, 1000, UpdateModifiers.UPDATE_ONLY);
    }           
    return totalAmount; 

}

@Override
public Double reduce(List<AsyncResult<Double>> results)
        throws Exception {

     Double totalAmount = 0.0;
      for(AsyncResult<Double> res: results ){
          if(res.getException() != null)
              throw res.getException();           
          double amount = res.getResult();
          totalAmount += amount;
      }       

     return totalAmount;
}   

private List<CallDetail> getCallDetailList(){

    List<CallDetail> list = null;
    SQLQuery<CallDetail> query = new SQLQuery<CallDetail>(CallDetail.class,"callingPartyNumber='"+ customerNo +"'");
    CallDetail[] prices = space.readMultiple(query, Integer.MAX_VALUE);
    list = Arrays.asList(prices);
    totalCalls = prices.length;
    return list; 
}

private Customer customer; private long customerNo; private int totalCalls; }

public class CustomerTask implements DistributedTask<customer[], customer[]>{<="" p="">

@TaskGigaSpace
private GigaSpace gigaSpace;

public CustTask(){

}

@Override
public Customer[] execute() throws Exception {

    List<Customer> cList = null;
    List<Customer> updateList = new ArrayList<Customer>();
    BillingTask task = null;
    Double customerCallAmount = 0.0;

    cList = getCustomers();
    Customer updateCustomer = null;
    for(Customer c : cList){
        updateCustomer = new Customer(c);
        task = new BillingTask(c);
        AsyncFuture<Double> future = gigaSpace.execute(task);
        if(future != null)
        {
            customerCallAmount = future.get();
            updateCustomer.setTotalAmount(customerCallAmount);
        }
        else
        {
            System.out.println(" No result");
        }           
        updateList.add(updateCustomer);
        updateCustomer = null;
    }

    return updateList.toArray(new Customer[updateList.size()]);
}

@Override
public Customer[] reduce(List<AsyncResult<Customer[]>> results)
        throws Exception {
      List<Customer>  cusList = new ArrayList<Customer>();

      for(AsyncResult<Customer[]> res: results ){
          if(res.getException() != null)
              throw res.getException();

          Customer[] ca = res.getResult();
          for(Customer c : ca)
              cusList.add(c);         
      }
    System.out.println("final Customer List " + cusList);    
    return cusList.toArray(new Customer[cusList.size()]);
}

private List<Customer> getCustomers(){


     SQLQuery<Customer> query = new SQLQuery<Customer>(Customer.class,"id > 0");
     Customer[] cu = gigaSpace.readMultiple(query, Integer.MAX_VALUE);
     List<Customer> list = Arrays.asList(cu);
     //System.out.println("Customer List " + list);
     return list;

    }

}

But Using CustomerTask will not yield the result i needed. There are many CallDetails are not updated. and i dont know how to combine these two tasks. That's y i called the BillingTask in the execute method of the CustomerTask? What is the problem in the CustomerTask implernentation?

Any help in this regard is very much appreciated.

ps: I guess there is no code tags available to post the code. is there any other way to post the code ?

Thanks and Regards Lenin Rajavel

{quote}This thread was imported from the previous forum. For your reference, the original is [available here|http://forum.openspaces.org/thread.jspa?threadID=3123]{quote}

asked 2009-06-05 08:02:03 -0500

updated 2013-08-08 09:52:00 -0500

jaissefsfex gravatar image
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

There is a code tag. It is { code } (remove the spaces). you should have these before and after the code.
See example:

Your code

Shay

answered 2009-06-08 07:57:59 -0500

shay hassidim gravatar image
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2009-06-05 08:02:03 -0500

Seen: 33 times

Last updated: Jun 08 '09