0

I have a server written in Java. It accepts http requests as commands to do work. The work takes time.

We use Java in the backend and Python in the front end sending the http requests and controlling the work done. In Python, there exists athreading.Event which allows communication between threads. What I would like to do might be difficult because of our language choice, but is it possible to have the same type communication between java and Python?

That is, Java creates the event and provides a pointer to it which can be accessed and set by Python? I've used this pattern between C++ and Python, but I guess that's easier because Python is written in C.

Any help or pointers to resources will be welcome, thanks.

CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
  • 1
    You can use a shared-memory region (posix-ipc or nmap) but i preffer OS Sockets as they are multiplatform and widely supported. An alternative would be to use pub/sub message-queue system, but this usually requires a broker (MQTT, RabbitMQ, Kafka) – Jorge Ruiz Gómez Apr 17 '23 at 08:58
  • Does this answer your question? [How to continues send data from backend to frontend when something changes](https://stackoverflow.com/questions/71395004/how-to-continues-send-data-from-backend-to-frontend-when-something-changes) – Michael Gantman Apr 17 '23 at 14:35
  • Certainly helps, thanks! – CiaranWelsh Apr 19 '23 at 07:32

2 Answers2

2

I suggest that you use an http-based callback mechanism.

Expose an http post endpoint in the Python application that accepts a call indicating that the Java service has completed its work.

Java calls Python back to indicate completion and pass back results. Usually the initial call that Python makes to Java includes info to identify the call and this identifier is passed back in the callback allowing Python to tie everything together.

The advantage of this approach is that, because it uses http for all integration, it is not tied to the implementation technology on either end. It allows scalability, location independence and a microservice type architecture.

Implementation notes

The payload in initial call from Python to Java must include a url where the Python http server is running, for Java to call Python back on when it’s completed the work. For example the following request body (if we are POSTing to Java):

{
    “callbackUrl”: “http://10.0.0.2:8000/callback?job_id=123”,
    “foo”: “bar”
}

When complete, Java (java.net.http.HttpClient or Spring RestTemplate) POSTs the results to the callbackUrl provided and identifies the job (if required.)

POST http://10.0.0.2:8000/callback?job_id=123
{
    “resultFoo”: true,
    “resultBar”: 10
}

In Python create a function to accept the callback, eg with flask

@api.route('/callback', methods=['POST'])
def post_callback():
    
  jobId = request.args.get('job_id')
  #link back to original call with JobId

  request_data = request.get_json()
  # used the posted body data, eg request_data.resultFoo

  return json.dumps({"success": True}), 201)
John Williams
  • 4,252
  • 2
  • 9
  • 18
0

I think that this is a very common question that should be better formulated as "how can backend notify frontend on some changes". So this question has been asked before. Here is one of those questions with good answer to it: How to continues send data from backend to frontend when something changes

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36