Blog post

Calling a Python Function From Java

By Raj Bala | February 07, 2020 | 0 Comments

I spend a great deal of time thinking about serverless computing and what it means to application development, operations, etc.

But one of the most understated aspects from the perspective of a software developer is that I can mix and match programming languages and frameworks like never before.

Writing a Java application, but you want to take advantage of Python’s rich data science libraries? Invoke those Python capabilities using your choice of functions as a service (FaaS) platform!

The way you do this when using Python on AWS Lambda is like this:

response = client.invoke(
FunctionName='MyJavaFunction',
InvocationType='Event',
LogType='Tail',
ClientContext='string',
Payload=b'bytes',
Qualifier='string'
)

MyJavaFunction is another Lambda function that could be Java, Go, Ruby or whatever the platform supports. The Payload parameter is the other important part. Typically, I use byte-encoded JSON payloads that are minimal in size. The called function gets an event parameter with the byte-encoded JSON.

Want to do something similar on Azure? It’s called a Durable Function.

Ultimately, this is the benefit of single-purpose microservices. You can use the right tool for the right job rather than being constrained to one language, one framework or even one database.

Leave a Comment