Spring异步监听器而不是线程?

时间:2011-04-06 10:37:29

标签: spring

我有一个线程来处理我的请求:基本上当我创建一个请求时,我找到了特定请求提供的服务。我正在使用thread.I想要摆脱线程并使用spring listeners和task executor(Can I)我怎么能这样做。?任何一个例子

public class FinderServiceImpl implements FinderService, Runnable {


    private RequestManager requestManager;
    private boolean continueRunning = true;
    private Thread service; 

    public RequestManager getRequestManager() {
        return RequestManager;
    }

    public void setRequestManager(
            RequestManager requestManager) {
        this.requestManager = requestManager;
    }

    public FinderServiceImpl() {
        service = new Thread(this);
        service.start();
    }

    public void run() {
        while (continueRunning) {
            try {
                synchronized (service) {
                    service.wait();
                }

            } catch (InterruptedException e) {
                LOG.error("Error in finder service: " + e);

            }

            if (requestManager != null) {
                LOG.debug("CALLING the RequestManager");
                requestManager.findForAllRequest();
                LOG.info("Finished Finding the  Offer. !!");
            }
        }
    }


    @Override
    public void notifyFinder()  {
        LOG.debug(" Notifier is called  the Service to Find ");
        synchronized (service) {
            service.notify();
        }
    }

    @Override
    public void scheduledFinding() {

        synchronized (service) {
            service.notify();
        }
    }


    @Override
    public void startService() {
        continueRunning = true;

        if (!service.isAlive()) {
            LOG.debug("Starting the finder service");
            service = new Thread(this);
            service.start();
        }
    }


    @Override
    public void stopService() {
        LOG.debug("Stopping the finder service");
        this.continueRunning = false;
    }
}

我有一个requestmanager类。

Class RequestManager{

private FinderService OfferFinder;
public int createRequest(Request Request){
// create my request
OfferFinder.notifyFinder();


}

0 个答案:

没有答案