simgineer simgineer - 11 months ago 99
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.

http://download.oracle.com/javaee/6/tutorial/doc/gipjg.html


"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. ;)

https://issues.jboss.org/browse/EJBTHREE-2161

Here is output from glassfish 3.1


INFO: Closing Metro monitoring root:
amx:pp=/mon/server-mon[server],type=WSEndpoint,name=soco.ws.bb.bearBearWS-BearBearImplPort
INFO: Portable JNDI names for EJB StateBean :
[java:global/BearBearService/StateBean!soco.ws.bb.StateBean,
java:global/BearBearService/StateBean] INFO: Metro monitoring rootname
successfully set to:
amx:pp=/mon/server-mon[server],type=WSEndpoint,name=soco.ws.bb.bearBearWS-BearBearImplPort
WARNING: Container [email protected]
doesn't support class com.sun.xml.ws.api.server.Module INFO: Portable
JNDI names for EJB BearBearImpl :
[java:global/BearBearService/BearBearImpl!soco.ws.bb.BearBearWS,
java:global/BearBearService/BearBearImpl] INFO: WS00019: EJB Endpoint
deployed


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
[email protected] doesn't support class
com.sun.xml.ws.api.server.Module INFO: Portable JNDI names for EJB
AppleImpl "
[java:global/AppleService/AppleImpl!com.ws.srv.MyService,java:global/AppleService/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: http://netbeans.org/bugzilla/show_bug.cgi?id=200911

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 = "soco.ws.bb.bearBearWS")
@Singleton
//@Stateless
public class BearBearImpl implements BearBearWS {

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

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

    @Override
    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;
    }

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

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

    @Override
    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