如何在Java App Engine中实现一个原子整数? [英] How to implement an atomic integer in Java App Engine?

查看:202
本文介绍了如何在Java App Engine中实现一个原子整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的应用程式推出自己的简讯验证系统。我不想开始支付一项服务,然后让他们抬起我的价格(城市飞艇对我做推送通知:教训)。在开发和beta测试期间,我一直使用Twilio与一个非常基本的设置:1电话号码。它工作了一年多了,但现在无论什么原因的消息并不总是交付。无论如何,我需要创造一个更好的生产系统。所以我考虑了以下规范:

I am trying to roll out my own SMS verification system for my app. I don’t want to start paying for a service and then have them jack up the price on me (Urban Airship did that to me for push notification: lesson learned). During development and beta testing I have been using Twilio with a very basic setup: 1 phone number. It worked well for over a year, but right now for whatever reason the messages aren’t always delivered. In any case I need to create a better system for production. So I have the following specs in mind:


  1. 600每分钟传送短讯

  2. / li>
  3. 省钱

现在;这意味着我可以处理的最好的是每分钟60快乐用户。那么我如何每分钟获得600个快乐用户?

Right now my Twilio phone number can send one SMS per second; which means the best I can handle is 60 happy users per minute. So how do I get 600 happy users per minute?

所以显而易见的解决方案是使用10个电话号码。但是我如何实现系统?我的服务器是App Engine,DataStore,Java。所以说,我从Twilio购买10个电话号码(更少的当然会更好)。如何实现数组,以便它可以处理来自用户的并发调用?以下是否足够?

So the obvious solution is to use 10 phone numbers. But how would I implement the system? My server is App Engine, DataStore, Java. So say I purchase 10 phone numbers from Twilio (fewer would of course be better). How do I implement the array so that it can handle concurrent calls from users? Will the following be sufficient?

public static final String[] phoneBank = {"1234567890","2345678901","3456789012","4567890123",…}; 
public static volatile nextIndex;

public void sendSMSUsingTwilio(String message, String userPhone){
  nextIndex = (nextIndex+1)%phoneBank.length;
  String toPhone = phoneBank[nextIndex];

  // boilerplate for sending sms with twilio goes here
  //…
}

现在想象1000个用户在同一时间调用这个函数。 nextIndex将从0,1,2 ... 9,0,1 ... 9,0,...连续运行,直到发送所有请求为止?

Now imagine 1000 users calling this function at the very same time. Would nextIndex run from 0,1,2…9,0,1…9,0,… successively until all requests are sent?

真的这是一个并发问题。这个并发问题如何在Java AppEngine上工作?会有交错吗?瓶颈?我希望这个在低预算快速:每分钟至少600。所以我绝对不希望同步在代码本身浪费宝贵的时间。那么如何最佳地同步调用递增 nextIndex ,以便电话号码每个被平等地和以周期性方式调用?同样,这是针对Google App Engine。

So really this is a concurrency problem. How will this concurrency issue work on Java AppEngine? Will there be interleaving? bottlenecking? I want this to be fast on a low budget: At least 600 per minute. So I definitely don’t want synchronization in the code itself to waste precious time. So how do I best synchronize calls to increment nextIndex so that the phone numbers are each called equally and in a periodic fashion? Again, this is for Google App Engine.

推荐答案

您需要使用任务API 。每封邮件都是一项新任务,您可以使用循环或随机分配来分配电话号码。随着任务完成,App Engine将自动拉并执行下一个任务。您可以配置所需的吞吐率(例如,每秒10个),App Engine将为您管理所需的容量。

You need to use Task API. Every message is a new task, and you can assign phone numbers using round-robin or random assignments. As a task is completed, App Engine will automatically pull and execute the next task. You can configure the desired throughput rate (for example, 10 per second), and App Engine will manage the required capacity for you.

您可以尝试实现类似的功能你自己的,但它比你想象的困难得多 - 你必须处理并发,重试,实例关闭,内存限制等。任务API为你做所有这一切。

You can try to implement something similar on your own, but it's much more difficult than you think - you have to handle concurrency, retries, instance shutdowns, memory limits, etc. Task API does all of that for you.

这篇关于如何在Java App Engine中实现一个原子整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆