Spring JMS:重新启动JMS Broker(Wildfly HornetQ)时恢复JMS消息侦听器

时间:2019-07-10 12:10:34

标签: java spring jms

我有一个正在运行的Spring JMS消息侦听器。但是,一旦我重新启动JMS代理(在此处使用Wildfly),侦听器就无法恢复,我会收到许多如下警告:

  

DefaultMessageListenerContainer:针对目标“ my_queue”的JMS消息侦听器调用程序的设置失败-尝试恢复。原因:HQ119001:创建会话失败

我怀疑重新启动Wildfly服务器时JNDI ConnectionFactory无效,但是我不知道如何“刷新” ConnectionFactory bean。

我使用以下配置:

@Configuration
public class ConnectionFactoryConfig {

  // ... fields here

  @Bean
  public InitialContext initialContext() throws NamingException {
    final Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, namingIntitalContextFactory);
    env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
    env.put(Context.PROVIDER_URL, jnpProviderUrl);
    env.put(Context.SECURITY_PRINCIPAL, jmsUsername);
    env.put(Context.SECURITY_CREDENTIALS, jmsPassword);
    return new InitialContext(env);
  }

  @Bean
  public SingleConnectionFactory connectionFactory(InitialContext initialContext) throws NamingException {
    ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup(jmsConnectionFactory);
    UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
    userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(connectionFactory);
    userCredentialsConnectionFactoryAdapter.setUsername(jmsUsername);
    userCredentialsConnectionFactoryAdapter.setPassword(jmsPassword);
    return new SingleConnectionFactory(userCredentialsConnectionFactoryAdapter);
  }

}
@Configuration
@EnableJms
public class JmsConfig {

  @Bean
  public JmsListenerContainerFactory<?> jmsListenerContainerFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    configurer.configure(factory, connectionFactory);
    return factory;
  }

}
@Configuration
@RequiredArgsConstructor
public class JmsListenerConfig implements JmsListenerConfigurer {

  private static final String RESPONSE_QUEUE_NAME = "my_queue";

  private final JmsInterface jmsInterface;

  @Override
  public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
    SimpleJmsListenerEndpoint endpointListener = new SimpleJmsListenerEndpoint();
    endpointListener.setId(RESPONSE_QUEUE_NAME + "endpoint");
    endpointListener.setDestination(RESPONSE_QUEUE_NAME);
    endpointListener.setMessageListener(jmsInterface);
    registrar.registerEndpoint(endpointListener);
  }

}

侦听器应该能够从Wildfly重新启动中恢复并继续接收JMS消息。

0 个答案:

没有答案