带有静态值的Google App Engine模块+ HttpServlet;

时间:2015-07-17 00:30:26

标签: java google-app-engine servlets apple-push-notifications

我正在开发一个向Android和iOS设备发送通知的应用程序。我正在使用基本扩展并实现了逻辑(modifying this example),因此在给定时间内有适当数量的工作者处于活动状态而不使用常驻实例。

public class NotificationWorkerServlet extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private static final Logger log = Logger
        .getLogger(NotificationWorkerServlet.class.getName());
private static final int MAX_WORKER_COUNT = 5;
private static final int MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED = 2500;
private static final int TEN_MINUTES = (10 * 60 * 1000);

// Area of concern
private static SyncCounter counter;

/**
 * Used to keep number of running workers in sync
 */
private class SyncCounter {

    private int c = 0;

    public SyncCounter(){
        log.info("Sync counter instantiated");
    }

    public synchronized void increment() {
        c++;
        log.info("Increment sync counter, workers:" + c);

    }

    public synchronized void decrement() {
        c--;
        log.info("Decrement sync counter, workers:" + c);
    }

    public synchronized int value() {
        return c;
    }
}

/**
 * Call made from module when notification was added to task queue
 */
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    super.doPost(req, resp);
    // Instantiate counter with first call
    if(counter == null){
        counter = new SyncCounter();
    }

    log.info("Starting to build workers");

    for (int workerNo = counter.value(); workerNo < MAX_WORKER_COUNT; workerNo++) {
        log.info("Starting thread for worker: " + workerNo);

        // Get the current queue to check it's statistics
        Queue notificationQueue = QueueFactory
                .getQueue("notification-delivery");
        if (notificationQueue.fetchStatistics().getNumTasks() > 30 * workerNo) {
            counter.increment();
            Thread thread = ThreadManager
                    .createBackgroundThread(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                doPolling();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }

                    });

            thread.start();
        } else {
            break; // Current number of threads is sufficient.
        }
    }

    resp.setStatus(HttpServletResponse.SC_OK);

}

/**
 * poll the task queue and lease the tasks
 * 
 * Wait for up to 10 minutes for tasks to be added to queue before killing
 * tasks
 * 
 */
private void doPolling() {
    log.info("Doing pulling");

    try {

        int loopsWithoutProcessedTasks = 0;
        Queue notificationQueue = QueueFactory
                .getQueue("notification-delivery");
        NotificationWorker worker = new NotificationWorker(
                notificationQueue);

        while (!LifecycleManager.getInstance().isShuttingDown()) {
            boolean tasksProcessed = worker.processBatchOfTasks();
            ApiProxy.flushLogs();

            if (!tasksProcessed) {
                log.info("waiting for tasks");

                // Wait before trying to lease tasks again.
                try {
                    loopsWithoutProcessedTasks++;

                    // If worker hasn't had any tasks for 30 min, kill it.
                    if (loopsWithoutProcessedTasks >= (TEN_MINUTES / MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED)) {
                        break;
                    } else {
                        // Else, wait and try again (to avoid tearing down
                        // useful Notification Senders)
                        Thread.sleep(MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED);
                    }

                } catch (InterruptedException e) {
                    log.info("Notification worker thread interrupted");
                    break;
                }
            } else {
                log.info("processed batch of tasks");
                loopsWithoutProcessedTasks = 0;
            }
        }
    } catch (Exception e) {
        log.warning("Exception caught and handled in notification worker: "
                + e.getLocalizedMessage());
    } finally {
        counter.decrement();
    }

    log.info("Instance is shutting down");

}

}

在受控测试场景中,它运行得很好。但是,我知道静态的,可变的值是servlet中的坏消息,其中多个用户可能同时连接。

有没有人做过类似的事情,并且在将多个通知推送到同一台设备,丢失任务或空闲任务烧毁银行漏洞方面存在问题?

0 个答案:

没有答案