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
of course when I do something silly like having both @We开发者_如何学CbService 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 org.glassfish.webservices.JAXWSContainer@249ef1e 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 org.glassfish.webservices.JAXWSContainer@191f81e 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
As @bkail pointed out JSR 109 explicitly allows the for a combination of both @WebService
and @Singleton
. Chapter 3.3.1 states:
3.3.1 Web Service Components
This specification defines two means for implementing a Web service, which runs in a Java EE environment, but does not restrict Web service implementations to just those means. The first is a container based extension of the JAX-RPC or JAX-WS programming model which defines a Web service as a Java class running in the web container. The second uses a constrained implementation of a stateless session EJB or singleton session EJB(only for JAX-WS services) in the EJB container. Other service implementations are possible, but are not defined by this specification.
A workaround could be to have a @Singleton
annotated member attribute in your Web Services class and then delegate to this singleton:
@WebService
public class MyService {
@EJB
private MySingleton singleton;
public void doSomeService() {
this.singleton.doSomeService();
}
}
@Singleton
public class MySingleton {
// some code ...
}
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.
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;
}
}
精彩评论