【问题标题】:java.lang.ClassCastException: org.jboss.ejb.client.naming.ejb.EjbNamingContext cannot be cast tojava.lang.ClassCastException: org.jboss.ejb.client.naming.ejb.EjbNamingContext 无法转换为
【发布时间】:2016-12-05 21:26:33
【问题描述】:

我有一个 EJB 无状态项目 (helloworld) 在 Wildfly 8.2.1 Final 服务器下运行,带有 java 1.7 和一个带有 java 1.8 的客户端应用程序。我收到一个错误。谢谢

线程“主”java.lang.ClassCastException 中的异常: org.jboss.ejb.client.naming.ejb.EjbNamingContext 不能转换为 com.aburak.sb.SessionBeanRemote 在 com.aburak.sb.Driver.main(Driver.java:15)

这是我的驱动程序类,它是客户端应用程序

public class Driver {
    public static void main(String[] args) throws NamingException {
            Context context=Driver.getInitialContext();
            SessionBeanRemote sbRemote= (SessionBeanRemote) context.lookup("ejb:SessionBean/SessionBean!com.aburak.sb.SessionBeanRemote");
            sbRemote.sessionBeanMethod();
    }
    public static Context getInitialContext() throws NamingException{
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        properties.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        properties.setProperty(Context.PROVIDER_URL, "http://127.0.0.1:8080");

        return new InitialContext(properties);
    }
}

远程 BeanSession

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class SessionBean implements  SessionBeanRemote, SessionBeanLocal{

private static final long serialVersionUID = -7201692010023776738L;

@Override
public void sessionBeanMethod() {
    System.out.println("SessionBean executed...");

}

SessionBeanRemote

@Remote
public interface SessionBeanRemote extends SessionBeanIF{

}

(SessionBeanLocal 与 SessionBeanRemote 完全相同,但 @Local 注解除外)

SessionBeanIF

public interface SessionBeanIF extends Serializable{
public void sessionBeanMethod();
}

这是客户端 Drive 类

public class Driver {
    public static void main(String[] args) throws NamingException {
            Context context=Driver.getInitialContext();
            SessionBeanRemote sbRemote= (SessionBeanRemote) context.lookup("ejb:SessionBean/SessionBean!com.aburak.sb.SessionBeanRemote");
            sbRemote.sessionBeanMethod();
    }
    public static Context getInitialContext() throws NamingException{
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        properties.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        properties.setProperty(Context.PROVIDER_URL, "http://127.0.0.1:8080");

        return new InitialContext(properties);
    }
}

SessionBeanClient >> pom.xml

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

SessionBean >> pom.xml

<dependency>
  <groupId>javax.ejb</groupId>
  <artifactId>javax.ejb-api</artifactId>
  <version>3.2</version>
  <scope>compile</scope>
</dependency>

我关注this tutorial video,但我更改了一些应用服务器等。 并搜索错误找到this,但它并没有解决我的问题。

这是我的Package explorer

感谢四位您的帮助。

【问题讨论】:

    标签: java eclipse maven ejb wildfly


    【解决方案1】:

    您的远程调用路径错误。将 ejb 部署到 Wildfly 服务器时,请查看服务器日志中的完整路径。 它看起来像

    ejb:/ejb-1.0-SNAPSHOT/SessionBean!com.thanhnn.training.SessionBeanRemote
    

    您需要在客户端的资源文件夹中有一个名为“jboss-ejb-client.properties”的属性文件。

    remote.connections=default
    remote.connection.default.host=localhost
    remote.connection.default.port=8080
    remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
    remote.connection.default.username=your-user-name
    remote.connection.default.password=your-pass-word
    

    要创建应用程序用户,请在 ${JBOSS_HOME}/bin 中运行 add-user.bat 或 add-user.sh 并按照流程操作。

    客户端和服务端的握手过程,需要在pom文件中有如下依赖:

    <dependency>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.2_spec</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.ejb</groupId>
            <artifactId>jboss-ejb-api_3.2_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss</groupId>
            <artifactId>jboss-ejb-client</artifactId>
            <version>2.1.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.xnio</groupId>
            <artifactId>xnio-api</artifactId>
            <version>3.4.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.xnio</groupId>
            <artifactId>xnio-nio</artifactId>
            <version>3.4.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.remoting</groupId>
            <artifactId>jboss-remoting</artifactId>
            <version>4.0.21.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.sasl</groupId>
            <artifactId>jboss-sasl</artifactId>
            <version>1.0.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.marshalling</groupId>
            <artifactId>jboss-marshalling-river</artifactId>
            <version>1.4.11.Final</version>
        </dependency>
    

    最后是客户端的代码

     public static void main(String[] args) throws NamingException {
        Context context=Driver.getInitialContext();
        SessionBeanRemote sbRemote= (SessionBeanRemote) context.lookup("ejb:/ejb-1.0-SNAPSHOT/SessionBean!com.thanhnn.training.SessionBeanRemote");
        sbRemote.sessionBeanMethod();
    }
    public static Context getInitialContext() throws NamingException{
        Properties properties = new Properties();
        properties.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    
        return new InitialContext(properties);
    }
    

    注意:不需要在接口中声明方法为public

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 2014-07-08
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      相关资源
      最近更新 更多