【问题标题】:Create Shared-Durable subscriber with Spring-boot使用 Spring-boot 创建 Shared-Durable 订阅者
【发布时间】:2018-07-31 06:39:28
【问题描述】:

我想使用 spring-boot 和 IBM MQ 创建一个共享持久订阅者。我已经做了一个持久订阅者,但是共享订阅失败了。

调试程序时发现空指针异常问题

java.lang.NoSuchMethodError: javax.jms.Session.createSharedDurableConsumer(Ljavax/jms/Topic;Ljava/lang/String;Ljava/lang/String;)Ljavax/jms/MessageConsumer;

在类 AbstractMessageListenerContainer 中,方法 createConsumer。这是因为它尝试调用 session.createSharedDurableConsumerMethod ,其中会话对象是 javax.jms.Session 并且 IntelliJ 将其指向库 geronimo-jms_1.1_spec-1.1.1.jar强>

我的 pom.xml 使用的是 JMS 2.0:

<dependency>
   <groupId>javax.jms</groupId>
   <artifactId>javax.jms-api</artifactId>
   <version>2.0.1</version>
</dependency>

这里是java代码:

import com.ibm.mq.jms.*;

...

public class JmsConfig {

...

@Bean
public MQTopicConnectionFactory mqTopicConnectionFactory(){
    MQTopicConnectionFactory factory = new MQTopicConnectionFactory();
    try{
        factory.setHostName(mqHostname);
        factory.setChannel(mqChannel);
        factory.setPort(Integer.parseInt(mqPort));
        factory.setQueueManager(mqQManager);
        factory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
        factory.setClientReconnectTimeout(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return factory;
}

@Bean
@Primary
UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(MQTopicConnectionFactory mqTopicConnectionFactory) {
    UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
    userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqTopicConnectionFactory);
    userCredentialsConnectionFactoryAdapter.setUsername(mqUsername);
    if(!mqPassword.equals("")) {
        userCredentialsConnectionFactoryAdapter.setPassword(mqPassword);
    }
    return userCredentialsConnectionFactoryAdapter;
}

@Bean
public CachingConnectionFactory cachingConnectionFactory(UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) {
    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);
    cachingConnectionFactory.setSessionCacheSize(1000);
    cachingConnectionFactory.setReconnectOnException(true);
    return cachingConnectionFactory;
}


@Bean
public JmsOperations jmsOperations(CachingConnectionFactory cachingConnectionFactory) {
    JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
    jmsTemplate.setPubSubDomain(true);
    jmsTemplate.setReceiveTimeout(2000);
    return jmsTemplate;
}

@Bean
public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) {

    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    configurer.configure(factory, connectionFactory);
    factory.setPubSubDomain(true);//must follow the configurer.configure(), because it will set the factory as default as connectionFactory
    factory.setConcurrency("1");
    factory.setSubscriptionShared(true);
    factory.setSubscriptionDurable(true);
    factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    factory.setClientId(mqClientID);
    return factory;
}
}

是否缺少任何配置,或者需要重写管理 Session 对象的方法以便它可以创建共享持久连接?

【问题讨论】:

  • 您正在将 JMS 2 功能与无法工作的 JMS 1.1 提供程序一起使用。 Afaik IBM MQ 是 JMS 1.1,因此尝试使用 JMS 2 功能根本行不通。
  • MQ 从 V8 开始支持 JMS 2.0
  • 是的,我认为 IBM MQ 现在支持 JMS2.0。但是,连接到服务器时不是抛出异常,而是在 java 运行时出错。我很好奇为什么即使我没有在 pom 中定义,maven 也会加载 jms1.1 lib 作为依赖项。

标签: spring spring-boot ibm-mq publish-subscribe spring-jms


【解决方案1】:

我终于弄明白了为什么程序使用 JMS1.1。这是因为程序的另一部分从 pom 加载了 AWS SQS 库,而该库使用 JMS1.1(geronimo-jms_1.1_spec-1.1.1.jar) 用于 ActiveMQ。

因此,我在 pom 中为 AWS SQS 库添加了排除项,以防止它覆盖 JMS2.0 库。

【讨论】:

    猜你喜欢
    • 2018-01-15
    • 2017-08-12
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2018-08-14
    • 2018-08-06
    相关资源
    最近更新 更多