simgineer simgineer - 5 months ago 51
Java Question

How to create a @Singleton bean that implements a @WebService

EDITED: after believing this is a NetBeans 7.0 editor bug. It still compiles and is deployable.

I want to convert my webservice which is @WebService;@Stateless implementation into a @Singleton bean but when I replace the @WebService with @Singleton annotation... I get the image below in my IDE Editor

Screen Shot of NetBeans IDE

of course when I do something silly like having both @WebService and @Stateless and deploy in glassfish I get:

severe error: Annotations processing failed for...

below is a link (there are more, but I'm limited to two links now) which leds me to believe Singleton beans can be used in the manner I'm trying to use them.

"Singleton session beans offer similar functionality to stateless session beans but differ from them in that there is only one singleton session bean per application, as opposed to a pool of stateless session beans, any of which may respond to a client request. Like stateless session beans, singleton session beans can implement web service endpoints."

This link might hint that there is a bug in the compiler which was fixed in the jboss environment. I'm not sure if this issue is related though and would love to hear what experienced users think. ;)

Here is output from glassfish 3.1

INFO: Closing Metro monitoring root:
INFO: Portable JNDI names for EJB StateBean :
java:global/BearBearService/StateBean] INFO: Metro monitoring rootname
successfully set to:
WARNING: Container org.glassfish.webservices.JAXWSContainer@249ef1e
doesn't support class INFO: Portable
JNDI names for EJB BearBearImpl :
java:global/BearBearService/BearBearImpl] INFO: WS00019: EJB Endpoint

Here is output from glassfish 3.0.1

INFO: Metro monitoring rooname successfully set to:
amx:pp=/mon/server-mon[server], type=WSEndpoint,
name=AppleImplService-AppleImplPort WARNING: Container
org.glassfish.webservices.JAXWSContainer@191f81e doesn't support class INFO: Portable JNDI names for EJB
AppleImpl "
INFO: WS00019: EJB Endpoint deployed AppleService listening at
address at http://localhost:8080/AppleImplService/AppleImpl INFO:
AppleService was successfully deployed in 438 milliseconds

Answer Source

This is a bug in NetBeans 7.0 editor. I was able to build and deploy a WS using both @WebService, @Singleton even though the service name was underlined in red to indicate a compile error. Just ran a test to verify that the spec bean works as advertised. I will provide code below and snapshot of my test ui.

Thanks to @home, @bkali and @Preston for contributing.

Submitted to netbeans as a bug:

Notice below the instance state does to timeout and change from 50 to 0 after the timeout period (10 minutes) when I redeploy the service as a Singleton instead of a stateless.

Snapshot of Test UI

Web Service Test Code:

import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.jws.WebService;
import javax.jws.WebParam;
import javax.ejb.Stateless;

@WebService(serviceName = "")
public class BearBearImpl implements BearBearWS {

    int state = 0;
    static int staticState = 0;
    @EJB StateBean sb = new StateBean();

    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";

    public void setAllState(int in) {
        System.out.println("setting instance state from "+state+" to "+in);
        state = in; 
        System.out.println("setting static state from "+staticState+" to "+in);
        staticState = in; 
        System.out.println("setting singleton state from "+sb.state+" to "+in);
        sb.state = in;

    public int getInstanceState() { 
        System.out.println("returning instance state "+state);
        return state; 

    public int getStaticState() { 
        System.out.println("returning static state "+staticState);
        return staticState; 

    public int getSingletonState() { 
        System.out.println("returning singleton state "+sb.state);
        return sb.state; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download