activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From czy11421 <czy11...@gmail.com>
Subject Spring JMS : can't receive message ?
Date Mon, 21 Sep 2009 01:10:07 GMT
Hi, All,

I have tried to write a JMS with Spring, but failed. no compiler error, 
no runtime error, just wait infinitely, can't receive message, but I am 
sure the ActiveMQ is publishing data in 'STOCKS.SUNW' topic.

Anybody has some clues ? Thanks.

///////////////////////// MessageReceiver.java /////////////////////////
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;

public class MessageReceiver {
    private JmsTemplate jmsTemplate;   

    public MessageReceiver() {
    }

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }   
   
    public void receiveMessage() {   
        System.out.println("receiving");
       
        Message message = jmsTemplate.receive(); // wait infinitely
        System.out.println("Message="+message); // never reach here
        TextMessage textMessage = null;
        if (message instanceof TextMessage) {
            textMessage = (TextMessage) message;
            //....
        }
    }
}

/////////////////////////////////

//////////////////////// spring beans xml /////////////////////
<?xml version="1.0"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean id="activeMQConnectionFactory" 
class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
    </bean>

    <bean id="sampleJmsTemplate" 
class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" 
ref="activeMQConnectionFactory" />
        <property name="defaultDestination">
            <ref bean="defaultDestination" />
        </property>
       
        <property name="receiveTimeout">
      <value>3000000</value>
    </property>
       
    </bean>

    <bean id="messageReceiver" class="com.MessageReceiver">
        <property name="jmsTemplate" ref="sampleJmsTemplate" />
    </bean>
  
    <bean id="defaultDestination" 
class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="STOCKS.SUNW" />
    </bean>
</beans>

/////////////////////////// main class /////////////////////////

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = new 
ClassPathXmlApplicationContext("appContext.xml");

        MessageReceiver receiver = (MessageReceiver) 
ctx.getBean("messageReceiver");

        receiver.receiveMessage();

    }

}

Mime
View raw message