I'm maintaining some code that has the following method:
private void loadNotifications()
{
final Map notifications = (Map) getManagedBean("notifications");
...
}
The map is being added to the session and is not serializable, so reconstructing it with a session manager throws an error. The item itself is some legacy code that is a JSF managed bean. If the Map was at the class level, e.g.
public class Notifications
{
final Map notifications;
}
I'd have two options - have the class implement serializable (not desirable here as the parent class has many fields I want in the session, and the action is used a lot) or simply use the transient modifier on the field I want to exclude. However, I can't do that from within the method as the transient modifier is not allowed.
Please let me know if any more detail is required to complete the example - I was mostly wondering if there is a general approach to making transient method variables like I can with class variables.
EDIT:
I've been accused of proposing a solution as the problem. So first, some background: My goal is to store my application's sessions in a memcached-backed Elasticache for high availability purposes. I have set up a session manager to this end, and when I run my application, I get the following error:
Jul 05, 2016 10:19:09 AM de.javakaffee.web.msm.JavaSerializationTranscoder writeAttributes
WARNING: Cannot serialize session attribute notifications for session A79414F801E44C2B8207CEF3532FA80A
java.io.NotSerializableException: com.google.gson.JsonArray
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at java.util.HashMap.internalWriteEntries(HashMap.java:1777)
at java.util.HashMap.writeObject(HashMap.java:1354)
at sun.reflect.GeneratedMethodAccessor141.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1028)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at de.javakaffee.web.msm.JavaSerializationTranscoder.writeAttributes(JavaSerializationTranscoder.java:138)
at de.javakaffee.web.msm.JavaSerializationTranscoder.serializeAttributes(JavaSerializationTranscoder.java:99)
at de.javakaffee.web.msm.TranscoderService.serializeAttributes(TranscoderService.java:154)
at de.javakaffee.web.msm.BackupSessionTask.serializeAttributes(BackupSessionTask.java:180)
at de.javakaffee.web.msm.BackupSessionTask.call(BackupSessionTask.java:110)
at de.javakaffee.web.msm.BackupSessionTask.call(BackupSessionTask.java:51)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
The problem as I have described is thus fairly evident - there is data in my session that cannot be serialized. I have solved similar problems before by making a class implement Serializable or making some fields transient. However, since this case has a method-scope and because it is impractical to make its parent class serializable, my original question was how one might avoid serializing a variable within a method given my context. If someone can propose another solution to my root problem of not being able to store my session/run my app as I intend, then please do.