@ApplicationScoped life cycle on ear

1

I have an application with several WEB modules and an EJB within an EAR according to the following image:

IntheEJBmoduleIcreatedanannotatedclasswith@ApplicationScoped,mygoalisforthisclasstostoreinformationthatmustbepassedbetweentheWEBprojects.IthappensthatwhenaddinginformationintheclassintheprojectPortalTreinamentosWEBtheyarenotvisibleintheprojectModuleAlunoforexample.

Hereisthecodefortheclassthatisannotatedwith@ApplicationScope

@ApplicationScopedpublicclassServiceAutenticacao{privateMap<String,Colaborador>mapaTickets=newHashMap<String,Colaborador>();publicServiceAutenticacao(){System.out.println("Objeto service Autenticação criado...........................");
}

public void adicionaColaborador(String ticket, Colaborador colaborador){
    this.mapaTickets.put(ticket, colaborador);
}


public Colaborador getColaborador(String ticket) throws ServiceException{
    if(mapaTickets.containsKey(ticket)){
        return mapaTickets.get(ticket);
    }else{
        throw new ServiceException("Ticket de autenticação inválido: " + ticket);
    }
}
}

It follows that I write information on the object of this class in the project PortalTreinamentosWEB:

@ManagedBean
@SessionScoped
public class PrincipalController {

@Inject private ServiceAutenticacao serviceAutenticacao;

public void redirecionaModuloAluno(){
    //pendura o colaborador com um ticket no Servico de Autenticação entre projetos
    String ticketInterno = UtilService.generateOid();
    this.serviceAutenticacao.adicionaColaborador(ticketInterno , this.colaboradorAutenticado);

    HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    String url = req.getRequestURL().toString();
    String urlModuloAluno = url.substring(0, url.length() - req.getRequestURI().length()) + "/ModuloAluno/autenticacao/autenticacao.jsf?ticketinterno="+ticketInterno;

    FacesContext context = FacesContext.getCurrentInstance();

    try {
        context.getExternalContext().redirect(urlModuloAluno);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

And as I try to get this information in the module ProjectAluno:

@ManagedBean
@RequestScoped
public class AutenticacaoController {

@Inject private ServiceAutenticacao serviceAutenticacao;

Colaborador colaborador  = serviceAutenticacao.getColaborador(this.ticketInterno);
            System.out.println("Colaborador: " + colaborador.getNome());
            principalController.setColaboradorAutenticado(colaborador);

I put a System.out in the constructor of class ServiceAutenticacao and realized that it is being instantiated several times. So my question is, is the object annotated with @ApplicationScope the same among several projects of the same EAR? If so, could you tell me why I can not access the information in the other project ( ModuloAluno )

    
asked by anonymous 18.04.2016 / 16:10

1 answer

1

I found this bug / issue on JBoss: link

What it says there is that the operation was not completely specified and the decision that @ApplicationScoped is instantiated once only by EAR (rather than once by WAR) is relatively recent, so it may not be working desired way on the current servers.

In a quick handy, I saw that only in CDI 2.0 we will have this guaranteed (1 instance per EAR) behavior: (

    
18.04.2016 / 17:54