Search code examples
javapythonsocketstomcatinterprocess

How to implement a socket for a Java EE application in Tomcat


I've a Python application for data analysis and a Java EE application for web monitoring. Now I need to establish a durable communication between them, in order to transfer the analysis result from python to java.

Since both of the applications are located at the same server, I want to implement a TCP socket. As for the data volume, there're about 10 sensors' data transferred per second. I'm using Tomcat 8.0 for Java EE and a simple script for python. So my questions are :

  • Is socket implementation a good idea ?
  • If yes, how to implement it, can somebody give me a tutorial or example ?
  • If not, what should I do next ?

Additional information

I saw a related question on StackOverflow How to serve a socket from a Java EE application, there're some propositions :

  • Implement a Connector (JCA). But I think the target runtime is JBoss rather than Tomcat.
  • Implement a Java Naming and Directory Interface (JNDI). That's what I'm trying as you can see as my previous question Why JNDI resource can only be called once in Tomcat? People use JNDI for using resources, e.g. db connection. So I'm not sure if it's a good way for realtime communication. And I've met many troubles by learning it.
  • I thought about web-socket. But does it mean I need a python server as well ?

Solution

  • I believe there is a hundred way to do it to share data between these technologies. But for Java you should keep it simple. In Tomcat you don't need to write a socket implementation, you just need a basic Servlet implementation. So basically for your questions.

    Is socket implementation a good idea ?

    Shortly NO.

    If yes, how to implement it, can somebody give me a tutorial or example ?

    Already answered NO.

    If not, what should I do next ?

    Write a basic servlet application who listen an server url address. Your phyton script is just a client. In phyton site you just send a POST request to servlet url and in Java side get the request read your data and process it. You can start to learn Servlet from Mkyong.