-2

I'm about to write a MMO, using HTTP-requests that are responsed with JSON.

I was writing it all in Java EE-style, hoping it won't be hard to port to Java EE than. But then I've found out that my static instance variables for a couple of sinletons weren't created properly - classloader made a bunch of them when calling SingletonClass.getInstance() from servlets.

I was totally desperate and thought adding @Singleton descriptions would help. But things weren't so easy. My classes simply not working while adding them with @EJB ClassName var. Context lookup doesn't work either.

I was trying developing in Eclipse, NetBeans, used Glassfish, tried to set it up, but nothing really helped. I do not know what to do and really desperate now.

All I need is just few classes, that work all the time application is loaded to handle game events and hold logged users data (which is distributed in non-EJB objects that hold user data, monsters and so on), some timed events for each logged user and ability to respond to HTTP POST requests with JSON. I even do not need the ORM, I wrote all queries by myself, but still... Something that had to work simply doesn't work out.

I'm aware that all that sounds messy and non-informative, but I do not know what to do - where is my problem? Maybe, I should fill web.xml, or use different port, or fly to the moon? Or just change programming language? Sorry for your time spent reading this.

UPD. Some application scheme parts. First two from package "server".

    @Startup
    @Singleton 
    public class DbWrapper

handles all database connections, DbConnectionPool is non-singleton class, which handles pool of java.sql.Conneciton.

    @Startup
    @Singleton
    @DependsOn("DbWrapper")
    public class World

is yet another class to handle all the in-game events, that holds HashMap of logged users. User and Monster classes are from package "entities" (User holds a list of monsters).

Package "servlets" hold HttpServlet descendants, annotated @WebServlet("/pathname"), that try to use

    @EJB World world

for example. But such things as world.getUser(id_user) simply won't work.

As for JDBC - postgres jar is included in GlassFish domain's /lib.

As for JSON - I use org.json found here: https://github.com/douglascrockford/JSON-java

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Pooh
  • 15
  • 5
  • 2
    sometimes I daydream about giving up coding and taking up yak-farming. – mcfinnigan Apr 23 '12 at 15:03
  • 1
    It sounds like the problem you are having is that the code you wrote to create singleton instances is not behaving as you expect when you deploy to J2EE container. If that is the case, you should rewrite the question emphasizing that, and showing the code that is not behaving as you expect. – antlersoft Apr 23 '12 at 15:03
  • @mcfinnigan - I hear there is good yak farming land on the moon. – Stephen C Apr 24 '12 at 04:58

2 Answers2

0

You should use singletons really rarely (best would be not to use them). As an alternative use application scoped beans (@Singleton beans should normally work - they should use instance variables though, not static ones).

With Java EE 6 you also can use CDI and thus you don't have to use EJBs if you don't need the additional features they provide (like automatic transaction demarcation, security etc.) or can live with adding those features yourself.

Additionally, you can use CDI in a SE application. Keep in mind though, that you need to define the scope for CDI beans (e.g. @Application, @Request etc.) otherwise the default scope (@Dependant) is used which causes the beans to be copied on every access.

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

I've found out that my static instance variables for a couple of singletons weren't created properly - classloader made a bunch of them when calling SingletonClass.getInstance() from servlets.

First, you should show us the code for one of these singleton classes. You may have made a mistake in the implementation that causes this problem.

It is true that you can get what appear to be multiple instances of a (properly implemented) singleton class in a servlet framework. But in fact they are not what they appear to be. What is actually going on is that you have loaded the class from multiple classloaders, either because you have multiple webapps each loading the class, or because you are redeploying your webapp and the previous deployment is not clearing up properly.

So what can / could you do about this?

  • You could use a dependency injection framework to configure your webapp, and hence avoid the need for singleton classes.

  • You could continue using singletons, but track down why you are getting multiple instances, and fix that problem.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Wait. You mean single application can use regular singleton without using all that scary j2ee stuff just by proper deployment? This can be a decision for my problem, a huge shame for me. Can you add some notes on this? – Pooh Apr 23 '12 at 15:17
  • @Pooh - You can always do things the old / simple way using servlets rather than EJBs; e.g. using Tomcat 6. "Can you add some notes on this?" - read the Tomcat 6 documentation, etc. – Stephen C Apr 24 '12 at 07:36