0

是否可以在应用引擎标准环境中使用 Firebase?我知道标准环境线程功能非常有限,并且由于 Firebase SDK 运行后台同步线程,它可能不兼容。我试了一下,遇到了以下错误,我似乎无法克服:

java.security.AccessControlException:访问被拒绝(“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>

构建.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
4

2 回答 2

1

Firebase 数据库客户端可以在 App Engine 标准环境中使用,只要您将其置于手动扩展模式即可。请参阅cloud.google.com 上的文档

我们最近修复了在 App Engine 上使用 SDK 时出现的问题(发行说明),因此请务必使用最新版本。

于 2017-02-01T17:59:01.117 回答
-4

我切换到 App Engine Flexible(这很简单),现在一切正常。

于 2017-02-02T09:47:30.563 回答