未设置ActiveMQ Artemis ClientID

时间:2019-01-24 16:24:20

标签: java spring-boot jms activemq-artemis

我很难在ActiveMQ Artemis中为我的使用者设置客户端ID。

我正在使用Spring创建ConnectionFactoryContainerFactory,然后使用它来设置ClientID。由于某些原因,它没有出现在Artemis控制台上。

以下是在我的配置类上定义工厂bean的代码:

@Bean
public ConnectionFactory myConnectionFactory() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(host);
    connectionFactory.setUser(user);
    connectionFactory.setPassword(password);

    // I have tried setting it here
    connectionFactory.setClientID("myClient");
    connectionFactory.setGroupID("myClient");
    return connectionFactory;
}

@Bean
public DefaultJmsListenerContainerFactory myContainerFactory(@Qualifier("myConnectionFactory") ConnectionFactory connectionFactory,
                                 DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setRecoveryInterval(10000);

    // I have tried setting it here
    factory.setClientId("myClient");
    configurer.configure(factory, connectionFactory);
    return factory;
}

这是我根据ListerEnum创建一堆侦听器的代码:

  for (ListersEnum listener : ListenersEnum.values()) {

        IListener handler = BeanUtil.getBean(listener.getHandler());
        DefaultJmsListenerContainerFactory containerFactory = BeanUtil.getBean<DefaultJmsListenerContainerFactory>(listener.getContainerFactory());

        SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
        endpoint.setId(listener.getId());

        endpoint.destination = listener.getQueue();
        endpoint.setMessageListener(message -> handler.onReceive(message));

        DefaultMessageListenerContainer messageListenerContainer = containerFactory.createListenerContainer(endpoint);

        // I have tried setting it here also
        messageListenerContainer.setClientId("myClient");

        endpoint.setupListenerContainer(messageListenerContainer);

        jmsListenerEndpointRegistry.registerListenerContainer(endpoint, containerFactory);

        messageListenerContainer.start();
  }

然后在Artemis控制台上,未设置客户端ID:

Artemis screenshot

除此之外,我注意到控制台上的Creation Time每秒更新一次。就像连接每次都在刷新。这是正常行为吗?与客户ID有关系吗?

我的依赖者是:

springBootVersion = '1.5.15.RELEASE'

compile 'org.springframework.boot:spring-boot-starter-artemis'
compile 'org.apache.activemq:artemis-commons:1.4.0'
compile 'org.apache.activemq:artemis-core-client:1.4.0'
compile 'org.apache.activemq:artemis-jms-client:1.4.0'
compile 'org.apache.activemq:artemis-selector:1.4.0'

感谢您的帮助

编辑

我将myContainerFactory更改为:

public DefaultJmsListenerContainerFactory myContainerFactory(@Qualifier("myConnectionFactory") ConnectionFactory connectionFactory,
                             DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setRecoveryInterval(10000);

    // I have tried setting it here
    factory.setClientId("myClient");

    // Commented this so Spring wont set its default values
    // configurer.configure(factory, connectionFactory);

    factory.setConnectionFactory(connectionFactory);

    return factory;
}

然后我开始学习

2019-01-24 17:50:28.612 ERROR 25504 --- [enerContainer-1] o.s.j.l.DefaultMessageListenerContainer  : Could not refresh JMS Connection for destination 'TEST_QUEUE' - retrying using FixedBackOff{interval=5000, currentAttempts=9, maxAttempts=unlimited}. Cause: Client id has already been set

我尝试通过将我的ClientID设置为"myClient" + new Date().getTime()来对其进行破解,但是得到了相同的结果。奇怪的是,如果我完全删除了clientId,在artemis控制台上我只会看到一个连接...那么它如何尝试创建具有相同客户端ID的另一个连接?有什么想法吗?

编辑2

我在Spring之外举了一个例子,看看这是否是Spring的问题...但是事实并非如此。我得到了相同的结果:客户端ID没有显示在控制台上,如果在Connection上进行了设置,则会出现Client ID already set错误。可以关于我的artemis控制台...吗?我正在使用Apache ActiveMQ Artemis 2.6.2。请暂停:s

在此处提供代码,以防有人尝试:

import javax.jms.*;

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;

public class Consumer {

    public static void main(String[] args) throws JMSException {

        Connection connection = null;
        Session session = null;
        MessageConsumer consumer = null;
        ConnectionFactory factory = null;
        ActiveMQConnectionFactory artemisCf = null;

        try {
            artemisCf = new ActiveMQConnectionFactory("tcp://localhost:61616?retryInterval=1000reconnectAttempts=-1",
                    "user", "pass");
            artemisCf.setRetryInterval(1000);
            artemisCf.setReconnectAttempts(-1);
            artemisCf.setClientID("TESTE");

            factory = (ConnectionFactory) artemisCf;

            connection = factory.createConnection();

            // If I uncomment this I get the "Client id has already been set" error
            // connection.setClientID("TESTE");

            session = connection.createSession(true, 2);

            Queue queue = session.createQueue("QUEUE.TESTE.CONNCETION");

            consumer = session.createConsumer(queue);

            consumer.setMessageListener(new Listener("Me"));

            connection.start();

            Thread.sleep(2000000);

        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            if (consumer != null && session != null && connection != null) {
                consumer.close();
                session.close();
                connection.close();
            }
        }
    }

    public static class Listener implements MessageListener {

        private String listenerName;

        public Listener(String consumerName) {
            this.listenerName = consumerName;
        }

        public void onMessage(Message message) {
            TextMessage textMessage = (TextMessage) message;
            try {
                System.out.println(listenerName + " received " + textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

0 个答案:

没有答案