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

天涯浪子 提交于 2019-12-06 10:48:36

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; 
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!