package async.ejb;

import java.math.BigDecimal;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.inject.Named;

@Named
@Stateless
public class PiBean {

    private static final Logger logger = Logger.getLogger(
            "async.ejb.PiBean");

    @Asynchronous
    public Future<String> computer(int max) {
        String status="计算中...";

      //  System.out.println("come in long operation......");
        logger.info("开始长时间运算");
        /* try {
         Message message = new MimeMessage(session);
         message.setFrom();
         message.setRecipients(
         Message.RecipientType.TO,
         InternetAddress.parse(email, false));
         message.setSubject("Test message from async example");
         message.setHeader("X-Mailer", "JavaMail");

         DateFormat dateFormatter = DateFormat.getDateTimeInstance(
         DateFormat.LONG,
         DateFormat.SHORT);
         Date timeStamp = new Date();
         String messageBody = "This is a test message from the async example "
         + "of the Java EE Tutorial. It was sent on "
         + dateFormatter.format(timeStamp) + ".";
         message.setText(messageBody);
         message.setSentDate(timeStamp);
         Transport.send(message);
         Pi p=new Pi();
         BigDecimal  temp=p.computePi(123456);
         status = "Sent";
         logger.log(Level.INFO, "Mail sent to {0}", email);
         } catch (MessagingException ex) {
         logger.severe("Error in sending message.");
         status = "Encountered an error: " + ex.getMessage();
         logger.severe(ex.getMessage());
         }*/
        Pi p = new Pi();
        BigDecimal temp = p.computePi(max);
        status = "计算完成";
        logger.info(temp.toString());
          logger.info("结束长时间运算");
       // System.out.println("come out long operation......");
        return new AsyncResult<String>(status);
    }
}
