我有以下情况:请求范围的JAX-RS服务调用无状态EJB3 bean,并且 EJB3 bean 在客户端连续调用 Web 服务之间保留状态。
代码如下:
网络服务
@javax.enterprise.context.RequestScoped
@Path("/actions")
public class CounterFrontEnd {
@EJB
private ICounterService.ILocal counterService;
@GET @Produces("application/text;") @Path("/counter")
public String counter() {
return String.format("%d ", counterService.incCounter());
}
无状态 EJB3 bean
@Stateless
@Local (ICounterService.ILocal.class)
@Remote(ICounterService.IRemote.class)
public class CounterService implements ICounterService.ILocal, ICounterService.IRemote {
public int counter = 0;
@Override
public int incCounter() {
return counter++;
}
然后我使用以下 python 脚本调用该服务:
for i in range(100):
os.system( 'curl http://somewhere:8080/counter-ws/rest/actions/counter' )
令人惊讶的是,输出是:
1 2 3 4 5 ...