Spring JMS接收主题消息

前端之家收集整理的这篇文章主要介绍了Spring JMS接收主题消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在编写一个简单的教程.我有一个发布者,它发送关于主题的消息和订阅者以接收它.当我启动应用程序时,Spring配置文件加载,然后我收到以下错误

  1. 2011-10-20 21:50:39,340 DEBUG [org.springframework.jms.support.destination.JndiDestinationResolver] - Located object with JNDI name [RateTopic]
  2. 2011-10-20 21:50:39,340 DEBUG [org.springframework.jms.connection.CachingConnectionFactory] - Closing cached Session: ActiveMQSession {id=ID:Reverb0253-PC-62259-1319161839013-0:1:3,started=true}
  3. 2011-10-20 21:50:39,340 DEBUG [org.springframework.jms.connection.CachingConnectionFactory] - Closing cached Session: ActiveMQSession {id=ID:Reverb0253-PC-62259-1319161839013-0:1:2,started=true}
  4. 2011-10-20 21:50:44,348 WARN [org.springframework.jms.listener.DefaultMessageListenerContainer] - Setup of JMS message listener invoker Failed for destination 'RateTopic' - trying to recover. Cause: Destination [RateTopic] is not of expected type [javax.jms.Queue]
  5. org.springframework.jms.support.destination.DestinationResolutionException: Destination [RateTopic] is not of expected type [javax.jms.Queue]
  6. at org.springframework.jms.support.destination.JndiDestinationResolver.validateDestination(JndiDestinationResolver.java:147)
  7. at org.springframework.jms.support.destination.JndiDestinationResolver.resolveDestinationName(JndiDestinationResolver.java:112)
  8. at org.springframework.jms.support.destination.JmsDestinationAccessor.resolveDestinationName(JmsDestinationAccessor.java:100)
  9. at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.createListenerConsumer(AbstractPollingMessageListenerContainer.java:221)
  10. at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.initResourcesIfNecessary(DefaultMessageListenerContainer.java:1081)
  11. at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1057)
  12. at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1050)
  13. at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:947)
  14. at java.lang.Thread.run(Thread.java:722)

为什么春天认为它应该是一个队列而不是主题

我的jndi文件看起来像这样

  1. java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
  2. java.naming.provider.url = tcp://localhost:61616
  3. java.naming.security.principal=system
  4. java.naming.security.credentials=manager
  5. connectionFactoryNames = TopicCF
  6. topic.RateTopic = RateTopic

spring配置文件

  1. diobjectfactorybean">

我的订阅者实现了MessageListener

  1. @Override
  2. public void onMessage(Message message) {
  3. try {
  4. // Get the data from the message
  5. BytesMessage msg = (BytesMessage) message;
  6. double newRate = msg.readDouble();
  7. // If the rate is at least 1 point lower than the current rate,then
  8. //recommend refinancing
  9. if ((currentRate - newRate) >= 1.0) {
  10. System.out.println(
  11. "New rate = " + newRate + " - Consider refinancing loan");
  12. } else {
  13. System.out.println("New rate = " + newRate + " - Keep existing loan");
  14. }
  15. System.out.println("\nWaiting for rate updates...");
  16. } catch (Exception ex) {
  17. ex.printStackTrace(System.out);
  18. System.exit(1);
  19. }
  20. }
  21. public static void main(String argv[]) {
  22. ApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml");
  23. try {
  24. // Run until enter is pressed
  25. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  26. System.out.println("TBorrower application started");
  27. System.out.println("Press enter to quit application");
  28. stdin.readLine();
  29. } catch (IOException ioe) {
  30. ioe.printStackTrace();
  31. }
  32. }
最佳答案
您尝试使用某个主题,但是您没有在DefaultMessageListenerContainer上设置pubSubDomain属性,它默认为“false”,这意味着点对点,这意味着队列而不是主题.因此,错误消息告诉您RateTopic不是javax.jms.Queue.

猜你在找的Spring相关文章