activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yury Batrakov <batra...@gmail.com>
Subject Active MQ sends empty message bodies via Stomp
Date Wed, 01 Apr 2009 15:47:46 GMT

Hi all!

I'm trying to force Ruby and Java code to interoperate between each other by
means of ActiveMQ's stomp. I already have a Java classes that send
ObjectMessages to each other and want these messages to be delivered and be
unmarshalled in Ruby code.  I read http://activemq.apache.org/stomp.html and
StompTest.java and written the code. The problem is that the Ruby script
receives messages with empty body. I am sure that object messages  messages
are properly marshalled to XML. I also ensured that TextMessages are
delivered ok.

Here is my code, I'll be very appreciated if  someone points me what is
wrong with it:
// StompSender.java
package ru.my.test;

import javax.jms.Connection;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.transport.stomp.Stomp;

public class StompSender
{
    public Connection connection = null;
    public Session session = null;
    public MessageProducer sender = null;

    public StompSender() throws Exception
    {
        ActiveMQConnectionFactory cf = new
ActiveMQConnectionFactory("tcp://localhost:61616");
        connection = cf.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        sender = session.createProducer(session.createTopic("stompcat"));
        connection.start();
    }

    public static void main(String[] args) {
        try {
            StompSender snd = new StompSender();
            ObjectMessage msg = snd.session.createObjectMessage(new
SamplePojo("10", "20"));
            msg.setStringProperty("transformation",
Stomp.Transformations.JMS_OBJECT_XML.toString());
            snd.sender.send(msg);
            snd.connection.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

// SamplePojo.class
package ru.my.test;

import java.io.Serializable;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.XStream;

@XStreamAlias("pojo")
    public class SamplePojo implements Serializable {
        @XStreamAlias("name")
            private String name;
        @XStreamAlias("city")
            private String city;

        public SamplePojo() {
        }

        public SamplePojo(String name, String city) {
            this.name = name;
            this.city = city;
        }


        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public static void main(String[] args) {
            XStream stream = new XStream();
            stream.processAnnotations(SamplePojo.class);
            SamplePojo msg = new SamplePojo("10", "20");
            System.out.println(stream.toXML(msg));
        }
    }

// modified stompcat.rb
require 'rubygems'
require 'stomp'

begin

    @port = 61613
    @host = "localhost"
    @user = ENV["STOMP_USER"];
    @password = ENV["STOMP_PASSWORD"]

    @host = ENV["STOMP_HOST"] if ENV["STOMP_HOST"] != NIL
    @port = ENV["STOMP_PORT"] if ENV["STOMP_PORT"] != NIL

    @destination = "/topic/stompcat"
    @destination = $*[0] if $*[0] != NIL

    $stderr.print "Connecting to stomp://#{@host}:#{@port} as #{@user}\n"
    @conn = Stomp::Connection.open @user, @password, @host, @port, true
    $stderr.print "Getting output from #{@destination}\n"

# None of two lines works
    @conn.subscribe @destination, { :ack=>"client",
:transformation=>"jms-object-xml" }
#    @conn.subscribe @destination, {:ack=>"client"}
    while true
        @msg = @conn.receive
        $stdout.print @msg
        $stdout.flush
        @conn.ack @msg.headers["message-id"]
    end
    @conn.disconnect

rescue
end

Message that is delivered to topic is:
<Stomp::Message headers={"timestamp"=>"1238598757167",
"message-id"=>"ID:storm-38501-1238598756986-0:0:1:1:1", "expires"=>"0",
"destination"=>"/topic/stompcat", "priority"=>"4",
"transformation"=>"jms-object-xml"} body='' command='MESSAGE' >
-- 
View this message in context: http://www.nabble.com/Active-MQ-sends-empty-message-bodies-via-Stomp-tp22829522p22829522.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Mime
View raw message