2017-02-01 32 views
0

可以在應用引擎標準環境中使用Firebase嗎?我知道標準環境線程功能非常有限,並且由於Firebase SDK運行後臺同步線程,它可能不兼容。我試了一下,我遇到了以下的錯誤,我似乎無法克服:關於應用引擎標準的Firebase Admin SDK

java.security.AccessControlException:access denied(「java.lang.RuntimePermission」「modifyThreadGroup」 )

下面是servlet代碼:

public class GeneratorServlet extends HttpServlet { 

    FirebaseDatabase database = null; 

    @Override 
    public void init(ServletConfig config) { 
//  ClassLoader classloader = Thread.currentThread().getContextClassLoader(); 
//  InputStream serviceAccount = classloader.getResourceAsStream("serviceAccountKey.json"); 

     FirebaseOptions options = new FirebaseOptions.Builder() 
       .setCredential(FirebaseCredentials.applicationDefault()) 
//    .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) 
       .setDatabaseUrl("https://app-name.firebaseio.com/") 
       .build(); 

     FirebaseApp defaultApp = FirebaseApp.initializeApp(options); 

     this.database = FirebaseDatabase.getInstance(defaultApp); 

    } 

    @Override 
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
     PrintWriter out = resp.getWriter(); 
     out.println(this.database); 
     this.loadData(); 
    } 

    private void loadData() { 
//  The following line throws the error 
     DatabaseReference ref = this.database.getReference("/publiclyReadable"); 
    } 
} 

難道我做錯了,或者它被標準環境的限制造成的?我選擇了標準環境而不是靈活性,因爲靈活版本尚不推薦用於生產用途。

謝謝,揚

編輯: AppEngine上-web.xml中

<?xml version="1.0" encoding="utf-8"?> 

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> 
    <application>doctor-appointment-system</application> 
    <version>0</version> 
    <threadsafe>true</threadsafe> 
    <instance-class>B1</instance-class> 
    <manual-scaling> 
     <instances>1</instances> 
    </manual-scaling> 
    <system-properties> 
     <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/> 
    </system-properties> 
</appengine-web-app> 

編輯:

的web.xml

<?xml version="1.0" encoding="utf-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     version="2.5"> 
    <servlet> 
     <servlet-name>generator</servlet-name> 
     <servlet-class>com.example.bookingtimes.GeneratorServlet</servlet-class> 
    </servlet> 
    <servlet> 
     <servlet-name>generatorinfo</servlet-name> 
     <servlet-class>com.example.bookingtimes.GeneratorInfoServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>generator</servlet-name> 
     <url-pattern>/times/generate</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>generatorinfo</servlet-name> 
     <url-pattern>/times/change</url-pattern> 
    </servlet-mapping> 
</web-app> 

的build.gradle:

buildscript { // Configuration for building 
    repositories { 
     jcenter() 
     mavenCentral() 
    } 
    dependencies { 
     classpath 'com.google.cloud.tools:appengine-gradle-plugin:+' 
    } 
} 

repositories { // repositories for Jar's you access in your code 
    maven { 
     url 'https://maven-central.storage.googleapis.com' 
    } 
    jcenter() 
    mavenCentral() 
} 

apply plugin: 'java'        // standard Java tasks 
apply plugin: 'war'        // standard Web Archive plugin 
apply plugin: 'com.google.cloud.tools.appengine' // App Engine tasks 

dependencies { 
    providedCompile group: 'javax.servlet', name: 'servlet-api', version: '2.5' 
    compile 'com.google.appengine:appengine:+' 
    compile 'com.google.firebase:firebase-admin:4.1.0' 
} 

appengine { // App Engine tasks configuration 
    run {  // local (dev_appserver) configuration (standard environments only) 
     port = 8080     // default 
    } 

    deploy { 
     stopPreviousVersion = true // default - stop the current version 
     promote = true    // default - & make this the current version 
    } 
} 

group = 'com.example.appengine' 
version = '0.1' 

sourceCompatibility = 1.7 
targetCompatibility = 1.7 

回答

-3

我切換到App Engine Flexible(這很簡單),現在一切正常。

1

只要您將其置於手動縮放模式,就可以在App Engine標準環境中使用Firebase數據庫客戶端。請參閱documentation on cloud.google.com

我們最近修復了在App Engine上使用SDK時出現的問題(release notes),因此請務必使用最新版本。

+0

感謝您的快速回復。然而,在你的建議(切換到手動縮放並將庫從4.1更新到4.1.1)之後,我仍然收到錯誤消息。該錯誤僅在本地環境中發生,但我想這是因爲它在服務器上被壓制。祝你有美好的一天 –

+0

@JanBeneš:我們的工程師一直在試圖重現這個問題,但不幸的是迄今爲止還沒有成功。你還可以分享你的web.xml,以便我們看到你的GAE處理程序是如何配置的? –

+1

我已將文件添加到原始文章。謝謝 –