1

I'm new to using Java within a web environment. At the moment I have a Java application that we run on a local machine, we'll call it the server app. We want to add the ability to communicate with this application by means of a RESTful interface.

I've been playing around with Tomcat and Glassfish and I have something that sort of works, but I'm certain I'm doing things in a weird way. I have a listener in the web application that creates an instance of my server application every time Tomcat is started.

When a connection is created the servlet grabs the instance of this server application so I can interact with it and pass data to a jsp. I can't help but feel this isn't a conventional approach.

I'm leaning towards just running the server jar entirely separately and using sockets on the local machine for the servlets to interact, but I can't help but think this is overkill. I've looked around and it looks like RMI might be a solution, but I'm unfamiliar with it.

Here is what I need to be able to do:

  • Tomcat needs to be running to accept REST requests
  • I need a way for the web application to check to see if the server app is running
  • I need the web app to be able to get an instance of the server app so that I can call it's methods or at least send it some JSON

Overall I think it's a fairly basic task, I'm just getting a bit lost trying to find a good way to organize my project.

Should I run the server app outside of the Tomcat environment and just use sockets and some JSON?

Should I run the server app outside of Tomcat and use RMI (will require some research, but that's no big deal)

Should I keep the server app within the Tomcat environment like I have it currently?

3
  • 1
    Please, explain how it is now the server app's interface: Is it a server socket? Is it a simple API? Commented Jul 15, 2015 at 23:49
  • If it is on the same machine (and even if it isn't if done properly), you could consider jmx for cross-application communication. However, this should really only be for internal communication. Don't use it for information that you could get anyway using RESTful interface.
    – Neil
    Commented Jul 16, 2015 at 7:16
  • @LittleSanti: Currently we interact with the server application via CLI, we want to be able to interact with it from a new version of our website that is being built on Amazon's cloud infrastructure. There is also a possibility of eventually moving the entire app to EC2, but for now it has to be local.
    – Mr. Nivlac
    Commented Jul 16, 2015 at 17:06

2 Answers 2

2

The easiest approach if you can accept the drawbacks is to have tomcat as a passive server such that it never contacts the server app. All data flows the other way by periodicly sending heart beats to tomcat and retrieve the json data you mentioned. The drawbacks is that it will (only) be eventual consistent so you will have to accept a small time delay.

The other approach is hosting a server in the server app. This can be a RMI server or a http server. Since java SE 6 there have been a http server built into java which is fairly easy to work for posting documents back and forth. There is no templating or all that stuff, but just serialize your data to JSON and you are good to go. Once you have this server running just connect to it from the tomcat server.

1
  • 1
    The first approach you mentioned would actually work perfectly. I don't know how I didn't see that as an option. I was getting to wrapped up in the idea of the web application acting as a controller of sorts. The application isn't very time sensitive. It basically just waits for a batch of work then processes it. Thank you for your input!
    – Mr. Nivlac
    Commented Jul 16, 2015 at 16:36
-2

Just use external IP to to interact with server machine, means u can pass.

1) start the Remote server (tomcat)

2) check the network , if Client and server in same network use internal ip otherwise use external ip.

1
  • 1
    Please fully explain how this solution works and how it answers the OP's question.
    – user40980
    Commented Aug 5, 2015 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.