13

I have the following managed beans :

@ApplicationScoped
public class ApplicationBean {
    // ...
}
@SessionScoped
public class SessionBean implements Serializable {

    @ManagedProperty("#{applicationBean}")
    private ApplicationBean applicationBean;

    // ...
}

This is deployed to a server cluster with several nodes. What will happen when the HTTP session will be serialized on another node?

ApplicationBean is not serialized because it doesn't implement Serializable. Will it be re-injected by @ManagedProperty? Or will it actually be serialized somehow?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3008066
  • 131
  • 1
  • 3

2 Answers2

13

What will happen when the HTTP session will be serialized on another node?

All HTTP session attributes will be serialized as well, including session scoped JSF managed beans. Any bean properties which are not serializable will be skipped. During deserialization on another node, you will face a NotSerializableException on all bean properties which are not serializable. Marking the property transient will fix that exception, but the property will still remain null after deserialization.


Will it be re-injected by @ManagedProperty? Or will it actually be serialized somehow?

Nope. It won't be re-injected. You have to take care of this manually in case of @ManagedProperty.

One somewhat naive and error-prone way is getting rid of @ManagedProperty and performing lazy loading in the getter (thus, act like a proxy yourself):

private transient ApplicationBean applicationBean;

public ApplicationBean getApplicationBean() {
    if (applicationBean == null) { 
        FacesContext context = FacesContext.getCurrentInstance();
        applicationBean = context.getApplication().evaluateExpressionGet(context, "#{applicationBean}", ApplicationBean.class);
    }

    return applicationBean;
}

and use the getter throughout the code instead referencing the property directly.

The better way is to make it an EJB or a CDI managed bean. They're fully transparently created and injected as serializable proxies and you never need to worry about serialization on them.

Thus, either make it an EJB:

import javax.ejb.Singleton;

@Singleton
public class ApplicationBean {
    // ...
}
import javax.ejb.EJB;
import.javax.faces.bean.ManagedBean;
import.javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class SessionBean implements Serializable {

    @EJB
    private ApplicationBean applicationBean;

    // ... (no setter/getter necessary!)
}

Or, make them both CDI managed beans:

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named
@ApplicationScoped
public class ApplicationBean {
    // ...
}
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@SessionScoped
public class SessionBean implements Serializable {

    @Inject
    private ApplicationBean applicationBean;

    // ... (also here, no setter/getter necessary!)
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

I'm facing similar problem.

I have a Quartz scheduler that runs each x seconds.

Quartz's Job updates a list of items in my application scoped bean.

I get the application bean with the static method show below.

When I deploy the app it run fine but when a user enter in the web app then FacesContext.getCurrentInstance() always return null.

    public static <T> T getBean(final String beanName, final Class<T> beanClass) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        T result = null;
        if (facesContext != null) {
            Application application = facesContext.getApplication();
            if (application != null) {
                String expresionBeanName = "#{" + beanName + "}";
                result = application.evaluateExpressionGet(facesContext, expresionBeanName, beanClass);
            }
        }
        return result;
    }

    @ManagedBean
    @ApplicationScoped
    public class ApplicationBean implements Serializable { ... }