简体   繁体   English

如何在单独的Web应用程序中访问EJB实现远程接口?

[英]How do i access EJB implementing remote interface in separate web application?

I am using Netbeans 6.8 and Glassfish v3.0. 我正在使用Netbeans 6.8和Glassfish v3.0。

I created an ejb module and created entity classes from database and then created stateless session bean with remote interface. 我创建了一个ejb模块并从数据库创建了实体类,然后使用远程接口创建了无状态会话bean。 Say eg. 比如说。

 @Remote
public interface customerRemote{
    public void add(String name, String address);
    public Customer find(Integer id);
}

@Stateless
public class customerBean implements customerRemote{
    //implementations of methods
}

Then i created a new web application. 然后我创建了一个新的Web应用程序。 But now how do i access remote ejb's in my web application. 但是现在如何在我的Web应用程序中访问远程ejb。 I could lookup a bean with jndi name but what i want to know is, what type of object it will return? 我可以用jndi名称查找一个bean,但我想知道的是,它将返回什么类型的对象? How do i typecast it in customerRemote? 我如何在customerRemote中进行类型转换? I don't have any class named customerRemote in my web application. 我的Web应用程序中没有任何名为customerRemote的类。 So, how do i do it? 那么,我该怎么做? Also, what about the entity class Customer? 另外,实体类客户怎么样? There is no such class named Customer in my web application also. 我的Web应用程序中也没有名为Customer的类。 All ejb's and entity classes are in separate ejb module. 所有ejb和实体类都在单独的ejb模块中。 Please help me :( 请帮我 :(

How do i typecast it in customerRemote? 我如何在customerRemote中进行类型转换? I don't have any class named customerRemote in my web application. 我的Web应用程序中没有任何名为customerRemote的类。 So, how do i do it? 那么,我该怎么做? Also, what about the entity class Customer? 另外,实体类客户怎么样? There is no such class named Customer in my web application also. 我的Web应用程序中也没有名为Customer的类。 All ejb's and entity classes are in separate ejb module. 所有ejb和实体类都在单独的ejb模块中。

Your web application must depend on a library that contains these classes and interfaces. 您的Web应用程序必须依赖于包含这些类和接口的库。 Then you will be able to import the interface and typecast as usual. 然后你就可以像往常一样导入界面和类型转换。 You have two approaches: 你有两种方法:

  • All in one jar . 所有在一个罐子里 This seems to be what you have now. 这似乎是你现在拥有的。 In this case your web app needs to depend on this jar. 在这种情况下,您的Web应用程序需要依赖于此jar。

  • Split API and implementation . 拆分API和实现 A better approach is to split your ejb module in two jars: one jar myModule-api contains the classes that belong to the API of your module. 更好的方法是将ejb模块拆分为两个jar:一个ja​​r myModule-api包含属于模块API的类。 In this case that would be customerRemote and Customer . 在这种情况下,将是customerRemoteCustomer And another jar myModule-impl contains the implementation (the implementation depends on the API of course). 另一个jar myModule-impl包含实现(当然,实现依赖于API)。 Then in your web app, you only need to depend on the API which is in myModule-api . 然后在您的Web应用程序中,您只需要依赖myModule-api

I'm using Eclipse 3.5.2, EJB 3.0 and GlassFish 2.1 but I'm sure that this will work for you too. 我正在使用Eclipse 3.5.2,EJB 3.0和GlassFish 2.1,但我确信这对你也有用。

Well, first of all I have created an EJB project that will be deployed as a stand alone EJB module on the container, and another project that is in fact a web application that uses a single servlet for testing. 好吧,首先我创建了一个EJB项目,它将作为独立的EJB模块部署在容器上,另一个项目实际上是一个使用单个servlet进行测试的Web应用程序。

I can resolve the same problem adding the next parameters to the @EJB declaration on the servlet that invokes the bean: 我可以解决相同的问题,将下一个参数添加到调用bean的servlet上的@EJB声明:

public class SimlpeServletClient extends HttpServlet {
.
.
.
@EJB(beanInterface=ISimpleJob.class,mappedName="ISimpleJob")
ISimpleJob statelesBean;
.
.
.
protected void doPost(HttpServletRequest ...){...}

.
.
.
}

And this is the structure of the bean that implements the interface exposed to the servlet: 这是实现暴露给servlet的接口的bean的结构:

@Stateless(name="SimpleJobBean", mappedName="ISimpleJob")
@Remote( { ISimpleJob.class })
public class SimpleJobBean implements ISimpleJob {
.
.
.
}

As you can see, it seems like your client only knows the interface to work with, but it doesn't tell the server which resource can fullfill the request. 正如您所看到的,您的客户端似乎只知道要使用的接口,但它并不告诉服务器哪个资源可以满足请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM