我正在使用ServletContextListener来执行后台线程。我设计的这个线程每小时调用一次。我用本地文件进行测试,每件事都正常工作。
但是,当更改为使用google引擎在谷歌云存储中读取/写入文件时,我收到了NullPointerException:
com.google.appengine.api.NamespaceManager.set(NamespaceManager.java:90):在既不是原始请求线程也不是由ThreadManager信息创建的线程中不允许的操作,该线程由ThreadManager INFO at com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService.makeKey(LocalRawGcsService.java:311) INFO at com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService.makeKey(LocalRawGcsService.java:305) INFO at com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService.getObjectMetadata(LocalRawGcsService.java:397) com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService.readObjectAsync(LocalRawGcsService.java:459)信息在com.google.appengine.tools.cloudstorage.PrefetchingGcsInputChannelImpl.requestBlock(PrefetchingGcsInputChannelImpl.java:107)信息在com.google.appengine.tools.cloudstorage.PrefetchingGcsInputChannelImpl.(PrefetchingGcsInputChannelImpl.java:88)信息com.google.appengine.tools.cloudstorage.GcsServiceImpl.openPrefetchingReadChannel(GcsServiceImpl.java:126)信息在com.lottery.base.Utils.getGCPInputStream(Utils.java:458)
函数getGCPInputStream(字符串文件)如下所示:
public static InputStream getGCPInputStream(String file) {
GcsFilename fileName = new GcsFilename(Constant.BUCKET_NAME, file);
GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(fileName, 0, BUFFER_SIZE);
return Channels.newInputStream(readChannel);
}
我还用示例代码进行了测试,它运行得很好。
有人能告诉我我有什么问题吗?我花了很多时间研究,但没有运气。
发布于 2018-08-07 09:37:06
这是由于App标准Java环境中当前的限制。目前,如果您想调用APIs (com.google.appEngin.api.*,根据您提供的堆栈跟踪),您必须从请求线程或使用ThreadManager API创建的线程调用这些API。这是记录在案的这里。
要从后台线程将文件读写到云存储中,您需要通过ThreadManager API创建它。
https://stackoverflow.com/questions/51722802
复制相似问题