Communicating between PHP and Java using ActiveMQ/Stomp

跟風遠走 提交于 2020-01-04 02:42:08

问题


Background

I have two services that need to communicate with each other over a message queue. One is a legacy service written in PHP and the other is in Java. Sooner than later, the PHP service will be rewritten in Java. The current way they communicate with each other is to write to a shared database, which the other service polls. This is what I'm trying to get away from and replace with a message queue.

Problem

The communication I'm working on right now is from the PHP service to the Java service. It needs to send a relatively complex object (strings and and integers and lists and maps of strings and integers). Ideally, the solution would be workable in PHP and ideal in Java, as that's going to be the legacy of this project.

Possible Solutions

    1.

回答1:


The solution that I've come up with is to enqueue messages from PHP/Stomp using the jms-json-object transformation. The messages are specially crafted JSON such that when they are dequeued in Java using readObject, they can be reconstructed to a full object using XStream. This reconstruction happens internal to ActiveMQ and I just have to make sure that the necessary aliases and converters are in place. When we finally replace the PHP component with Java, sending a message will just be a matter of using writeObject.




回答2:


There is a STOMP client for PHP,

http://pecl.php.net/package/stomp




回答3:


In my view you should keep component interactions free from specific implementations. ActiveMQ is buggy and many systems removed it in favour of RabbitMQ or Sun OpenMQ. You have to avoid coding "send message" routines in PHP, create "SendMessage" servlet instead, and use curl to post JSON-encoded message. The servlet will then use connection factory etc. With a little overhead you will free PHP component from any mq-server specific code. You may use TextMessage with JSON'ed load, encoding and decoding it easily with Jackson.



来源:https://stackoverflow.com/questions/3070869/communicating-between-php-and-java-using-activemq-stomp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!