Dynamic Queue creation and consumption
I am looking for a little guidance on the best way to handle this:
Network thingys come and register with us.
We create a queue for them and we start accepting events from them which we place on that queue.
The events are ordered so must follow one-by-one.
The event consumer is independent of the queue-adder so both run at the same time.
There may be 20 or 30 or more network thingys.
My current solution is:
Code:
<bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.DynamicDestinationResolver"/>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory"/>
<property name="destinationResolver" ref="jmsDestinationResolver"/>
<property name="pubSubDomain" value="false"/>
<property name="receiveTimeout" value="5000"/>
</bean>
The java class which adds messages uses:
Code:
String queueName = "net-thing-" + id;
jmsTemplate.send (queueName, new MessageCreator ()
{
public Message createMessage (Session session) throws JMSException
{
return session.createTextMessage (mytext) ;
}
}) ;
The java class which processes messages uses:
Code:
while (true)
{
try
{
Message msg = jmsTemplate.receive (queueName) ;
// do stuff
}
catch (receiveTimeoutException e)
{
// ignore
}
}
I have found that when I call jmsTemplate.receive it blocks which means I need a separate thread to sit around waiting for things to do.
Is there a way that spring can manage this? Is there a way to get Spring to call onMessage (or similar) when a message arrives for that queue without having to define the queue in advance or having to create my own threads to do the event processing.
Any help would be greatly appreciated.
JMS dynamic destination resolution
I am not using jndi, just spring, and developing a jms framework that has to also connect with flex's message service for async handling. I'm passing dynamic dest names and possibly a pub sub type to resolve to a jms Destination. The second wraps a dynamic destination created in the callback
Code:
public Destination resolveJmsDestination(String dest, boolean pubSubDomain) throws JMSException {
return getJmsTemplate().getDestinationResolver().resolveDestinationName(createSession(), dest, pubSubDomain);
}
Code:
public Message createAndReceive(final String dest, final String selector, final boolean pubSubDomain) {
return (Message) getJmsTemplate().execute(new ProducerCallback() {
public Object doInJms(Session session, MessageProducer producer) throws JMSException {
Destination d = getJmsTemplate().getDestinationResolver().resolveDestinationName(session, dest, pubSubDomain);
return session.createConsumer(d, selector).receive();
}
});
}