JPA and Threads in play framework

后端 未结 4 1965
Happy的楠姐
Happy的楠姐 2021-01-11 18:07

I\'m trying to create a multithreading server. The problem is that I get the following error: play.exceptions.JPAException: The JPA context is not initialized. JPA Entit

4条回答
  •  青春惊慌失措
    2021-01-11 18:34

    Don't use plain thread, use jobs instead :

    @OnApplicationStart 
    public class MainLoop extends Job { 
           public void doJob() { 
                   new BallJob().now(); 
           }
    } 
    

    And BallJob :

    public class BallJob extends Job {
    public void doJob() {
        List balls;
        new Ball(5,5,2,2,10,15);
        while (true){
            balls = Ball.all().fetch(); 
            for (Iterator iterator = balls.iterator(); iterator.hasNext();) {
                Ball ball = (Ball) iterator.next();
                ball.applyForces();
            }
        }
    }
    

提交回复
热议问题