Experimented with the need to integrate a system made with Java and Python, and to make it easy to operate a system implemented with Java with a Python script. At first, I was wondering if I would decide the protocol myself, but MessagePack-RPC seemed to be very convenient, so I decided to go with it.
The point is that I want to call the method on the Java side from Python and the method on the Python side from Java. Of course, you have to be able to handle arguments and return values. So, this time,
--If you pass a string, you will get a character with Hello returned (hello) --Pass two arguments and get the sum of them returned (add) --Pass an array of numbers and get the total value returned (sum) --Pass an array of numbers and have it return a line graph image created by matplotlib (make_graph)
I will try it. The server side is Python and the client side is Java.
server.py
import msgpackrpc
import matplotlib.pyplot as plt
from PIL import Image
class TestServer(object):
def hello(self, mesg):
print(mesg)
return ("Hello, " + mesg.decode()).encode()
def add(self, a, b):
return a + b
def sum(self, d):
return sum(d)
def make_graph(self, d):
plt.plot(d)
plt.savefig('hoge.png')
f = open('hoge.png', 'rb')
d = f.read()
f.close()
return d
server = msgpackrpc.Server(TestServer())
server.listen(msgpackrpc.Address("localhost", 1985))
server.start()
TestClient.java
import org.msgpack.rpc.Client;
import org.msgpack.rpc.loop.EventLoop;
import org.msgpack.type.Value;
import java.io.*;
import java.util.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
public class TestClient {
public static void main(String[] args) throws Exception {
EventLoop loop = EventLoop.defaultEventLoop();
Client client = new Client("localhost", 1985, loop);
int v;
System.out.println(" --- hello ---");
Value result = client.callApply("hello", new Object[]{"miyo"});
System.out.println("Result type:" + result.getType());
String s = new String(result.asRawValue().getByteArray());
System.out.println(s); // Hello, miyo
System.out.println();
System.out.println(" --- add --- ");
result = client.callApply("add", new Object[]{100, 10}); // 110
System.out.println("Result type:" + result.getType());
v = result.asIntegerValue().getInt();
System.out.println("result = " + v + " :" + (v == 110));
System.out.println();
System.out.println(" --- sum --- ");
result = client.callApply("sum", new Object[]{new int[]{1, 2, 3, 4, 5}}); // 15
System.out.println("Result type:" + result.getType());
v = result.asIntegerValue().getInt();
System.out.println("result = " + v + " :" + (v == 15));
System.out.println();
System.out.println(" --- make graph ---");
result = client.callApply("make_graph",
new Object[]{new int[]{1,2,4,8,10,3,6,8,9,-1}});
System.out.println("Result type: " + result.getType());
byte[] raw = result.asRawValue().getByteArray();
BufferedImage img = ImageIO.read(new ByteArrayInputStream(raw));
System.out.println(img);
Icon icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null,
"",
"image",
JOptionPane.PLAIN_MESSAGE,
icon);
client.close();
loop.shutdown();
}
}
The state of the executed terminal looks like this
I was able to display the graph generated by Python properly
No, it's convenient !!
I didn't make a note because I used it as a reference ...
Recommended Posts