Create a new Java generic project to
JBoss is used here to install JBoss and then go to the Jboss-4.2.3.ga\client directory and copy all the jar packages to the project Lib.
The 3 interfaces are as follows:
Public Interface Calculate { // Calculator interface, there is an addition public String Add (Double a,double b);}
Public Interface Calculatelocal extends Calculate { // This interface is used for localization, inheriting parent class }
Public Interface Other { // Another interface public String hello (); }
Here are two implementation classes:
//This class is a stateless local and remote bean@Stateless @remote (Calculate.class) @Local (calculatelocal.class) Public classCalculateimpl implements calculate,calculatelocal{/*using the other EJB * mode one, through annotation injection * Beanname indicates which EJB to inject * if there are two classes that implement this interface, you must specify which class to inject * Only one, you can not specify **/@EJB (Beanname="Otherimpl") Other and other ; /** * @Resource * Inject resources **/ //@Resource Timeservice t; //@Resource (mappedname= "Java:jndi's name") DataSource da;@Override PublicString Add (double A, double b) {/** Mode two, search through Jndi * Properties props = new properties (); Set Jndi connection Properties (JBoss) Props.setproperty ("Java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); Server URL (jboss) props.setproperty ("Java.naming.provider.url", "127.0.0.1:1099"); try{InitialContext ctx = new InitialContext (props); Other other = (other) ctx.lookup ("Otherimpl/remote");//Local Call: Calculateimpl/local Other.hello (); }catch (Exception e) {}*/ return "Results:"+a+b+Other.hello (); }}
@Stateless @remote (other. class publicclass Otherimpl implements other{ @Override String Hello () { System. out. println ("hello ... " ); return " Hello " ; }}
Above, the service-side bean has been developed and is now packaged and released
Right-click Project-->export-->java JAR file-->next
We're going to see the exported jar package.
If you put this jar package into the Jboss-4.2.3.ga\server\default\deploy directory, start JBoss.
Here is the client development
The 3 interfaces are still needed, the implementation class can be done, because we are accessing the server-side bean by remote
Public classMain { Public Static voidMain (string[] args) {Properties props=NewProperties (); //Setting Jndi Connection Properties (JBoss)Props.setproperty ("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); //URL of the server (JBoss)Props.setproperty ("Java.naming.provider.url","127.0.0.1:1099");//local IPTry{InitialContext CTX=NewInitialContext (props); //Jndi Lookup RemoteCalculate Calculate = (Calculate) ctx.lookup ("Calculateimpl/remote");//Local call is: Calculateimpl/localSystem. out. println (Calculate.add (12D, 45D));//Call}Catch(Exception e) {System. out. println (E.getmessage ()); } }}
In this way, a simple distributed application succeeds in implementing the deployment
2. A small demo of an EJB