如何将数据从 Perl 传递到 Java?
我正在研究一些 Java <-> Perl 交互。 我想知道将信息从 Perl 传递到 Java 的最佳方法是什么。 (有关 Perl 和 Java 的精彩答案此处和< a href="https://stackoverflow.com/questions/603163/is-perl-a-good-option-for-heavy-text-processing/603837#603837">这里顺便说一句)。
我正在 Perl 中解析大量文本和 XML(XML::Twig),在我应该从 Java Web 应用程序调用的脚本中。 所以我拥有所有收集到的数据,并且我需要它来在 Java 中的某些对象中使用它。
将信息从 Perl 发送到 Java 的好策略是什么? 是否有可能将对象或其他兼容的数据结构从 Perl 返回到 Java?
我猜想写入一个文本文件并从 Java 中读取它会使使用 Perl 获得的所有优化变得毫无意义。
性能在这里是一个重要问题。
编辑: 从我所看到的这里,也许 Inline-Java 是一个好的选择?
I'm working on some Java <-> Perl interaction. I would like to know what the best way is to pass information from Perl to Java. (Great answers about Perl and Java here and here btw).
There's a lot of text and XML(XML::Twig) I'm parsing in Perl, in a script I'm supposed to call from a Java Web App. So I have all this gathered data, and I need it to use it inside certain objects in Java.
What could be a good strategy to send the information from Perl to Java? Is it even possible to return an object or other compatible data structure from Perl to Java?
I guess writing to a text file and reading it from Java would make all the optimization gained by using Perl meaningless.
Perlformance is an important issue here.
EDIT:
From what I've seen here, maybe Inline-Java would be a good option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果性能很重要,我建议运行一个持久的 Perl 进程。 每次要运行代码时启动 Perl 解释器将是相当大的开销。
最简单的通信方式是 Java 进程打开到 Perl 进程的 TCP 连接,写入一些数据并取回一些数据。
用于将数据从 Perl 进程发送回 Java 进程的格式取决于您发送的内容以及您希望代码的通用性和可重用性。 发送回 XML 字符串将是很好且通用的,但发送回使用 pack 创建的字节数组Perl,然后使用 DataInputStream 在 Java 中会快得多。
If performance is important I'd recommend having a persistent Perl process running. Starting a Perl interpreter every time you want to run your code will be quite an overhead.
The easiest way to communicate is for the Java process to open a TCP connection to the Perl processs, write some data and get some back.
The format you use to send data to from your Perl process back to your Java one will depend on what you're sending and how generic and re-usable you want your code to be. Sending back XML strings will be nice and generic but sending back byte arrays created with pack in Perl and then read a with DataInputStream in Java will be much, much faster.
JSON 是一种简单、轻量级的数据传递格式。
您可以添加 Rhino 或类似于您的工具包的东西,并且可能会获得额外的性能(更不用说脚本引擎),但这取决于您计划项目的复杂程度。
JSON is an easy, lightweight format for passing data around.
you could add Rhino or something similar to your toolkit and may gain additional performance (not to mention a scripting engine), but this will depend on how complex you are planning your project to be.
如果您已经有了 XML,那么最好的选择可能是继续使用它。
if you've got XML already, your best option is probably to continue using it.
Inline::Java。 一旦你启动并运行它,它就会非常有效。 几年前,我正在开发一个项目,其中有一个 Perl Web 应用程序与 Java SOAP 服务器进行实时通信,但速度太慢了。 我们将其替换为使用 Inline::Java 进行通信的系统,并且速度更快。 一定要尽量减少来回传递对象的次数,并尝试使这些对象变得简单(我们坚持使用字符串、数字和数组),以防止事情变得过于失控。 但我绝对是一个皈依者!
Inline::Java. It works really well once you get it up and running. I was working on a project a few years ago that had a Perl web application talking to a Java SOAP server for some realtime communication and it was just way too slow. We replaced it with a system using Inline::Java for the communication and it was much faster. Definitely minimize the points that you pass objects back and forth and try to make those objects simple (we stuck with strings, numbers and arrays) to keep things from getting too out of control. But I'm definitely a convert!
我还发现奇怪的是,用于解析 XML 的 perl 代码比 java 中的等效代码要快得多。 即使是这样,我也无法想象它会比 IPC 产生的开销更快。 即使您使用持久的 Perl 进程,您仍然必须通过套接字向其发送数据,然后取回一些数据,最后将其反序列化为可用的内容。
您是否尝试过提高 Java 中 XML 解析的性能? 如果您使用 DOM 或 JDOM 或 Castor 等第三方库,请尝试使用 SAX。 或者也许只使用正则表达式而不是解析 XML 会更快(jwz 尽管如此) 。
无论如何,我建议首先对您的 java 代码使用探查器,看看是否可以改进。
I also find it strange that your perl code for parsing XML would be significantly faster than the equivalent code in java. Even if it is, though, I can't imagine that it would ever be faster than the overhead incurred by the IPC. Even if you use a persistent perl process, you're still going to have to send it data, presumably over a socket, then get some data back, and finally deserialize it into something usable.
Have you tried improving the performance of your XML parsing in java? If you're using DOM or a third-party library like JDOM or castor, try using SAX instead. Or maybe just using regular expressions instead of parsing the XML would be faster (jwz notwithstanding).
In any case, I would recommend using a profiler with your java code first to see if it can be improved.