• Post Reply Bookmark Topic Watch Topic
  • New Topic

why the bean class does not implement remote interface

 
alfred jones
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface MyHome extends EJBHome {
Myint create() throws RemoteException, CreateException;
}




package com;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface Myint extends EJBObject {
public int show(int x) throws RemoteException;





package com;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;


public class MyBean implements SessionBean { // is this code wrong ?
public int show(int x) {
System.out.println(x);
return (x);
}
}





why "MyBean" class is not like

public class MyBean implements SessionBean, Myint {
public int show(int x) {
System.out.println(x);
return (x);
}
}


as the MyBean class implementing the show() method so the MyBean class should implement Myint interface .
 
Andrew Bowes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfred,

You have partly answered your own question.

The Bean class should NOT implement the Remote Interface as it should not know that it is being used remotely therefore there should be no references to RemoteExceptions in the definition of the Bean. This will allow you to switch between Local & Remote interfaces with a minimum of effort.
 
alfred jones
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the bean does not implement the remote interface then how the client would reach to the implemented business method in the bean ? so we need to implement the remote interface for the bean.
 
alfred jones
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the bean does not implement the remote interface then how the client would reach to the implemented business method in the bean ? so we need to implement the remote interface for the bean.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bean must throw only application exceptions and system exceptions. The problem with RemoteException is that, being checked, it would propagate to the client (which would not know how to deal with it). This is not the intent, as RemoteException is really meant as a system exception.

If RemoteException were thrown from your bean, you would need to wrap it snd rethrow as javax.ejb.EJBException. (Indeed, EJBException must be used for communicating all system exceptions to the Container.) The container will then throw RemoteException to a remote client.
 
steven peh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alfred,

EJBs are not your typical java inheritance objects. The app server are not performing polymorphism on the remote interface to the ejb object implementation. In fact as you stated, the ejb does not implement the remote interface, so they are in fact not related from an inheritance point of view.

Now, i dont know if its in the J2EE spec, but most J2EE App Servers will inspect your EJB during deploy time and enforce the rule that all the methods in your remote interface are implemented in your ejb class (and, as some one pointed out, make sure your dont throw RemoteExceptions). So during runtime, the J2EE App server will match the methods being called on the remote interface to the ejb. How it does this, I wouldnt know, there are many ways, one quick and dirty way would probably be using introspection.

Cheers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!