1

i'd like to have an singleton object in my Jersey 1.19.1 webservice, which is the same instance over all my Glassfish nodes. This is my current implementation:

@Singleton
@ApplicationScoped
@Stateless
public class ValueObject {

    public long downloads = 0;

}

and

@Path("downloads")
public class Downloads {

    @InjectParam
    private ValueObject singleton;

}

The counter is increased when a file is downloaded. After downloading a file and asking for the downloadCounter 1 and 0 is returned depending on which of the two Glassfish nodes processed the request.

My goal is to get always 1. How can i achieve that?

Without @ApplicationScoped or using @Stateful instead of @Stateless leads to the same result.

Regards

John

John Steel
  • 121
  • 1
  • 6

1 Answers1

0

This is not possible with GlassFish. As discussed in this StackOverflow answer, the EJB @Singleton annotation will have one instance per JVM, as per the EJB 3.1 spec:

A Singleton session bean is a session bean component that is instantiated once per application. In cases where the container is distributed over many virtual machines, each application will have one bean instance of the Singleton for each JVM

The answer also mentions that WildFly 10 has a mechanism to support this, but this is a proprietary solution, and not one found in GlassFish.

A solution is currently being investigated for Payara Server, though this is not yet implemented.

Mike
  • 4,852
  • 1
  • 29
  • 48