1

We all use simple Java classes without any annotation. When we use it in normal standalone application, we use 'New' keyword to create instance and use it.The object is created on heap. If its not instantiated i can still access or use its static members.

My question is, if i deploy this simple class to EJB container, then what happens to it? I have not annotated it Stateless or Stateful or Entity, so how container manages it. Below is sample code. The POJO here (ClientCounter) does nothing special but is just for example:

@Stateless 
public class WelcomeBean implements WelcomeBeanRemote {
    private ClientCounter pojo = new ClientCounter();

    @Override
    public void showMessage() {
        System.out.println("welcome client");
        pojo.increment();
    }
}

class ClientCounter {
    private int count;

    public void increment() {
         count++;
    }
}

And the client is:

public class Client {

    public static void main(String []args) {
        Properties jndiProps = new Properties();
        jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");
        jndiProps.put(Context.PROVIDER_URL,"http-remoting://localhost:8080");
        jndiProps.put("jboss.naming.client.ejb.context", true);
        jndiProps.put(Context.SECURITY_PRINCIPAL, "admin");
        jndiProps.put(Context.SECURITY_CREDENTIALS, "admin");

        final String appName = "";
        final String moduleName = "EJBProject02";
        final String sessionBeanName = "WelcomeBean";
        final String viewClassName = WelcomeBeanRemote.class.getName();

        Context ctx = new InitialContext(jndiProps);
        WelcomeBeanRemote bean =(WelcomeBeanRemote) ctx.lookup(appName+"/"+moduleName+"/"+sessionBeanName+"!"+viewClassName);
        bean.showMessage();         
        System.exit(0);
    }
}
António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
gero
  • 15
  • 3
  • You should refer organization of pojo files in an ear. How they are assembled and how they provide access to which part of the application ? – SacJn Apr 01 '16 at 17:41
  • i have added sample code – gero Apr 01 '16 at 18:03
  • You're making an assumption that's not correct. You **don't** deploy *this simple class to EJB container*. You deploy your application to a Java EE application server (WildFly, TomEE, Glassfish, etc.) and all your EJBs will be managed by your EJB container, which is within your application server. Your POJOs, as your `ClientCounter`, will be managed by the classloader as a normal Java class on a normal Java program. Take a look at [this answer](http://stackoverflow.com/a/8088403/1346996) by @Will Hartung. – António Ribeiro Apr 03 '16 at 12:45

1 Answers1

0

Each time a @Stateless EJB3 is called, the container picks one from a previosly created pool (or creates a new one, if the pool is empty) and uses this instance to execute the called function. In your case, each one has a "new" ClientCounter, so its "counter" will always be 0 when instantiated and 1 just after the calling and before the container destroy it.

You can identify clearly this behavior adding the following to your EJB:

@PostConstruct
public void init() {
    System.out.println(counter.getCount());
}

@PreDestroy
public void destroy() {
    System.out.println(counter.getCount());
}

Obviously, you have to add a getCount() { return count; } on your ClientCounter.

If you really want to make this kind of counting, you have to pick another solution, because even if you try to make your count property static, you may face issues on concurrency and when using your EJB on a cluster.

Paulo Araújo
  • 539
  • 3
  • 12