3

my problem is that the @PostConstruct is called twice even though it shouldn't. I searched a lot and found similiar problems with jersey https://java.net/jira/browse/JERSEY-1883?filter=-3. However I tried to make a small example which apparently still causes the problem even without any clatter.

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Singleton
@Startup
public class TestSingleton {

  @PostConstruct
  public void init() {
    System.out.println("How many times am I being called?");
  }
}

Configuration

  • Application server: Glassfish 3.1.2
  • Java Version: JDK 1.7_17
  • Packaging: A war file within an ear

Any ideas?

Dr4gon
  • 421
  • 8
  • 17
  • That bug (1883) was fixed 6 days after you asked this question: https://jersey.java.net/release-notes/2.17.html – ROMANIA_engineer Dec 11 '15 at 13:50
  • Thanks for the info. Didn't know it was a real problem. I thought it was just an eclipse hiccup because otherwise that would mean there's a general problem with singletons and post constructs. – Dr4gon Dec 11 '15 at 13:57

2 Answers2

1

I figured it out by using @PreDestroy or Publish of the application server.

The problem is "normal". With using Eclipse as an IDE after the server start the application will be published again. Therefore you'll only see one log message of @PreDestroy but two of @PostConstruct.

The same goes for changing something and publishing afterwards. Here you should only see the message from @PostConstruct once.

Then you'll know everything is alright.

Dr4gon
  • 421
  • 8
  • 17
  • This problem is not present while using WebSphere Liberty, so it probably is Glassfish Eclipse plugin issue. – Gas Mar 07 '15 at 14:18
0

@PostConstruct —This is invoked immediately after a bean instance is created. You can see this : @Startup @Singleton instantiated twice in WebLogic (EJB 3.1)

Community
  • 1
  • 1
Tinh Cao
  • 67
  • 6
  • I read the post. However this does not help at all. I don't want everything that is in `@PostConstruct` to be executed twice. I want to know how to avoid this? – Dr4gon Mar 06 '15 at 12:37