views:

361

answers:

1

Hi,

I have a singleton ejb which is getting initialised twice. I have no idea why and it is completly defenting the point in having a singleton bean as far as I can tell. Any help will be appreciated. As you can see I tried to put a static boolean in to prevent the multiple initialisation (not that it should be required) but it made no difference.

Bean:

@Singleton 
@Startup
public class DataModelBean implements DataModelBeanLocal {

   private static Logger log = Logger.getLogger(DataModelBean.class.getName());

   @PostConstruct
   public void init(){
      log.info(this);           
   }
}

Log output snippet:

2010-02-17 16:06:13,670 INFO  [AutoDeployer        :DataModelBean       ] com.xxx.xxx.datamodel.DataModelBean@117843d
2010-02-17 16:06:14,233 INFO  [AutoDeployer        :DataModelBean       ] com.xxx.xxx.datamodel.DataModelBean@62b9d3

Is it creating 2 beans!! or is it deploying the app twice?

As an aside I am using glassfish v3, is this mature enough? Should I use v2 or something else? Thoughts?

Cheers,

James

A: 

The following singleton:

@Singleton
public class MasterDataCache 
{
    private final static Logger logger = LoggerFactory.getLogger(MasterDataCache.class);

    private Map cache;

    @PostConstruct
    public void initCache() {
        logger.debug("initCache()");
        this.cache = new HashMap();
    }

    public Object get(String key){
        return this.cache.get(key);
    }

    public void store(String key,Object value){
        this.cache.put(key, value);
    }
}

And the following servlet:

@WebServlet(name="SingletonTester", urlPatterns={"/SingletonTester"})
public class SingletonTester extends HttpServlet {

    @EJB
    MasterDataCache masterDataCache;

    @Override
    public void init(){
     masterDataCache.store("startup", new Date());
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            out.println("Startup time: " + masterDataCache.get("startup") );
        } finally {
            out.close();
        }
    }
}

packaged as a war works as expected when deployed "manually" under GFv3. It also deploys fine and works as expected under NetBeans (the initCache is called once only). My only problem is that the deployment fails under Eclipse (GFv3 complains about eclipseApps/$projectName not containing any EJB module, which are however in eclipseApps/$projectName/WEB-INF/classses). Sadly, this seems to be a bug with the GlassFish Eclipse plugin (at least the version I'm using). I don't see many issues in the issue tracker though... weird because this looks like a big blocking one. But outside Eclipse, GFv3 behaves normally, I couldn't reproduce your issue.

Update: I finally got things working under Eclipse and GlassFish v3. I won't give all the details but the problem is that I somehow failed to get my project directly recognized as a "Dynamic Web Module" 2.5, the version was initially set to 2.3 and I think this had something to do with the deployment error on GFv3. After settings up my project correctly (with a facet set to 2.5), deploying it worked fine. So I just screwed up myself.

Pascal Thivent
which version are you using?
vkraemer
@vkraemer I was wrong, classes are copied in `eclipseApps/$projectName/WEB-INF/classses` but glassfish complains with the following exception: `java.lang.RuntimeException: Unable to load EJB module. DeploymentContext does not contain any EJB Check archive to ensure correct packaging for /home/pascal/opt/glassfishv3/glassfish/domains/domain1/eclipseApps/maven-ejb31-testcase`. But as I said, my classes are there. Regarding the version, it seems I'm using com.sun.enterprise.jst.server.sunappsrv (1.0.40).
Pascal Thivent
@vkraemer Actually, the GlassFish Eclipse plugin does work fine. Maybe it could give more meaningful information but I got things working after making my application recognized as a 2.5 dynamic web module.
Pascal Thivent