I’ve been in the Microsoft world for a while, there are a lot distributed technology in the Microsoft stack. DCOM, COM+,Remoting, ASMX/WCF. What’s the equivalent term for EJB? I would say that’s dot net remoting.
for a typical remoting project, we need define the MBR Object, Host it ( single call, singleton or Client control activation.) using different options, (IIS or in-process hosting. the runtime will take care the proxy/sub stuff.)
here I will put a hello world EJB. Jboss will be the role of IIS.
like remoting,First define a Interface (shared as a contract between server and client. )
public interface ICalculatorBusinessObjects { public int Add(int ... args); } |
then Implement a MBR like remoting. @Stateless @Remote public class CalculatorBeanBase implements ICalculatorBusinessObjects { @Override public int Add(int... args) { // TODO Auto-generated method stub int i=0; System.out.println("------------------" + "get HIt on Server side, My hashcode" + this.hashCode() ); for (int j : args) { i+=j; } return i; } } |
like the Microsoft attribute, those @xx are annotations. the runtime will check thos annotation and enforce some ad-hoc logic. like provide different hosting control, transaction control,etc.
Once Done, need deployed it to a container or server . for .net , it’s just a assembly or a dll. for J2ee, a zip file called EJB jar. you can click eclipse and export the build to a jar file firstEjb.jar
then deploy the jar to Jboss. drag and drop the jar to c:\jboss6\server\default\deploy folder. ( for standard configuration, c:\jboss6\server\all\deploy if you run with –c all option.)
the server discovers new bits and host it, also register a JDNI entry. like the remoting .rem stuff.
the ejb is hosted and ready for client connection now.
for client code, just find the proxy by querying the JNDI server and call the biz logic.
import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class main { public static void main(String[] args) throws NamingException { // TODO Auto-generated method stub Context context=new InitialContext(); ICalculatorBusinessObjects o=(ICalculatorBusinessObjects)context.lookup("java:CalculatorBeanBase/remote"); System.out.println(o.getClass().getCanonicalName()); System.out.println(o.Add(1,2,3,4,5)); } } |
when you run the jboss, Jboss is also one JNDI server listening one a specific port , 1099 by default.
so you need tell the client, which jndi server we should talk to by put some environment variables like the following.
-Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory -Djava.naming.provider.url=jnp://localhost:1099 -Djava.naming.factory.url.pkgs=org.jnp.interfaces -Djnp.timeout=0 -Djnp.sotimeout=0 |
When you run the client, you will get the result as you expected, a simple calculator. but all logic runs on server, the Jboss jvm.
Jboss log.
here concludes the basic tutorial. have fun.