douwen3362 2014-08-14 17:36
浏览 26
已采纳

如何在PHP中使用Java? [关闭]

I want to use WordPress for my web development, which is PHP written, including the database connection to MySql. The whole thing is PHP. But I need to use Java to back-end data processing and a number of existing Java open source libraries.

A google search shows that PHP/Java Bridge is a way to go. Is that bridge best way to go? If everything is PHP with WordPress, is still a way to use J2EE technologies, inlcuding JSP, Servelet, etc?

edit

Java is needed becaue I need to run machine learning algorithms, libraries for which are only available for Java. Also, PHP may run into efficiency issues when it's used to process large amount of data.

A good example of libraries in Java I am going to use is those processing Big Data, which are mainly Java, like Hadoop.

  • 写回答

1条回答 默认 最新

  • doujiao1949 2014-08-14 17:48
    关注

    The very simple answer here is don't

    PHP is designed to, at every page request start up, execute a small series of scripts as a single operation, output the data associated with those scripts and then immediately die after generating the output. It literally does not have time to wait for your Java programs and libraries to do their thing, so don't try to put one in the other, which is why PHP scripts that rely on databases tend to have heavily optimised databases for immediate retrieval, instead of general databases that rely on joins and selects that take a few seconds to form the correct data response. Neither PHP or users browsing websites have time for that.

    What you could do is wrap your java tools in Java Servlets and have them running on the same server/host that your PHP instance is running from, so that your scripts can access the Servlets as http://127.0.0.1:7254/... as it would any other restful API it needs to use while generating your script output, as long as you make damn sure that you're not going to make PHP wait: if it has to send data to your tools, that is a post-and-forget operation, PHP should not be getting any response back other than an immediate "data accepted" or "data rejected" before the data is then actually handled by your tools. If you need to post data and then get a result back, you're going to have to use two calls. One to post the data, and then a second call to request the result of that posting.

    For instance:

    • web page generation chain: WordPress CMS based on PHP -> your database
    • web input for processing: WordPress CMS based on PHP -> Java Servlets for machine learning
    • data processing chain: Java Servlets for machine learning -> your database

    So you build pages only based on what's in your data base, you post data to your java Servlets only to get them to start doing something and you don't wait for a response, their result will end up in your database and you'll get it for pages once it's in, and your java programs do what they need to do independently of your WordPress setup.

    And if you're going to do that, you should probably write that functionality as a WordPress plugin that can talk to your Java Servlets.

    And now you have a second project you need to work on: turning your java programs into web servers. Not terribly complex, but definitely something you're going to lose some time on doing right (because you'll need to wrap with servlets, as well as make sure you can have those running without crashing on the same server as your wordpress instance, which is always fun)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line