PHP to Java "signals"

I’m not sure how to phrase this, so bear with me. I want to execute some Java code in response to an HTTP request handled by PHP. For example, an HTTP request causes a PHP file to update a MySQL database and I want to have a thread in a Java server program wake up and query the database and see what the new value is. I don’t want to launch a new Java program, just signal/create an event so that an already running Java program will handle the event. Note I am using event and signal in the broadest possible sense.

I know I could just have a thread sleep and check the database every so often, but that seems inefficient. I don’t need details, just a general direction. Do I need to do this with sockets? Or is there a better way?

Maybe try this (I’ve never used it; just found it in a quick Google search): PHP/Java Bridge

Seems like the easiest thing is to just make the Java app listen on the network for a message (likely just an HTTP request) that the PHP script can send. Should be fairly easy. That might be what that PHP/Java Bridge thing is doing – I didn’t read any of their docs.

I’d just run a separate Java web server with the code as a servlet and send a simple HTTP request from PHP to that. That way, you can use bog-standard libraries to communicate.

The php-java-bridge thing linked above seems to use the same scheme but also introduces some kind of XML format and bi-directional function/method calling, which is probably overkill for your application. Personally, if I needed complicated requests or responses, I’d choose JSON over XML.

Have you considered using an actual signal handler? That’s exactly what OS signals are for. Write some Java code to handle a SIGUSR1 (for example) then have your PHP script send a ‘kill -USR1’ to the JVM process. A quick walk through Google turned up this example - Java – Signal Handling : twit88.com

Thanks, those are both really good suggestions

After a cup of coffee, it occurs to me to mention the OS signal is better if it’s just a console app running in a vanilla JVM. This has the advantage of being completely testable outside of an HTTP session. If however it is already running in a web servlet container or for some reason already has the plumbing to handle HTTP requests, then it’s preferable to use http requests. The container may already have its own handling of OS signals and probably prefers that you not muck around with them.