Example usage for java.util.concurrent LinkedBlockingDeque take

List of usage examples for java.util.concurrent LinkedBlockingDeque take

Introduction

In this page you can find the example usage for java.util.concurrent LinkedBlockingDeque take.

Prototype

public E take() throws InterruptedException 

Source Link

Usage

From source file:com.edgenius.wiki.webapp.admin.action.AdvanceAdminAction.java

public String redeployShell() {
    User anonymous = userReadingService.getUserByName(null);

    //in shell side, page request also verify if space exists or not, if not, it will do space request
    //so here won't do space request
    final LinkedBlockingDeque<String[]> pageQ = new LinkedBlockingDeque<String[]>();

    //This will make GW request Shell.key again - so if Shell site cleans the data, re-deploy still can work out.
    //If shell data is still there, it will return same key as GW instanceID and address won't changed.
    Shell.key = null;//from  w w w  .  j  a  v a2  s  . c om

    new Thread(new Runnable() {
        public void run() {
            int pageCount = 0;
            do {
                try {
                    String[] str = pageQ.take();

                    if (StringUtils.equalsIgnoreCase(SharedConstants.SYSTEM_SPACEUNAME, str[0]))
                        break;

                    Shell.notifyPageCreate(str[0], str[1], true);
                    pageCount++;

                    //don't explode the too many concurrent request to Shell!
                    Thread.sleep(1000);
                    if ((pageCount % QUOTA_RESET_COUNT) == 0) {
                        log.warn("Maximumn page shell request count arrived {}, sleep another 24 hours ",
                                QUOTA_RESET_COUNT);
                        //google app engine has quota limitation. Here will sleep 24 hours to wait next quota reset.
                        Thread.sleep(24 * 3600 * 1000);
                        log.warn("Maximumn page shell request sleep is end, restart page request process");
                    }
                } catch (InterruptedException e) {
                    log.error("Thread interrupted for shell request", e);
                }
            } while (true);

            ActivityLog activity = new ActivityLog();
            activity.setType(ActivityType.Type.SYSTEM_EVENT.getCode());
            activity.setSubType(ActivityType.SubType.REDEPLOY_SHELL.getCode());
            activity.setTgtResourceName("SHELL-DEPLOYED");//hardcode
            activity.setCreatedDate(new Date());
            activityLog.save(activity);

            log.info("Shell page request is done for {} pages", pageCount);
        }
    }).start();

    int pageCount = 0;

    long start = System.currentTimeMillis();
    log.info("Shell redeploy request starting...");

    List<Space> spaces = spaceDAO.getObjects();
    if (spaces != null) {
        for (Space space : spaces) {
            if (space.isPrivate() || space.containExtLinkType(Space.EXT_LINK_SHELL_DISABLED)
                    || StringUtils.equalsIgnoreCase(SharedConstants.SYSTEM_SPACEUNAME, space.getUnixName()))
                continue;

            String spaceUname = space.getUnixName();

            List<String> pages = pageDAO.getPagesUuidInSpace(spaceUname);
            if (pages != null) {
                for (String puuid : pages) {
                    if (!securityService.isAllowPageReading(spaceUname, puuid, anonymous))
                        continue;
                    try {
                        pageQ.put(new String[] { spaceUname, puuid });
                        pageCount++;
                    } catch (InterruptedException e) {
                        log.error("Thread interrupted for shell Page Queue", e);
                    }

                }
            }
        }

        log.info("All shell request put into queue. Pages{}; Takes {}s",
                new Object[] { pageCount, (System.currentTimeMillis() - start) / 1000 });
    }

    try {
        pageQ.put(new String[] { SharedConstants.SYSTEM_SPACEUNAME, "" });
    } catch (InterruptedException e) {
        log.error("Thread interrupted for shell Page Queue - end sign", e);
    }

    getRequest().setAttribute("message", messageService.getMessage("redeploy.shell.invoked"));
    return MESSAGE;
}

From source file:com.edgenius.wiki.webapp.action.SpaceAction.java

/**
  * Redeploy shell - request spaceUname as input parameter
  * @return/*from w  ww. ja v a2 s.c o  m*/
  */
public String shellDeploy() {
    securityDummy.checkSpaceAdmin(spaceUname);

    Space space = spaceService.getSpaceByUname(spaceUname);
    if (space.isPrivate() || space.containExtLinkType(Space.EXT_LINK_SHELL_DISABLED)
            || StringUtils.equalsIgnoreCase(SharedConstants.SYSTEM_SPACEUNAME, space.getUnixName())) {
        getRequest().setAttribute("error", "Deploy failed: Space is private or shell service is disabled.");
        return RET_SHELL;
    }

    User anonymous = userReadingService.getUserByName(null);

    final LinkedBlockingDeque<String[]> pageQ = new LinkedBlockingDeque<String[]>();
    //This will make GW request Shell.key again - so if Shell site cleans the data, re-deploy still can work out.
    //If shell data is still there, it will return same key as GW instanceID and address won't changed.
    Shell.key = null;

    new Thread(new Runnable() {
        public void run() {
            int pageCount = 0;

            //in shell side, page request also verify if space exists or not, if not, it will do space request
            //however, if space already exist, it won't refresh space - if space has menu, it won't refresh again.
            //so, we also refresh space 
            Shell.notifySpaceCreate(spaceUname);

            do {
                try {
                    String[] str = pageQ.take();

                    if (StringUtils.equalsIgnoreCase(SharedConstants.SYSTEM_SPACEUNAME, str[0]))
                        break;

                    Shell.notifyPageCreate(str[0], str[1], true);
                    pageCount++;

                    //don't explode the too many concurrent request to Shell!
                    Thread.sleep(1000);
                    if ((pageCount % QUOTA_RESET_COUNT) == 0) {
                        log.warn(
                                "Maximumn page shell request count arrived {}, sleep another 24 hours on space {}.",
                                QUOTA_RESET_COUNT, spaceUname);
                        //google app engine has quota limitation. Here will sleep 24 hours to wait next quota reset.
                        Thread.sleep(24 * 3600 * 1000);
                        log.warn(
                                "Maximumn page shell request sleep is end, restart page request process on space{}.",
                                spaceUname);
                    }
                } catch (InterruptedException e) {
                    log.error("Thread interrupted for shell request", e);
                }
            } while (true);

            log.info("Shell page request is done for {} pages on space {}.", pageCount, spaceUname);
        }
    }).start();

    int pageCount = 0;

    long start = System.currentTimeMillis();
    log.info("Shell on space {} redeploy request starting...", spaceUname);

    List<String> pages = pageService.getPagesUuidInSpace(spaceUname, anonymous);
    if (pages != null) {
        for (String puuid : pages) {
            try {
                pageQ.put(new String[] { spaceUname, puuid });
                pageCount++;
            } catch (InterruptedException e) {
                log.error("Thread interrupted for shell Page Queue on space " + spaceUname, e);
            }
        }
    }

    log.info("All shell on space {} request put into queue. Pages{}; Takes {}s",
            new Object[] { spaceUname, pageCount, (System.currentTimeMillis() - start) / 1000 });

    try {
        pageQ.put(new String[] { SharedConstants.SYSTEM_SPACEUNAME, "" });
    } catch (InterruptedException e) {
        log.error("Thread interrupted for shell Page Queue - end sign", e);
    }

    getRequest().setAttribute("message",
            "Shell is redeploying, this may take a couple of minutes to complete.");

    return shell();
}

From source file:org.commoncrawl.service.listcrawler.DataTransferAgent.java

static Thread startTransferThread(final int threadIndex, final CCBridgeServerMapping mapping,
        final File shutdownFile, final FileSystem fs, final Configuration conf,
        final LinkedBlockingDeque<ProxyTransferItem> itemQueue, final EventLoop eventLoop,
        final Semaphore shutdownSemaphore) {
    Thread thread = new Thread(new Runnable() {

        @Override/*from w  w w.j a  v a 2 s  .  co m*/
        public void run() {
            try {
                while (true) {
                    if (shutdownFile.exists()) {
                        LOG.info("Exiting due to shutdown file existense!");
                        break;
                    }
                    ProxyTransferItem item = itemQueue.take();

                    if (item.hdfsFilePath == null) {
                        LOG.info("Transfer Thread:" + Thread.currentThread().getId() + " Exiting");
                    } else {
                        try {
                            LOG.info("Transfer Thread:" + threadIndex + " for Host:" + mapping._internalName
                                    + " Transferring File:" + item.hdfsFilePath);
                            int result = uploadSingeFile(mapping, fs, conf, item.hdfsFilePath, item.uploadName,
                                    eventLoop);
                            if (result == 200) {
                                LOG.info("Transfer Thread:" + threadIndex + "for Host:" + mapping._internalName
                                        + " Done Transferring File:" + item.hdfsFilePath);
                                //item.logFilePath.createNewFile();
                            } else if (result == 409) {
                                LOG.info("Transfer Thread:" + threadIndex + "for Host:" + mapping._internalName
                                        + " File Already Exists for Path:" + item.hdfsFilePath);
                                //item.logFilePath.createNewFile();
                            } else {
                                LOG.error("Transfer Thread:" + threadIndex + "for Host:" + mapping._internalName
                                        + " File Transfer Failed with Error:" + result + " for Path:"
                                        + item.hdfsFilePath);
                                itemQueue.putFirst(item);
                            }
                        } catch (IOException e) {
                            LOG.error("Transfer Failed for Thread:" + threadIndex + "Host:"
                                    + mapping._internalName + " File: " + item.hdfsFilePath);
                            LOG.fatal(CCStringUtils.stringifyException(e));
                        }
                    }
                }
            } catch (InterruptedException e) {
            } finally {
                shutdownSemaphore.release();
            }
        }

    });
    thread.start();
    return thread;
}