首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在JBoss EAP 7: ClassCastException上注入远程bean

无法在JBoss EAP 7: ClassCastException上注入远程bean
EN

Stack Overflow用户
提问于 2017-03-06 06:17:46
回答 2查看 842关注 0票数 0

我有两个maven Java应用程序运行在两个不同的Jboss 7实例上,让我们将它们命名为Client-Application,。

我想在客户端应用程序上进行远程注入,从注入一个bean。

我的结构如下:

Client-Application

我添加了以下maven依赖项:

代码语言:javascript
复制
<dependency>
    <groupId>org.jboss.eap</groupId>
    <artifactId>wildfly-ejb-client-bom</artifactId>
    <type>pom</type>
</dependency>

这个应用程序上的主类和接口如下所示:

代码语言:javascript
复制
public void remoteInjectionTest() {
    try {


        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        jndiProperties.put("java.naming.factory.initial", "org.jboss.as.naming.InitialContextFactory");
        jndiProperties.put("java.naming.provider.url", "localhost");
        jndiProperties.put("jboss.naming.client.ejb.context", "true");


        Context context = new InitialContext(jndiProperties);

        String jndi = "java:global/Server-Application/ServiceBean!test.package.ServiceBeanInterface";
        serviceInterface = (ServiceBeanInterface) context.lookup(jndi);

        return serviceInterface.getHelloString();
    } catch (NamingException e) {

        e.printStackTrace();
    }

}

和接口:

代码语言:javascript
复制
public interface ServiceBeanInterface {

    public String getHelloString();

}

Server-Application本节展示了我的服务器应用程序的样子:

代码语言:javascript
复制
@Stateless
@Remote(ServiceBeanInterface.class)
public class ServiceBean implements ServiceBeanInterface {

    @Override
    public String getHelloString() {
        return "Hello";
    }

}

我还试图在服务器应用程序接口上添加一个@Remote,但还是没有成功。

代码语言:javascript
复制
@Remote
 public interface ServiceBeanInterface {
    public String getHelloString();
 }

有什么建议吗?

我得到的错误是:

由: java.lang.ClassCastException: com.sun.Proxy引起。$Proxy118不能转换为test.package.ServiceBeanInterface

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-06 13:28:44

假设您的Client-Application也部署在JBoss EAP 7实例上,那么解决方案比您想象的要简单得多。

我建立了一个测试项目,它创建了四个工件:

  • serverapi -包含ServiceBeanInterface
  • 包含ServiceBean
  • 包含serverejbserverapi的serverapp - EAR
  • clientapp - WAR包含servlet (如下图所示)及其WEB/lib目录中的serverapi

测试servlet如下所示:

代码语言:javascript
复制
@WebServlet(urlPatterns = "/")
public class Client extends HttpServlet {

    // Inject remote stub
    @EJB(lookup="java:global/serverapp-1.0-SNAPSHOT/serverejb-1.0-SNAPSHOT/ServiceBean!com.stackoverflow.p42618757.api.ServiceBeanInterface")
    private ServiceBeanInterface serviceBeanInterface;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write(serviceBeanInterface.getHelloString());
    }

}

serverappclientapp被部署到服务器上。

点击http://localhost:8080/clientapp-1.0-SNAPSHOT/就能得到预期的Hello

不需要老式的JNDI查询。

但是,如果您真的想进行JNDI查找,那么只需要:

代码语言:javascript
复制
 Context initialContext = new InitialContext(); // no props needed in the same server instance
 ServiceBeanInterface serviceBeanInterface
     = (ServiceBeanInterface)initialContext.lookup("java:global/serverapp-1.0-SNAPSHOT/serverejb-1.0-SNAPSHOT/ServiceBean!com.stackoverflow.p42618757.api.ServiceBeanInterface");

GitHub上可以找到一个示例。

票数 2
EN

Stack Overflow用户

发布于 2017-03-07 11:24:39

在JBoss文档站点上,来自远程服务器实例的EJB调用上有大量用于执行实例到实例的远程EJB调用的文档。

配置看起来可能有点复杂,但这主要是出于安全原因。代码端仍然相对简单。

我建议你参考这个,如果你还有问题的话,再问一个新的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42618757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档