문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

This issue happens when in ejb-jar.xml EJB name not equals EJB class name.
For example:

<ejb-name>MySingletonBean</ejb-name>
<ejb-class>ru.rozge.MyTestSingletonBean</ejb-class>

In this case GF registers two beans with different JNDI names ("java:global/MySingletonBean" and "java:global/MyTestSingletonBean"). And then in initialization it create two objects, first - MySingletonBean and second - MyTestSingletonBean.
Also if you use @Resource annotation for injection resources, GF actions:
1) GF creates first object "MySingletonBean" and correctly injected resources in it;
2) GF creates second object "MyTestSingletonBean" and fails on injecting (all injected resources equals null or default values).
Tested on GF 3.1 build 12 with @Singleton @Startup combo.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top