开发者

Singleton with JBoss SEAM

I have the following code:

@Name("myService")
@Scope(ScopeType.APPLICATION)
@Stateless
public class MyService {
  private Service service;

  private Service getService() {
    if (service == null) {
      service = Service.create(url, new QName("URL",
        "Envelope"));
    }

    return service;
  }    

  public synchronized Port getPort() {
    return getService().getPort();
  }
}

And getPort method is invoked from different threads. "Service.create" takes a lot of time and I've found 开发者_JAVA技巧that actually it is invoked more than once. So it looks like more than one instance of MyService class is created and that's why synchronized doesn't help.

I've changed annotations to:

@AutoCreate
@Startup
@Name("myService")
@Scope(ScopeType.APPLICATION)

And now it seems to work fine: only one instance is created and access to the getPort() method is synchronized.

Could anyone explain why the first case doesn't wok as expected?


A @Stateless bound to an Application scope is an oxymoron

you are asking Java EE to provide a component that has no state, to live in the Application Scope, shared by all users

When you removed the @Stateless annotation, seam handled the instance of the component and placed it in the Application Scope, it also created it at startup, hence having a singleton

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜