I'm using RMI to pass string from one web app to another web app.Initiating RMI service from the "contextInitialized" method and trying to close the RMI service from "contextDestroyed" method.
MessageContextListener class:
public class MessageContextListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent sce)
{
try
{
RMIServer rmiServerObj = new RMIServer();
MediatorImplementation.getInstance().setRmiServerObj(rmiServerObj);
}
catch (RemoteException re)
{
re.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent sce)
{
RMIServer rmiServerObj = MediatorImplementation.getInstance().getRmiServerObj();
if(rmiServerObj != null)
{
try
{
rmiServerObj.unbindRegistry();
}
catch (RemoteException re)
{
re.printStackTrace();
}
catch (NotBoundException nbe)
{
nbe.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
RMIServer class:
public class RMIServer extends UnicastRemoteObject implements RemoteInterface
{
private int serverPort = 3232;
public RMIServer() throws RemoteException
{
try
{
System.setProperty("java.security.policy","file:/F:/PolicyFiles/rules.policy");
if(System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}
Registry registry = LocateRegistry.createRegistry( serverPort );
registry.rebind("MessagePath", this);
}
catch(RemoteException re)
{
re.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void unbindRegistry() throws RemoteException, NotBoundException
{
Registry registryObj = LocateRegistry.getRegistry(serverPort);
UnicastRemoteObject.unexportObject(registryObj, true); //this line throws java.rmi.NoSuchObjectException: object not exported
}
public void sendMessageParams(String messageFilePath) throws RemoteException
{
System.out.println("Message File Path: " + messageFilePath);
}
}