简体   繁体   中英

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

I am using Netbeans 6.8 and Glassfish v3.0.

I created an ejb module and created entity classes from database and then created stateless session bean with remote interface. 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. But now how do i access remote ejb's in my web application. I could lookup a bean with jndi name but what i want to know is, what type of object it will return? How do i typecast it in customerRemote? I don't have any class named customerRemote in my web application. 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. All ejb's and entity classes are in separate ejb module. Please help me :(

How do i typecast it in customerRemote? I don't have any class named customerRemote in my web application. 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. All ejb's and entity classes are in separate ejb module.

Your web application must depend on a library that contains these classes and interfaces. 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.

  • Split API and implementation . 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. In this case that would be customerRemote and Customer . And another jar myModule-impl contains the implementation (the implementation depends on the API of course). Then in your web app, you only need to depend on the API which is in 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.

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.

I can resolve the same problem adding the next parameters to the @EJB declaration on the servlet that invokes the bean:

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:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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