单引擎在应用程序引擎上的任务队列中的行为 [英] Behavior of singletons in task queues on app-engine

查看:95
本文介绍了单引擎在应用程序引擎上的任务队列中的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当app-engine旋转新实例时,我的静态变量会发生什么变化?更具体地说,我使用的任务队列可以有40个实例/线程。在有问题的Servlet中,我正在使用一个单例,如

  public class WorkerThread extends HttpServlet {
@Override
protected void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {

MySingleton single = MySingleton.getInstance();
..
}
...
}

以下是创建单例的方法:

  public class MySingleton {

public static I MySingleton getInstance (){
return MySingletonHolder.INSTANCE;


private static class MySingletonHolder {
public static final MySingleton INSTANCE = new MySingleton();


私人MySingleton(){

}
..
}

我有以下问题:


  1. 是一个任务队列,我是否必须担心App-Engine启动新实例以满足高要求?


  2. 单身是否是内在的WorkerThread类的类或WorkerThread类正在访问的另一个类?

  3. Task Queues实例是独立的吗?我相信他们是但不确定。


我希望问题清楚。我希望在所有实例中只有一个单例实例。如果问题不清楚,请询问澄清。

更新



是我确切的用例

  public class SingletonProductIndexWriter {

private static final Logger LOG = Logger.getLogger (SingletonProductIndexWriter.class.getName());

public static IndexWriter getSingleIndexWriter(){
return IndexWriterHolder.INDEX_WRITER;


private static class IndexWriterHolder {

static PorterAnalyzer analyzer = new PorterAnalyzer();
static GaeDirectory index = new GaeDirectory(LuceneWorker.PRODUCTS); //创建产品索引
static IndexWriterConfig config = GaeLuceneUtil.getIndexWriterConfig(LuceneWorker.LUCENE_VERSION,analyzer);
public static final IndexWriter INDEX_WRITER = getIndexWriter();


private static IndexWriter getIndexWriter(){
try {
LOG.info(为工作人员创建单个索引编写者);
返回新的IndexWriter(index,config);
} catch(IOException e){
return null;




作为

  IndexWriter writer = SingletonProductIndexWriter.getSingleIndexWriter(); 

有关详细信息,请参阅Stack Overflow主题:

和Pull队列都由标准实例处理(您可以在queue.xml中定位模块/前端/后端)。实例可以扩展以满足您的正常流量以及您的队列的需求。

单点(在这里描绘的经典意义上的)仅仅是类加载器所特有的被加载 - 它们在你的应用程序的appengine实例上绝对不是唯一的 - 没有状态将被共享。使用这种模式,每个appengine实例将有一个单例。



如果您需要共享状态,则需要使用数据存储/云端sql / something。


What happens to my static variables when app-engine spins new instances? More specifically I am using a Task Queue that can have 40 instances/thread. Within the Servlet in question, I am using a singleton, as in

public class WorkerThread  extends HttpServlet {
    @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      ..
      MySingleton single = MySingleton.getInstance();
      ..
    }
    ...
}

Here is how the singleton is created

public class MySingleton {

  public static I  MySingleton getInstance() {
    return   MySingletonHolder.INSTANCE;
  }

  private static class MySingletonHolder {
    public static final MySingleton INSTANCE = new MySingleton();
  }

  private   MySingleton() {

  }
  ..
}

I have the following questions:

  1. Since this is a Task Queue, do I have to worry about App-Engine starting new instances to scale with high demands?

  2. Does it matter if the singleton is an inner class of the WorkerThread class or another class that the WorkerThread class is accessing?

  3. Are Task Queues instance independent? A believe they are but am not sure.

I hope the question is clear. I want there to be only one instance of my singleton across all instances. Please ask for clarification if the question is not clear.

UPDATE

Following is my exact use case

public class SingletonProductIndexWriter {

  private static final Logger LOG = Logger.getLogger(SingletonProductIndexWriter.class.getName());

  public static IndexWriter getSingleIndexWriter() {
    return IndexWriterHolder.INDEX_WRITER;
  }

  private static class IndexWriterHolder {

    static PorterAnalyzer analyzer = new PorterAnalyzer();
    static GaeDirectory index = new GaeDirectory(LuceneWorker.PRODUCTS);// create product index
    static IndexWriterConfig config = GaeLuceneUtil.getIndexWriterConfig(LuceneWorker.LUCENE_VERSION, analyzer);
    public static final IndexWriter INDEX_WRITER =  getIndexWriter();


    private static IndexWriter getIndexWriter() {
      try {
        LOG.info("Create single index writer for workers");
        return new IndexWriter(index, config);
      }catch(IOException e){
        return null;
      }
    }
  }
}

called as

IndexWriter writer = SingletonProductIndexWriter.getSingleIndexWriter();

For pertinent details see Stack Overflow thread: Worker threads cause Lucene LockObtainFailedException

解决方案

Push and Pull queues are both processed by standard instances (you can target a module/frontend/backend in your queue.xml). Instances will scale up to meet the needs of both your normal traffic as well as your queues.

Singletons (in the classic sense portrayed here) are only unique to the classloader they are loaded by - they definitely are not unique across instances of your application on appengine - no state will be shared. Using this pattern you will have one singleton per appengine instance.

If you need to share state, you will need to use the datastore/cloud sql/something.

这篇关于单引擎在应用程序引擎上的任务队列中的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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