Problems with use Spring's ThreadPoolTaskExecutor

1,288 views
Skip to first unread message

曹江华

unread,
Feb 2, 2010, 10:06:36 AM2/2/10
to play-framework
1) i'm create a model class named PushInfo.
@Entity(name="pushinfo")
public class PushInfo extends Model {
public Integer cid;
public String cname;
public String cupdate;
public String cnote;
public String msgnote;
}
2) i'm create a application-context.xml in to conf directory.
<?xml version="1.0" encoding="utf-8"?>

    <bean id="pushService" class="service.PushService">
        <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    </bean>

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="5" />
        <property name="maxPoolSize" value="10" />
        <property name="queueCapacity" value="25" />
    </bean>
</beans>

3) i'm create a class named PushService in to app/service directory.
package service;

import models.PushInfo;
import org.springframework.core.task.TaskExecutor;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: arden
 * Date: 2010-2-2
 * Time: 21:41:35
 * To change this template use File | Settings | File Templates.
 */
public class PushService {
    private TaskExecutor taskExecutor;

    public void setTaskExecutor(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }

    public void push() {
        System.out.println("-------------------1");
        this.taskExecutor.execute(new PushServiceTask());
        System.out.println("-------------------2");
    }

    private class PushServiceTask implements  Runnable {
        public void run() {
            // there are some errors when call this line.
            List<PushInfo> pushInfos = PushInfo.all().fetch();
            try {
                if (pushInfos.size() > 0 ) {
                    for (PushInfo push : pushInfos) {
                        System.out.println("========" + push.cname);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(5 * 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("==========================3");
        }
    }
}

4) then i'm call PushService in my Controller.
package controllers;

import com.jrails.commons.utils.StringUtils;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import play.modules.spring.Spring;
import play.mvc.Controller;

import java.util.Calendar;

/**
 * Created by IntelliJ IDEA.
 * User: arden
 * Date: 2010-1-30
 * Time: 14:20:53
 * To change this template use File | Settings | File Templates.
 */
public class Window extends Controller {
    public static void push() {
        try {
            //PushService pushService = (PushService)Spring.getService("pushService");
            PushService pushService = Spring.getBeanOfType(PushService.class);
            pushService.push();  // have errors
        } catch (Exception e) {
            e.printStackTrace();
        }
        renderText("hello");
    }
}

5) and then type "http://localhost:9000/push/push" on my Chrome.now there are some errors like this:
Exception in thread "threadPoolTaskExecutor-2" play.exceptions.JPAException: The
 JPA context is not initialized. JPA Entity Manager automatically start when one
 or more classes annotated with the @javax.persistence.Entity annotation are fou
nd in the application.
        at play.db.jpa.JPA.get(JPA.java:19)
        at play.db.jpa.JPA.em(JPA.java:52)
        at play.db.jpa.JPASupport.em(JPASupport.java:452)
        at models.PushInfo.all(PushInfo.java)
        at service.PushService$PushServiceTask.run(PushService.java:31)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec
utor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:908)
        at java.lang.Thread.run(Thread.java:619)


--
http://www.caojianghua.com

Guillaume Bort

unread,
Feb 2, 2010, 10:23:17 AM2/2/10
to play-fr...@googlegroups.com
Ok well,

Play is a managed environment. It loads, reloads and executes your
code. It's mainly based on the concept of HTTP requests (an HTTP
request = a play invocation) and allows asynchronous tasks using Jobs
(A job = a play invocation).

So you can't use spring to rebuild your own framework inside of play
and just hope that all will go well. If you want really build a spring
based application then just use spring. Otherwise try to think your
application in the play way (HTTP requests and aynschronous Jobs) and
all will be fine.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.
>

曹江华

unread,
Feb 2, 2010, 11:02:43 AM2/2/10
to play-fr...@googlegroups.com
Thanks very much, this problem have resolved. use Play! job instead of the Spring's ThreadPoolTaskExecutor.

2010/2/2 Guillaume Bort <guillau...@gmail.com>
Ok well,

Play is a managed environment. It loads, reloads and executes your
code. It's mainly based on the concept of HTTP requests (an HTTP
request = a play invocation) and allows asynchronous tasks using Jobs
(A job = a play invocation).

So you can't use spring to rebuild your own framework inside of play
and just hope that all will go well. If you want really build a spring
based application then just use spring. Otherwise try to think your
application in the play way (HTTP requests and aynschronous Jobs) and
all will be fine.



--
http://www.caojianghua.com
Reply all
Reply to author
Forward
0 new messages