开发者

Communicating between PHP and Java using ActiveMQ/Stomp

开发者 https://www.devze.com 2023-01-03 18:53 出处:网络
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

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 rela开发者_Python百科tively 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.


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.


There is a STOMP client for PHP,

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消