通过@Produces进行远程EJB注入 - ClassCastException

时间:2015-10-27 14:15:20

标签: java java-ee jboss

我将继续表达我在EJB中看似无穷无尽的误解:

我使用JBoss 8(Wildfly 8.1)作为我的Applicationserver。

我目前正在为问题构建一个解决方法,Inpustreams无法通过RMI传递给远程EJB(一旦我想到它就绝对有意义)。

我不能替换现有的资源接口(将Inpustream写入文件)我在以下3个项目中构建解决方案:

接口(两个EAR中包含的库):

com.package.ejb
public interface StorageAdapter extends Serializable{
    String store(Inpustream is); (//returns an id
    Inputstream load(String id);
}

public interface StorageAdapterProvider{
    StorageAdapter provide();
}

持久性-EAR

com.package.impl
public class FileSystemStorageAdapter implements com.package.ejb.Storageadapter {
//implementation, writing to locally mounted path in filesystem...
}

com.package.impl
@Singleton
@Remote(com.package.ejb.StorageAdapterProvider.class)
public class StorageAdapterProviderBean implements  com.package.ejb.StorageadapterProvider {

    public StorageAdapter provide() {
        return new FileSystemStorageAdapter(); 
    }
}

商家-EAR

com.package.business

public class StorageProvider {
    @EJB(looklup = "java:global/Persistence-EAR/StorageAdapterProviderBean!com.package.ejb.StorageAdapterProvider"
    private StorageAdapterProvider provider;

    @Produces
    public StorageAdapter getStorageAdapter() {
    return provider.provide();
    }

}

然后我使用@Inject StorageAdapter storageAdapter;来获取Storageadapter实现的实例。

Business-EAR / Storageprovider然后抛出ClassCastException,告诉我com.package.impl.FileSystemStorageAdapter cannot be cast to com.package.ejb.StorageAdapter

我在EJB中添加了一些日志记录

com.package.impl
@Singleton
@Remote(com.package.ejb.StorageAdapterProvider.class)
public class StorageAdapterProviderBean implements  com.package.ejb.StorageadapterProvider {

    public StorageAdapter provide() {
    StroageAdapter ret = new FileSystemStorageAdapter();
          logger.info("EJB: RETURNING Stortage adapter");
    logger.info("    is of type:"+ret.getClass().getName());
    logger.info("        is Storageadapter: "+ (ret instanceof StorageAdapter));
    logger.info("        is FileSystemStorageAdapter: "+ (ret instanceof FileSystemStorageAdapter));
    return ret;
    }
}

并正确打印:

EJB: RETURNING Stortage adapter
is of type:com.package.impl.FileSystemStorageAdapter
is Storageadapter: true
is FileSystemStorageAdapter: true

进一步调查显示,当容器正在包装“ret”对象时抛出Exception,因为如果我将@Provides实现设置为:

,我也会得到Exeption
    Object o = provider.provide();

是否无法从EJB返回接口类型? 我错过了什么? Business-EJB是否需要知道实现类? - 在这种情况下,ClassNotFound Exception不会更有意义吗?

提前致谢!

编辑:

我的包装如下:

Persistence.ear

 -lib/Interfaces.jar
  -StorageAdapter
  -Storageprovider
 -persistence_ejb.jar
  -FileSystemStorageAdapter
  -StorageAdapterProviderBean

Business.ear

 -lib/Interfaces.jar
  -StorageAdapter
  -Storageprovider
 -business_ejb.jar
  -StorageProvider 
 -web.war
  -Jaxrsres

Jaxrsres是通过@Inject注入Storageadapter的人 我正在将这两场战争部署到JBoss Wildlfly 8.1服务器上。

我没有对配置进行任何相关更改 - 因此,根据我的理解,您请求的所有其他Point都是默认值。

EDIT2: 它在某种程度上违背了目的,但如果我将Persistence.ear/persistence_ejb.jar添加到Business.ear的库中,它就有效。

Business.ear

 -lib
  -Interfaces.jar
   -StorageAdapter
   -Storageprovider
  -persistence_ejb.jar
   -FileSystemStorageAdapter
   -StorageAdapterProviderBean
 -business_ejb.jar
  -StorageProvider 
 -web.war
  -Jaxrsres

这显然不是我想要的:/

主要观点是,我希望能够使用“DatabaseStorageAdapter”重新部署Persistence.ear,而无需触及business.ear。

“Dream”是直接将Storageadapter作为EJB提供 - 但由于接口已修复且需要InputStream,因此无法完成此操作。

0 个答案:

没有答案