1

我正在将 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 上对 play 应用程序进行基准测试,最大堆大小约为 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
                }
            }

        }

    }

}
4

0 回答 0