activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From petersk <petersk...@msn.com>
Subject Re: Just receive text from STOMP in java
Date Wed, 08 Sep 2010 22:27:09 GMT

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.

Mime
View raw message