package async.web;

import async.ejb.PiBean;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class CountManagedBean {
    private static final Logger logger = Logger.getLogger(
                "async.web.CountManagedBean");
    @EJB
    protected PiBean pb;
    protected int max;

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }
    protected String status;

    /** Creates a new instance of MailerManagedBean */
    public CountManagedBean() {
    }

    /**
     * Get the value of status
     *
     * @return the value of status
     */
    public String getStatus() {
        return status;
    }

    /**
     * Set the value of status
     *
     * @param status new value of status
     */
    public void setStatus(String status) {
        this.status = status;
    }

    
    public String begin() {
        String response = "response";

        try {
          //  System.out.println("come in......");
            logger.info("开始调用异步方法");
            Future<String> re = pb.computer(max);
              logger.info("结束调用异步方法");
          //    System.out.println("come out......");
            try {
                if(re.isDone())
                this.setStatus(re.get());
            } catch (ExecutionException ex) {
                this.setStatus(ex.getCause().toString());
            }

          
        } catch (Exception ex) {
            logger.severe(ex.getMessage());
        }

        return response;
    }
}
