简体   繁体   中英

Load Applicationscoped Bean with ServletContext

I have created a class StartupListener implements ServletContextListener. Inside

@Override
public void contextInitialized(ServletContextEvent contextEvent) {

I create a timer which will be executed every 30 seconds. Inside this timer I want to set a parameter from my @ApplicationScoped - Bean in order to display the information from the timer to my website.

Here is what I did so far:

StartupListener.java

package de.whatever;

import java.util.Timer;
import javax.naming.Context;

public class CargoServiceStartupListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent contextEvent) {

    ....        

        ModuleScannerTask task = new ModuleScannerTask(contextEvent.getServletContext());
        Timer timer = new Timer("Scanner");
        timer.schedule(task, 0, 10000);

ModuleScannerTask.java

package de.whatever;

import java.util.Enumeration;

public class ModuleScannerTask extends TimerTask{

private ServletContext context;

public ModuleScannerTask (ServletContext context) {
    this.context = context;
}

@Override
public void run() {
      System.out.println("Start scanning ...");
          ...

ModuleBean.java

package de.whatever;

@ManagedBean (name="modOverview")
@ApplicationScoped
public class ModuleOverviewBean {

private String randomData;

My question is how to set data into randomData field in my application bean.

I read that I could load the attribute "modOverview" from the ServletContext, but that returns null.

What would be the correct way to load and set the values?

I am using JBoss (without spring).

Thanks Hauke

If it returns null then it simply means that JSF hasn't created it yet. Just create one yourself. JSF won't override it when already present in application scope. You just have to be aware that any managed properties of this class won't be set automatically.

ModuleOverviewBean bean = (ModuleOverviewBean) servletContext.getAttribute("modOverview");

if (bean == null) {
    bean = new ModuleOverviewBean();
    servletContext.setAttribute("modOverview", bean);
}

// ...

Unrelated to the problem, the java.util.Timer is a very bad way of doing the background job in a Java EE application. Look at least at ScheduledExecutorService , or since you're already using JBoss, a @Singleton EJB with a @Schedule method.

See also:


Update : if setting of managed properties is also your concern, you could also try to annotate the managed bean with eager=true as follows so that JSF will autocreate it on webapp's startup:

@ManagedBean(name="modOverview", eager=true)

I'm only not entirely sure if this will run before your ServletContextListener runs. I think it will. However, it will definitely work if you're using a @Singleton EJB with @Schedule .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM