首页 > 解决方案 > 发生异常“java.lang.ClassCastException:org.wildfly.naming.client.store.RelativeContext 无法转换为 com..dispatchLogs.IDispatchLogs”

问题描述

我有一个 EJB:

@Stateless
@EJB(name = "LogsTransferService/LoggedDataBean/remote", mappedName = "LogsTransferService/LoggedDataBean/remote", beanInterface = ILoggedData.class)
public class DispatchLogsBean implements IDispatchLogs {

并且接口有@Remote注解:

@Remote
public interface IDispatchLogs {

我正在尝试将其查找为:

final Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
        env.put("jboss.naming.client.ejb.context", true);
        InitialContext remoteContext = new InitialContext(env);
        Object ejb = remoteContext
                .lookup("ejb:DispatchService/DispatchLogsBean!com.nextcontrols.dispatchLogs.IDispatchLogs" + IDispatchLogs.class.getName());
        System.out.println(ejb);
        IDispatchLogs dEJB=(IDispatchLogs) ejb;

首先,使用我在 EJB 标头中指定的“LogsTransferService/LoggedDataBean/remote”无法访问它。它给出了 Name not found 异常。其次,如果我按照代码所示访问它。name not found 异常消失了,但是发生了 Class cast 异常:

java.lang.ClassCastException: org.wildfly.naming.client.store.RelativeContext cannot be cast to com.dispatchLogs.IDispatchLogs

我在我的 POM 中有这种依赖关系:

<dependency>
    <groupId>org.wildfly</groupId>
    <artifactId>wildfly-ejb-client-bom</artifactId>
    <version>23.0.2.Final</version>
    <type>pom</type>
</dependency>

我正在使用 JDK 8 和 Wildfly 16。

标签: javawildflyjndi

解决方案


推荐阅读