Problems using firebase admin sdk with tomcat

150 views
Skip to first unread message

Andrea Murru

unread,
Oct 12, 2023, 12:21:14 PM10/12/23
to Firebase Google Group
Hi all,
Is anyone successfully using firebase admin sdk with apache tomcat?
I'm experiencing big problems, probably related to the internal messaging system (io.netty): after the call to FirebaseApp.initializeApp (which "apparently" ends correctly) every (synchronous) call to the API remains blocked, while asynchronous events never arrive.
If I start a console application with the same Java version on the same server with the same service_account_file, all works fine, therefore we can exclude authentication (or network) problems.  

I'm using Java 11 (OpenJDK) and Tomcat 9.0.80, firebase-admin ver. 9.2.0 on a linux server
I put the initialization code in my 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://caasa.firebaseio.com/")
              .build();
FirebaseApp.initializeApp(options);
org.apache.log4j.Logger.getLogger(CaasaAdminServletListener.class).info("[init] FirebaseApp.initializeApp OK");

And then, I (try to) use firebase in one servlet class, but without success. An example of my code that stucks:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference().child(key).addListenerForSingleValueEvent(listener);

further notes
*) I tested my code on a console application on the same server and it works fine;
*) I've tried initializing firebase also in a static block, in the servlet.init method or simply before using the API, but nothing changes.
*) If I look at the debug firebase library prints at initialization, I can see that the main difference with the (working) console test app is netty messaging system initialization is missing (nothing is printed after the line: ---firebase-database-worker io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false)
Here the log I obtain in the working case:
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
.... 

Many thanks in advance to anyone who can reply.

Andrea Murru
Reply all
Reply to author
Forward
0 new messages