Announcement Announcement Module
Collapse
No announcement yet.
Could anybody share a working example of Spring 2.0 and ActiveMQ 4.1.1? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Could anybody share a working example of Spring 2.0 and ActiveMQ 4.1.1?

    I'm facing problems when configuring an embedded broker.

    If your example works, could you please send it to me at dxxvi@hatrang.net?

    Thanks.

  • #2
    Spring Framework & ActiveMQ Sample

    Hello,

    Here is a SpringFramework & ActiveMQ sample with a message sender and a message receiver :

    JMS Message Sender Sample
    Code:
    public class SampleSender {
    
        protected JmsTemplate jmsTemplate;
    
        public void simpleSend(final String message) {
            this.jmsTemplate.convertAndSend(message);
        }
    
        public void setJmsTemplate(JmsTemplate jmsTemplate) {
            this.jmsTemplate = jmsTemplate;
        }
    }
    JMS Message Listener Sample
    Code:
    public class SampleListener implements MessageListener {
    
        private final static Logger logger = Logger.getLogger(SampleListener.class);
    
        protected int receivedMessagesCounter;
    
        public void onMessage(Message message) {
    
            logger.debug("> SampleListener.onMessage");
            logger.debug(message);
            receivedMessagesCounter++;
            logger.debug(new Date() + "< SampleListener.onMessage");
        }
    
        public int getReceivedMessagesCounter() {
            return receivedMessagesCounter;
        }
    }
    Spring Framework Configuration File
    HTML Code:
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:lang="http://www.springframework.org/schema/lang"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                               http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">
    
        <bean id="activeMqConnectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
            <property name="brokerURL" value="vm://localhost?broker.persistent=false&amp;broker.useJmx=false" />
        </bean>
    
        <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
            <property name="targetConnectionFactory" ref="activeMqConnectionFactory" />
        </bean>
    
        <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
            <property name="connectionFactory" ref="connectionFactory" />
            <property name="defaultDestinationName" value="defaultQueue" />
            <property name="pubSubDomain" value="false" />
        </bean>
    
        <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
            <property name="concurrentConsumers" value="5" />
            <property name="connectionFactory" ref="connectionFactory" />
            <property name="destinationName" value="defaultQueue" />
            <property name="messageListener" ref="messageListener" />
        </bean>
    
        <bean id="queueSender" class="fr.xebia.sample.springframework.jms.SampleSender">
            <property name="jmsTemplate" ref="jmsTemplate" />
        </bean>
    
        <bean id="messageListener" class="fr.xebia.sample.springframework.jms.SampleListener" />
    
    </beans>
    Test Case
    Code:
    public class SpringFrameworkJmsTest extends AbstractDependencyInjectionSpringContextTests {
    
        protected SampleSender sampleSender;
    
        protected SampleListener sampleListener;
    
        protected String[] getConfigLocations() {
            return new String[]{"classpath:fr/xebia/sample/springframework/jms/beans.xml"};
        }
    
        public void setSampleListener(SampleListener sampleListener) {
            this.sampleListener = sampleListener;
        }
    
        public void setSampleSender(SampleSender sampleSender) {
            this.sampleSender = sampleSender;
        }
    
        public void testSimpleSendJmsMessage() throws Exception {
    
            int numberOfMessages = 100;
            for (int i = 0; i < numberOfMessages; i++) {
                sampleSender.simpleSend("hello world");
            }
            Thread.sleep(5 * 1000);
    
            int actual = sampleListener.getReceivedMessagesCounter();
            assertEquals(numberOfMessages, actual);
        }
    }
    Hope this helps,

    Cyrille

    Comment

    Working...
    X