JBoss Singleton服务示例:不公开单个对象

时间:2015-10-09 19:43:50

标签: java service jboss singleton ejb

我正在使用JBoss 6.2并尝试从webapp过滤器调用单例服务。

我已成功安装并在群集环境中启动单件服务,如以下快速入门教程所述:

https://github.com/jboss-developer/jboss-eap-quickstarts/tree/44c8c90/cluster-ha-singleton

我们可以看到单例服务已在管理控制台中成功部署:

http://www.rakkatak.com/so/stackoverflow_question_singletonservice_img1.png

作为本教程的补充,我在SchedulerBean.java中添加了一个方法count(),如下所示:

@Remote
@Singleton
public class SchedulerBean implements SessionManagerSingleton {
…

    private int counter;

…


    @Override
    public void getSession() {
        counter++;
        LOGGER.info("SchedulerBean: Counter=" + counter);
    }
}

我想从webapp访问上面的单例。在我的例子中,这是一个过滤器(LoginFilter),可以通过放置在JBoss模块目录中的service.jar访问。这个jar包含以下内容:

/META-INF/jboss-ejb-client.properties /com/login/filter/LoginFilter.class

jboss-ejb-client.properties包含:

remote.connections=node1, node2

remote.connection.node1.host=localhost
remote.connection.node1.port = 9999
remote.connection.node1.connect.timeout = 500
remote.connection.node1.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

remote.connection.node2.host=localhost
remote.connection.node2.port = 10099
remote.connection.node2.connect.timeout = 500
remote.connection.node2.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

LoginFilter.java包含:

final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);

Scheduler scheduler = (Scheduler) context.lookup("global/jboss-cluster-ha-singleton-service/SchedulerBean!org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.Scheduler");
scheduler.count();

请注意,org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean接口位于类路径上。

我将service.jar复制到$ JBOSS_HOME / modules / com / login / filter / main中,然后启动node1服务器。接下来,我在浏览器中调出Login webapp,配置为在节点1上使用上面的LoginFilter类:

本地主机:8080 / V1 /登录

Scheduler计数方法的输出是:

14:58:39,682 INFO [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean](http- / 127.0.0.1:8080-1)调度程序Bean:计数器= 1

在此之后,我调出指向Node 2的Login webapp并点击LoginFilter:

本地主机:8180 / V1 /登录

在节点2的控制台上,我看到以下输出:

16:29:23,183 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8180-1) SchedulerBean: Counter=1

我希望看到'Scheduler Bean:Counter = 2',因为这意味着SchedulerBean的行为类似于群集节点之间的单例。有没有人知道这是否可以作为Singleton服务的一部分或其他?谢谢。

重复命中节点1时的输出:

17:15:07,347 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=1
17:15:08,617 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=2
17:15:09,903 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=3
17:15:11,284 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=4
17:15:12,615 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=5
17:15:13,500 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=6
17:15:14,179 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=7
17:15:14,832 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=8
17:15:15,565 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=9
17:15:16,317 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=10
17:15:18,657 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8080-2) SchedulerBean : Counter=11

重复命中节点2时的输出:

17:29:43,785 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8180-1) SchedulerBean: Counter=1
17:29:48,645 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8180-1) SchedulerBean: Counter=2
17:29:49,012 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8180-1) SchedulerBean: Counter=3
17:30:04,162 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8180-1) SchedulerBean: Counter=4
17:30:10,254 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8180-1) SchedulerBean: Counter=5
17:30:14,524 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8180-1) SchedulerBean: Counter=6
17:30:15,291 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8180-1) SchedulerBean: Counter=7
17:30:19,969 INFO  [org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.SchedulerBean] (http-/127.0.0.1:8180-1) SchedulerBean: Counter=8

0 个答案:

没有答案