将 firebase java admin sdk 与 tomcat 配合使用时出现的问题

Problems using firebase java admin sdk with tomcat

提问人:Andrea Murru 提问时间:10/15/2023 最后编辑:Frank van PuffelenAndrea Murru 更新时间:10/15/2023 访问量:37

问:

是否有人成功将 java firebase admin sdk 与 apache tomcat 一起使用? 我遇到了大问题,可能与 firebase sdk 使用的内部消息传递系统 (io.netty) 有关:初始化 Firebase 后(FirebaseApp.initializeApp() “显然”正确结束),每个 firebase API 调用都会卡住。 如果我在具有相同service_account_file的同一服务器上启动具有相同 Java 版本的控制台应用程序,则一切正常,因此我们可以排除身份验证(或网络)问题。

我在 linux 服务器上使用 Java 11 (OpenJDK)、Tomcat 9.0.80 和 firebase-admin 9.2.0。 我将 firebase 的初始化代码放在我的 ServletContextListener.contextInitialized() 中:

org.apache.log4j.Logger.getLogger(CaasaAdminServletListener.class).info("[init]");
String serviceAccountKey = sce.getServletContext().getRealPath("/WEB-INF/firebase.json");
org.apache.log4j.Logger.getLogger(CaasaAdminServletListener.class).info("[init] serviceAccountKey= "+serviceAccountKey);
FileInputStream serviceAccount = new FileInputStream(serviceAccountKey);            
FirebaseOptions options = new FirebaseOptions.Builder()
              .setCredentials(GoogleCredentials.fromStream(serviceAccount))
              .setDatabaseUrl("https://mydatabase.firebaseio.com/")
              .build();
FirebaseApp.initializeApp(options);
org.apache.log4j.Logger.getLogger(CaasaAdminServletListener.class).info("[init] FirebaseApp.initializeApp OK");

初始化后(在我的 Web 应用程序部署时调用一次),当我尝试在我的 servlet 中使用任何 firebase API(同步或异步)时,它会卡住(或超时)。 这是我在一个简单的servlet中的代码示例(在processRequest方法中):

final FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference().child(key).addListenerForSingleValueEvent(listener);

我认为我在基于 Tomcat 如何管理库(尤其是静态对象)的加载和不同上下文之间的共享,在 webapp 生命周期中初始化库时做错了什么。

请注意:

*) 我还在同一台服务器上的控制台应用程序上测试了我的代码,它工作正常:我能够访问我的数据库并使用同步和异步 API 读取或设置值 *) 我尝试在静态块中、servlet.init 方法中或只是在使用 API (processRequest) 之前初始化 firebase,但没有任何变化。 *) 在控制台应用程序(工作)和 tomcat webapp(不工作)之间比较来自 firebase lib 的调试日志 我注意到与io.netty lib的内部消息传递系统初始化相关的消息完全丢失。以下是我在这两种情况下获得的调试日志的常见(初始)部分:

12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.logging.InternalLoggerFactory - Using Log4J as the default logging framework
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent0 - Java version: 11
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.storeFence: available
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent0 - direct buffer constructor: unavailable: Reflective setAccessible(true) di
sabled
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable: class io.netty.util.internal.PlatformDependent0$7 cannot access class jdk.internal.misc.Unsafe (in module java.base) because module java.base does not export jdk.internal.misc to unnamed module @6c382782
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, {int,long}): unavailable
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent - maxDirectMemory: 7677673472 bytes (maybe)
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: /tmp (java.io.tmpdir)
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: -1 bytes
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.CleanerJava9 - java.nio.ByteBuffer.cleaner(): available
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false

而我只能在工作案例(控制台应用程序)中读取以下日志:

12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 16
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.concurrent.GlobalEventExecutor - -Dio.netty.globalEventExecutor.quietPeriodSeconds: 1
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.handler.ssl.OpenSsl - netty-tcnative not in the classpath; OpenSslEngine will be unavailable.
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.handler.ssl.JdkSslContext - Default protocols (JDK): [TLSv1.3, TLSv1.2]
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.handler.ssl.JdkSslContext - Default cipher suites (JDK): [TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384]
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.channel.DefaultChannelId - -Dio.netty.processId: 17423 (auto-detected)
12 10 2023 10:27:41 DEBUG firebase-database-worker io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
.... 
java tomcat firebase-realtime-database firebase-admin

评论


答: 暂无答案