Initializing Glassfish Singleton Bean Twice

I have a singleton ejb that is initialized twice. I have no idea why, and he completely defends the point that I can use a singleton bean, as far as I can tell. Any help would be appreciated. As you can see, I tried to include a static Boolean value to prevent multiple initialization (not that it was necessary), but it did not matter.

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 of the output fragment:

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

Does he create 2 beans !! or does he deploy the application twice?

How aside do I use glassfish v3, mature enough? Should I use v2 or something else? Thoughts?

+3
source share
2

:

@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);
    }
}

:

@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();
        }
    }
}

, , , "" GFv3. , , NetBeans (initCache ). , ​​Eclipse (GFv3 eclipseApps/$projectName, EJB, , , eclipseApps/$projectName/WEB-INF/classses). , , , GlassFish Eclipse ( , , ). -, ... , . Eclipse GFv3 , .

: - Eclipse GlassFish v3. , , - , " -" 2.5, ​​ 2.3, , - GFv3. ( 2,5), . .

+1

, ejb-jar.xml EJB EJB.
:

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

GF beans JNDI ( "java: global/MySingletonBean" "java: global/MyTestSingletonBean" ). : - MySingletonBean, - MyTestSingletonBean.
, @Resource , GF:
1) GF "MySingletonBean" ;
2) GF "MyTestSingletonBean" ( ).
GF 3.1 build 12 @Singleton @Startup .

+1

Source: https://habr.com/ru/post/1733222/


All Articles