Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
scroll top