简体   繁体   中英

ActiveMQ / PHP Stomp use TextMessage instead of BytesMessage

when I send a message to ActiveMQ using PHP-Stomp the message is sent as BytesMessage. However I would like to send an TextMessage.

According to the STOMP documentation this can be achieved by not sending an content-length header:

... The protocol does however support a content-length header. To provide more robust interaction between Stomp and JMS clients, ActiveMQ keys off of the inclusion of this header to determine what message type to create when sending from Stomp to JMS. The logic is simple:

Inclusion of content-length header => Resulting Message

yes => BytesMessage

no => TextMessage

This same logic can be followed when going from JMS to Stomp, as well. A Stomp client could be written to key off of the inclusion of the content-length header to determine what type of message structure to provide to the user.

In my test PHP script I have not defined the content-length header and it seems to be automaticly added. I also tried passing content-length as 0, false or null. In all of these cases it produces a BytesMessage.

So my question is, how do I produce a Textmessage instead of BytesMessage .

My code looks like the following:

<?php

$stomp = new Stomp('tcp://localhost:61613');
$stomp->send('/topic/test.central_message_topic', 'testmessage', [
    "persistent" => "true",
]);

it's been a long time but this worked for me :

try {  
$stomp->send("your_queue", "your_message", array("amq-msg-type" => "text"));
} catch (StompException $e) {  
die('send failed: ' . $e->getMessage());
}

I have found the answer.

TL;DR: It is not possible.

With the PECL-Stomp it is not possible to not send the content-length header, since it is automaticly created when the request is sent. If you pass the reuqest will contain two content-length headers.

For reference the sourcecode .

Add transformation header, using this with activemq:

$stomp->send('/queue/pizzamonsters', json_encode($frame), [
    'transformation' => 'TEXT'
]);
<?php
$stomp = new Stomp('tcp://localhost:61613');
$stomp->send('/topic/test.central_message_topic', 'testmessage', ["content-type" =>"text/plain"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM