具有持久共享订阅的 Artemis JUnit

Artemis JUnit with durable shared subscription

我正在使用 artemis-junit 依赖项

testImplementation("org.apache.activemq:artemis-junit")

使用 Spring Boot 2+,我有 2 个具有以下配置的 JMS 侦听器:

destination = "my.topic"
clientId = "sharedApp1" 
subscription = "mySharedSub"
durable = "true"
shared = "true"

我想验证共享订阅是否有效,但是当我 运行 JUnit 测试第二个监听时抛出

ActiveMQDuplicateMetaDataException[errorType=DUPLICATE_METADATA message=AMQ229035: Metadata jms-client-id=sharedApp1 had been set already]

堆栈跟踪:

javax.jms.JMSException: Failed to create session factory
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:886) ~[artemis-jms-client-2.13.0.jar:2.13.0]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:299) ~[artemis-jms-client-2.13.0.jar:2.13.0]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:294) ~[artemis-jms-client-2.13.0.jar:2.13.0]
at org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:196) ~[spring-jms-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.jms.listener.AbstractJmsListeningContainer.createSharedConnection(AbstractJmsListeningContainer.java:412) ~[spring-jms-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.jms.listener.AbstractJmsListeningContainer.establishSharedConnection(AbstractJmsListeningContainer.java:380) ~[spring-jms-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.jms.listener.DefaultMessageListenerContainer.establishSharedConnection(DefaultMessageListenerContainer.java:818) ~[spring-jms-5.2.7.RELEASE.jar:5.2.7.RELEASE]

这是设计使然(开发人员帮助),因为我 运行宁在同一个 JVM 中?或者我做错了什么?

错误是设计使然。让多个并发 JMS 客户端使用相同的客户端 ID 违反了 JMS 规范。 JMS 2 规范的第 6.1.2 节是这样说的:

The purpose of client identifier is to associate a connection and its objects with a state maintained on behalf of the client by a provider. By definition, the client state identified by a client identifier can be ‘in use’ by only one client at a time. A JMS provider must prevent concurrently executing clients from using it.

请记住,对于 JMS 2 共享持久订阅,客户端 ID 可选。 JMS 2 规范的第 4.2.2 节指出:

A shared durable subscription is identified by name and an optional client identifier, and may have several consumer objects consuming messages from it.

在我看来,您是 运行 同一 JVM 中的测试,并且由于以下验证 (org.apache.activemq.artemis.jms.client.ActiveMQConnection) 而不可能:

private void validateClientID(ClientSession validateSession, String clientID)
     throws InvalidClientIDException, ActiveMQException {
  try {
     validateSession.addUniqueMetaData(JMS_SESSION_CLIENT_ID_PROPERTY, clientID);
  } catch (ActiveMQException e) {
     if (e.getType() == ActiveMQExceptionType.DUPLICATE_METADATA) {
        throw new InvalidClientIDException("clientID=" + clientID + " was already set into another connection");
     } else {
        throw e;
     }
  }
}