Question

I have a Java web application I’m developing for a school project. There’s a requirement to have the presentation tier (servlets/jsp) be deployed to one server and business logic be deployed to another.

I need a solution to connecting the 2 servers.

Currently I’ve researching RMI and Axis2.

I’m not able to get RMI successfully working. I’m following official tutorial and keep getting a security exception locally, and imagine that it will get worse when Tomcat is involved.

Axis2 seems like a good solution, but I will need time to ramp up on it.

My question is: there a simple way of connection 2 servers so that I can call my business layer? Maybe Tomcat has something built-in.

If RMI is the de-facto protocol and API I should use, is there any good tutorials on using RMI with Tomcat.

Servers that I’m using are both running Tomcat.

Was it helpful?

Solution 3

I ended up using RMI. Using this tutorial I got it working: http://sacrosanctblood.blogspot.com/2008/11/rmi-tutorial-rmi-and-tomcat.html . The key is: in the start up servlet you have to make sure that the Object that you are stubbing out is class scoped and not method scoped. Also, the Security manager code is not needed.

Here's the code for the startServer servlet that I'm using:

public class startServer extends HttpServlet
{
    public static boolean isRegistered = false;    
    public IRemote service = new RemoteImpl();

    @Override
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);        
        if (!isRegistered)
        {
            try
            {                
                IRemote stub = (IRemote) UnicastRemoteObject.exportObject(service, 0);
                Registry registry = LocateRegistry.createRegistry(9345);
                registry.rebind(IRemote.serviceName, stub);
                System.out.println("Remote service bound");
                isRegistered = true;
            }
            catch (Exception e)
            {
                System.err.println("Remote service exception:");
                e.printStackTrace();
            }
        }
    }
}

And here's the client code:

public String getRemoteString()
{
    String result = "";
    try
    {
        Registry registry = LocateRegistry.getRegistry(9345);            
        IRemote serv = (IRemote) registry.lookup(IRemote.serviceName);

        result = serv.executeRemote("Test");
    }
    catch (Exception e)
    {
        System.err.println("Remoteservice exception:");
        e.printStackTrace();
    }

    return result;
}

Currently it's running all on the same server, but I'm sure that I can get the 2 working at a later time.

OTHER TIPS

I am not sure how complicated is your data layer but you can implement REST interface on business logic server using Apache CXF, for example. That should be easier than using Axis2.

There are many many options:

  • Write a file from one side, read it from the other. "the other" has to have an infinite loop to monitor the folder where "one side" writes request files.
  • Use sockets
  • Use REST
  • RMI

If you're on Linux:

Given your environment I would go with REST.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top