You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by petersk <pe...@msn.com> on 2010/09/08 23:37:17 UTC

Just receive text from STOMP in java

I'd like to simply just be able to receive text sent via Python in STOMP
using java/Spring.  Is there ANY way to do this?  I see in this web site:
http://activemq.apache.org/stomp.html
that ActiveMQ provides a STOMP API.  Is THAT what everyone is using to
receive STOMP text?  Otherwise, I assume everyone's using the JSON converter
method?
  Just looking for some best practices here?  Is there a "java-based" on
message listener thread to use like the Spring
DefaultMessageListenerContainer?

I've done A LOT of internet searching and really don't see any solutions
presented other than the mapped message solution or JSON solution.  Why
can't it just be simple to just get some text?

Kurt
-- 
View this message in context: http://activemq.2283324.n4.nabble.com/Just-receive-text-from-STOMP-in-java-tp2532079p2532079.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: Just receive text from STOMP in java

Posted by petersk <pe...@msn.com>.
OK, contrary to some information out there, the jms onMessage as part of
Spring will process stomp as a text message. This post is misleading, at
best:http://www.apacheserver.net/ActiveMQ-JMS-API-hangs-when-trying-to-start-connection-over-Stomp-at214579.htm

Below is an example of my configuration that actually allowed me to see the
text message with the listener.
{{{
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>

  <!-- an embedded broker - monitor it at http://localhost:8161/admin-->

  <bean id="broker" class="org.apache.activemq.broker.BrokerService"
init-method="start">
    <property name="transportConnectorURIs">
      <list>
        <value>tcp://localhost:9999</value>
        <value>stomp://localhost:9998</value>
      </list>
    </property>
    <property name="brokerName">
    	<value>myBroker</value>
    </property>
  </bean>
  

  <!-- JMS ConnectionFactory to use -->
  <bean id="jmsFactory"   
class="org.apache.activemq.ActiveMQConnectionFactory" depends-on="broker">
    <property name="brokerURL" value="tcp://localhost:9999" />
  </bean>
    
  <!-- Spring JMS Template -->
  <bean id="consumerJmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="jmsFactory"/>
  </bean>
  
  <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic"
autowire="constructor">
    <constructor-arg>
      <value>manager</value>
    </constructor-arg>
  </bean>
  
	<!-- a jms POJO consumer -->
	<bean id="consumer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="jmsFactory" />
		<property name="destination" ref="destination" />
		<property name="messageListener" ref="messageListener"/>
		<!-- property name="messageSelector" value="type = 'a' or type = 'b'"/-->
		<property name="sessionTransacted" value="true"/>
		<!-- property name="cacheLevelName" value="CACHE_CONNECTION" -->
		<property name="clientId" value="jmsclient"/>
	</bean>
		
	<bean id="messageListener" class="org.jms.JmsSynchronousListener">
		<property name="fileName" value="messageStore-"/>
		<property name="clientID" value="<myid>"/>
		<property name="messageType" value="type = 'a or type = 'b'"/>
		<property name="saveMessages" value="true"/>
	</bean>
</beans>
}}}

python code:
{{{
#!/usr/bin/env python
import stomp
import time
import logging
import sys

logging.basicConfig()

class MyListener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error %s' % message)
    def on_message(self, headers, message):
        for k,v in headers.iteritems():
            print('header: key %s , value %s' %(k,v))
        print('received message\n %s'% message)


dest='/topic/manager'
conn=stomp.Connection([('localhost',9998)])
print('set up Connection')
conn.set_listener('somename',MyListener())
print('Set up listener')

conn.start()
print('started connection')

conn.connect(wait=True)
print('connected')
conn.subscribe(destination=dest, ack='auto')
print('subscribed')

message='hello cruel world'
conn.send(message=message,
destination=dest,headers={'type':'a','MessageNumber':21},ack='auto')
print('sent message')
time.sleep(2)
print('slept')
conn.disconnect()
print('disconnected')
}}}

I hope this saves someone a bunch of work and searching.

-- 
View this message in context: http://activemq.2283324.n4.nabble.com/Just-receive-text-from-STOMP-in-java-tp2532079p2532135.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.