JAVA RMI and EJB Remote Invocation Model

I just graduated half a year ago, and it is the first time to write an article on technology. The main purpose is to record my understanding of some knowledge points at work and in the usual learning process, and share it with you. There must be some bad writing and mistakes. I hope to point out the criticism and let everyone grow together, thank you~~. The following is my own opinion on EJB calls and java RMI.
Programmers who have used the server should know that when a rich client or other methods are used to call an EJB application deployed on the server side, the client calls lookup("JndiName") to send a lookup jndi name to the server side, and then the server If the JNDI name exists on the client, an ejb object bound to JNDI will be returned to the client, and finally the client will call the business method through the returned EJB object. The principle of the entire EJB calling process is basically the same, but the specific implementation of the EJB container is still relatively complicated, and my level is limited and cannot reach the level of detail. As far as the whole process of EJB invocation is concerned, the most basic theoretical basis is RMI. In fact, the specific implementation of EJB container for these is RMI. The


first step is to create a simple remote interface.
package com.rmi.demo;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface FirstRmiInterface extends Remote{
	public void sayHello() throws RemoteException;
}


Step 2: Implement the remote interface
package com.rmi.demo;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class FirstRmiInterfaceImpl extends UnicastRemoteObject implements FirstRmiInterface{

	protected FirstRmiInterfaceImpl() throws RemoteException {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 *
	 */

	@Override
	public void sayHello() throws RemoteException {
		// TODO Auto-generated method stub
		System.out.println("HELLO WORLD FIRST RMI DEMO");
	}

}

Step 3: Create a remote server:
package com.rmi.demo;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class RmiServer {
	public static void main(String args[]){
		try {
			LocateRegistry.createRegistry(1000);
			FirstRmiInterface rmiInterface = new FirstRmiInterfaceImpl();
			  Naming.bind("rmi://localhost:1000/rmiObject",rmiInterface);
			//Bind the interface of the remote object on the remote server side for the client to make remote calls. In fact, this simple model is the simple model of ejb remote invocation. The remote server binding object here is equivalent to deploying an EJB application on the application server (weblogic, glassfish, etc.), then the EJB container of the server will publish the deployed EJB in the form of JNDI [or other forms], and then The client can call and access the remote EJB in a specific way.
			System.out.println("Rmi Bind Success");
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		} catch (AlreadyBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		
	}
}



Step 4: Create a client and access the objects bound to the remote end
package com.rmi.demo;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class RmiClient {
	public static void main(String args[]){
		FirstRmiInterface rmiInterface;
		try {
			rmiInterface = (FirstRmiInterface)Naming.lookup("rmi://localhost:1000/rmiObject");
			rmiInterface.sayHello();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		
	}
}



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327080082&siteId=291194637