在playframework2.0中使用akka promise / future的例外情况

时间:2012-05-17 03:21:33

标签: mysqli playframework-2.0 akka

我正在将play framework 2.0用于一个简单的应用程序,该应用程序从MySQL数据库返回json字符串中的员工列表。我的控制器如下

public class Application extends Controller {

 public static Result getEmployees() {
        Logger.info("enter getting employee information");
        Promise <List<Employee>> employeeList = Akka.future(new     Callable<List<Employee>>(){
            public List<Employee> call() {
                return Employee.getAll();

            }
        });

        return async(
                  employeeList.map(
                  new Function<List<Employee>,Result>() {
                    @Override
                    public Result apply(List<Employee> employeeList)
                             {
                        // TODO Auto-generated method stub
                      try { 
                        if (employeeList!=null) {
                            return ok(Json.toJson(employeeList));
                        } else {
                            return ok("");
                        }
                      } catch (Exception e) {
                          return ok("");
                      }
                    } 
                  }
                )
         );

  }
}

模型类如下。

public class Employee extends Model{


    @Id
    public Long id;

    @Required
    public String name;

    @Required 
    public Float age;



       public static Model.Finder<Long,Employee> find = new Model.Finder<Long, Employee>(Long.class, Employee.class);

        @Transactional(readOnly = true)
    //    @Cached(key = "alllist")
        public static List<Employee> getAll() {


            //return JPA.em("default").find(Employee.class, 1L);

            Logger.info("Enter getAll");
            if (Cache.get("allList")!=null){
                Logger.info("cache is not null");
                return (List<Employee>) Cache.get("allList");
            }
            EntityManager entityManager = JPA.em("default");
            Query query = entityManager.createQuery("select m from Employee m", Employee.class);
            List<Employee> data = query.setFirstResult(0).getResultList();
            Logger.info("setting cache");
            Cache.set("allList", data);
            entityManager.close();

            return data;



    //      if (find!=null)
    //          return find.all();
    //      else 
    //          return null;
        }


    }

我开始在生产模式下使用apache基准测试在我的macbook上标记播放应用程序,最大堆大小为1500 MB。

我注意到的一些事情是增加请求的并发性会增加平均响应时间和更高的并发率。

ab -n 100 -c 10 http://localhost:9000/api/employees/

我收到以下错误

[error] play - Waiting for a promise, but got an error: null
java.lang.RuntimeException: null

我的Akka特定配置是。

play {

    akka {
        event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
        loglevel = WARNING

        actor {

            deployment {

                /actions {
                    router = round-robin
                    nr-of-instances = 100
                }

                /promises {
                    router = round-robin
                    nr-of-instances = 100
                }

            }

            retrieveBodyParserTimeout = 4 second

            actions-dispatcher = {
                fork-join-executor {
                    parallelism-factor = 1
                    parallelism-max = 100
                }
            }

            promises-dispatcher = {
                fork-join-executor {
                    parallelism-factor = 1
                    parallelism-max = 100
                }
            }

            websockets-dispatcher = {
                fork-join-executor {
                    parallelism-factor = 1
                    parallelism-max = 100
                }
            }

            default-dispatcher = {
                fork-join-executor {
                    parallelism-factor = 1
                    parallelism-max = 100
                }
            }

        }

    }

}

提前感谢您的帮助

0 个答案:

没有答案