【问题标题】:activemq-all "5.15.3" does not work with Spring 5activemq-all "5.15.3" 不适用于 Spring 5
【发布时间】:2018-07-17 16:28:49
【问题描述】:

我正在将 Spring 从 4.x.x 更新到 Spring 5.0.3。该项目使用 ActiveMQ 版本 5.15.3。当我尝试使用最新版本的 Spring 部署应用程序时,出现此错误:

Caused by: java.lang.NoSuchMethodError: org.springframework.web.servlet.handler.AbstractHandlerMapping.obtainApplicationContext()Lorg/springframework/context/ApplicationContext;
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.detectMappedInterceptors(AbstractHandlerMapping.java:269)
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.initApplicationContext(AbstractHandlerMapping.java:243)
    at org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.initApplicationContext(SimpleUrlHandlerMapping.java:102)
    at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:120)
    at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:77)
    at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:74)
    at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:121)
    at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:97)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    ... 53 more

我注意到 ActiveMQ 有 Spring 版本“4.3.9”作为依赖项。此版本在“AbstractHandlerMapping”中没有方法“obtainApplicationContext”,因此存在问题。有没有办法从 activemq-all 包中排除 Spring 库?

【问题讨论】:

  • 您使用哪种构建系统?马文?摇篮?
  • 我正在使用 Ant + Ivy

标签: java spring activemq


【解决方案1】:

我也认为这也是我的问题,但我最终将我的 Spring webapp 部署在 TomEE 上,以成功连接并使用在该 Tomcat 容器内部托管和运行的 ActiveMQ。

我正在使用 Spring 5.0.3-RELEASE 和 activemq-client 5.15.3。我不需要 maven 阴影 uber jar activemq-all 中的所有内容。

@Configuration
public class MyConfig {

  @Bean
  public SingleConnectionFactory connectionFactory() {
      ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
      ((ActiveMQConnectionFactory) connectionFactory)
            // See http://activemq.apache.org/objectmessage.html why we set trusted packages
            .setTrustedPackages(new ArrayList<String>(Arrays.asList("com.mydomain", "java.util")));
    return new SingleConnectionFactory(connectionFactory);
}

  @Bean
  @Scope("prototype")
  public JmsTemplate jmsTemplate() {
      return new JmsTemplate(connectionFactory());
  }

  @Bean
  public Queue myQueue() throws JMSException {
      Connection connection = connectionFactory().createConnection();
      connection.start();
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Queue queue = session.createQueue("message-updates");
      return queue;
  }
}

@Component
public class MyQueueImpl implements MyQueue {

  @Inject
  private JmsTemplate jmsTemplate;

  @Inject
  private Queue myQueue;

  @PostConstruct
  public void init() {
   jmsTemplate.setReceiveTimeout(JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT);
  }

  @Override
  public void enqueue(Widget widget) {
      jmsTemplate.send(myQueue, new MessageCreator() {
          @Override
          public Message createMessage(Session session) throws JMSException {
              return session.createObjectMessage(widget);
          }
      });
  }

  @Override
  public Optional<Widget> dequeue() {
    Optional<Widget> widget = Optional.empty();
    ObjectMessage message = (ObjectMessage) jmsTemplate.receive(myQueue);
    try {
        if (message != null) {
            widget = Optional.ofNullable((Widget) message.getObject());
            message.acknowledge();
        }
    } catch (JMSException e) {
        throw new UncategorizedJmsException(e);
    }

    return widget;
  }
}

【讨论】:

    【解决方案2】:

    感谢上面的 Matthew K。我也发现了。 ActiveMQ-all 里面都打包了一个 spring 版本(目前是 4.x 版本)。与 spring v.5 之间有一些不向后兼容的变化。我自己在其他春季课程中遇到了一种新方法。它可能会导致此类问题(在我的情况下没有此类方法异常)。 我在使用 activeMQ 5.15.4 和 spring 5.0.7 时遇到了这个问题。最后我用更细粒度的罐子解决了这个问题。我必须使用所有这些:activemq-broker、activemq-client、activemq-pool、activemq-kahadb-store、activemq-spring

    【讨论】:

      猜你喜欢
      • 2019-11-13
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      相关资源
      最近更新 更多