jboss-eap-quickstarts cluster-ha-singleton client JNDI Lookup Class Cast Exception

时间:2015-10-06 16:56:10

标签: java maven 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

作为快速入门教程的补充,我想要一个从外部客户端调用单例服务的示例。在我的例子中,这是一个过滤器(LoginFilter),可以通过放置在JBoss模块目录中的service.jar访问。这个jar包含以下内容:

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

出于我想要做的目的,我没有使用群集环境,只有Node1才会运行。

jboss-ejb-client.properties包含:

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=node1
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

LoginFilter.java包含:

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

System.out.println(">>>>> JNDI LOOKUP >>>>>");
Object obj = context.lookup("global/jboss-cluster-ha-singleton-service/SchedulerBean!org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.Scheduler");
Scheduler scheduler = (Scheduler) obj;
System.out.println(">>>>> JNDI END >>>>>");

我将service.jar复制到$ JBOSS_HOME / modules / com / login / filter / main中,然后启动node1服务器。

在service.jar所在的目录中,我们在module.xml中定义了以下依赖项:

<module xmlns="urn:jboss:module:1.0" name="td.oca.spike.kernel">
    <resources>
        <resource-root path="service.jar"/>
    </resources>
    <dependencies>
        ...
        <module name="org.jboss.as.quickstarts" />
    </dependencies>
</module>

其中org.jboss.as.quickstarts是另一个包含SchedulerBean接口的模块。

接下来,我在我的浏览器中调出Login webapp,它配置为使用上面的LoginFilter类。当我调用它时,我们永远不会看到系统输出&#34;&gt;&gt;&gt;&gt;&gt; JNDI END&gt;&gt;&gt;&gt;&gt;&#34;相反,我得到以下类强制转换异常:

java.lang.ClassCastException: org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.Scheduler$$$view1 cannot be cast to org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.Scheduler

我认为我必须缺少某种配置以允许从LoginFilter调用单例服务。请提出您认为可能有所帮助的任何建议,并提前致谢!

Scheduler.java

package org.jboss.as.quickstarts.cluster.hasingleton.service.ejb;

/**
 * @author <a href="mailto:wfink@redhat.com">Wolf-Dieter Fink</a>
 */
public interface Scheduler {

    void count();

    void initialize(String info);

    void stop();

}

SchedulerBean.java

package org.jboss.as.quickstarts.cluster.hasingleton.service.ejb;

import javax.annotation.Resource;
import javax.ejb.ScheduleExpression;
import javax.ejb.Singleton;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;

import org.jboss.logging.Logger;


@Singleton
public class SchedulerBean implements Scheduler {
    private static Logger LOGGER = Logger.getLogger(SchedulerBean.class);
    @Resource
    private TimerService timerService;

    private int counter=0;

    @Timeout
    public void scheduler(Timer timer) {
        LOGGER.info("*** HASingletonTimer: Info=" + timer.getInfo());
    }

    @Override
    public void initialize(String info) {
        ScheduleExpression sexpr = new ScheduleExpression();
        // set schedule to every 10 seconds for demonstration
        sexpr.hour("*").minute("*").second("0/10");
        // persistent must be false because the timer is started by the HASingleton service
        timerService.createCalendarTimer(sexpr, new TimerConfig(info, false));
    }

    @Override
    public void stop() {
        LOGGER.info("Stop all existing HASingleton timers");
        for (Timer timer : timerService.getTimers()) {
            LOGGER.trace("Stop HASingleton timer: " + timer.getInfo());
            timer.cancel();
        }
    }


    @Override
    public void count() {
        counter++;
        LOGGER.info("***** HASingletonTimer: Counter=" + counter);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为问题在于您尝试远程访问Scheduler,但由于它未注释为远程接口,因此默认为本地:https://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html

您可以向@Remote添加Scheduler,看看是否能让事情变得更好?