Spock test and Artemis Embedded server: How to configure a consumer queue on an address?

371 Views Asked by At

I am trying to write a test to make sure my Spring Boot application is able to receive messages from an address after migrating from ActiveMQ "Classic" to ActiveMQ Artemis. As this is a migration from ActiveMQ "Classic" some topic/queue names remain as VirtualTopic... What I am trying to achieve is a publish/subscribe scenario where a message sent on VirtualTopic.audit.events would be received by VirtualTopic.audit.events::junit or any other queue with that address. This is what I mean by "consumer queue." Also some of our older clients (e.g. on WebLogic) still use the OpenWire protocol to connect to our broker.

I am using a jmsMessagingTemplate to send and receive messages, and I have have enabled pubSubdomain in my ConnectionFactory.

From what I can deduce in the logs, I am able to send the message on the topic, but jmsMessagingTemplate.receive(...) on a FQDN of a listening queue on that address never returns (unless spring.jms.template.receive-timeout is set).

Here is my code, and the relevant configuration:

StatusEventSpec

    def 'notify a success status event'() {
        when:
            Message message = buildMessage(false)
            service.notify(message.headers, message.headers.getId(), StatusType.AUDIT_STORE_SUCCESS)
            sleep(1000)
        then:
            Message<String> notifyMessage = jmsMessagingTemplate.receive(config.statusEventDestination + "::junit")
        notifyMessage != null
            StatusEvent event = jsonService.fromString((String) notifyMessage.payload, StatusEvent.class)
            event.id == message.headers.getId().toString()
            event.statusDate != null
            event.status == StatusType.AUDIT_STORE_SUCCESS
            event.event == StatusEvent.CCNCSI_EVENT_STATUS
            event.error == null
    }

application.yml

ccncsi.common.statusEventDestination=VirtualTopic.audit.events
spring.jms.template.receive-timeout=10s
spring.jms.pub-sub-domain=true

I already tried the following:

  • use spring.artemis.embedded.topics=VirtualTopic.audit.events,VirtualTopic.audit.events::junit(test hangs)
  • spring.artemis.embedded.topics=VirtualTopic.audit.events and spring.artemis.embedded.queues=VirtualTopic.audit.events::junit I get error message:
AMQ221080: Deploying address VirtualTopic.audit.events supporting [MULTICAST]
2023-07-05 15:43:06.837  WARN 14508 --- [           main] org.apache.activemq.artemis.core.server  : AMQ222274: Failed to deploy address VirtualTopic.audit.events: AMQ229209: Can't remove routing type ANYCAST, queues exists for address: VirtualTopic.audit.events. Please delete queues before removing this routing type.
  • I also tried to define a DynamicDestinationResolver bean (test hangs)
    @Bean
    public DynamicDestinationResolver dynamicDestinationResolver() {
        return new DynamicDestinationResolver() {
            @Override
            public Destination resolveDestinationName(Session session, String destinationName, boolean pubSubDomain) throws JMSException {
                if(destinationName.contains("VirtualTopic.")) {
                    pubSubDomain = true;
                }
                return super.resolveDestinationName(session, destinationName, pubSubDomain);
            }
        };
    }
  • I also tried using QueueConfiguration and topicConfiguration beans with no success
    @Bean
    public TopicConfiguration topicConfiguration() {
        TopicConfigurationImpl topicConfiguration = new TopicConfigurationImpl();
        topicConfiguration.setName(statusEventDestination);

        return topicConfiguration;
    }

    @Bean
    public QueueConfiguration queueConfiguration() {
        QueueConfiguration queueConfiguration = new QueueConfiguration("junit");
        queueConfiguration.setAddress(statusEventDestination);
        return queueConfiguration;
    }

I suspect that the listening queue on VirtualTopic.audit.events is created after (when jmsMessagingTemplate.receive(..) is called) the message is sent on the embedded broker.

What am I missing?

1

There are 1 best solutions below

4
Themikebe On

I finally got this sorted but I think there is a problem with the Artemis embedded server. I had to resort to create my addresses and queues manually to get the right configuration and add them to the EmbeddedServer configuration to get what I wanted instead of using spring.artemis.embedded.queues and spring.artemis.embedded.topics properties or automatic configuration.

If I use either, I get the following behaviour:

2023-07-11 17:01:07.570  INFO 15344 --- [host-456653804)] org.apache.activemq.audit.base           : AMQ601267: User anonymous@invm:0 is creating a core session on target resource ActiveMQServerImpl::name=localhost [with parameters: [c3b44e30-1ffb-11ee-9a86-025041000001, null, ****, 102400, RemotingConnectionImpl [ID=c3a1b08c-1ffb-11ee-9a86-025041000001, clientID=null, nodeID=c2f03ad7-1ffb-11ee-9a86-025041000001, transportConnection=InVMConnection [serverID=0, id=c3a1b08c-1ffb-11ee-9a86-025041000001]], true, true, false, false, null, org.apache.activemq.artemis.core.protocol.core.impl.CoreSessionCallback@1343f728, true, org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager$2@1f175a6, {}]]
2023-07-11 17:01:07.591  INFO 15344 --- [mpl$6@1f9d4b0e)] org.apache.activemq.audit.base           : AMQ601262: User anonymous@invm:0 is creating address on target resource: c3b44e30-1ffb-11ee-9a86-025041000001 [with parameters: [VirtualTopic.audit.events, [MULTICAST], true]]
2023-07-11 17:01:07.594  INFO 15344 --- [mpl$6@1f9d4b0e)] org.apache.activemq.audit.base           : AMQ601019: User anonymous@invm:0 is getting mbean info on target resource: org.apache.activemq.artemis.core.management.impl.AddressControlImpl@c210412 []
2023-07-11 17:01:07.605  INFO 15344 --- [mpl$6@1f9d4b0e)] org.apache.activemq.audit.message        : AMQ601500: User anonymous@invm:0 is sending a message CoreMessage[messageID=12,durable=true,userID=c3b95741-1ffb-11ee-9a86-025041000001,priority=4, timestamp=Tue Jul 11 17:01:07 CEST 2023,expiration=0, durable=true, address=VirtualTopic.audit.events,size=897,properties=TypedProperties[__AMQ_CID=c3ad976e-1ffb-11ee-9a86-025041000001,timestamp=1689087667421,_AMQ_ROUTING_TYPE=0]]@1672867470, with Context: RoutingContextImpl(Address=null, routingType=null, PreviousAddress=null previousRoute:null, reusable=null, version=0)
..................................................

2023-07-11 17:01:08.656  INFO 15344 --- [mpl$6@1f9d4b0e)] org.apache.activemq.audit.resource       : AMQ601065: User anonymous@invm:0 is creating a queue on target resource: ServerSessionImpl() [with parameters: [QueueConfiguration [id=null, name=junit, address=VirtualTopic.audit.events, routingType=MULTICAST, filterString=null, durable=true, user=null, maxConsumers=-1, exclusive=null, groupRebalance=null, groupRebalancePauseDispatch=null, groupBuckets=null, groupFirstKey=null, lastValue=null, lastValueKey=null, nonDestructive=null, purgeOnNoConsumers=false, enabled=null, consumersBeforeDispatch=null, delayBeforeDispatch=null, consumerPriority=null, autoDelete=null, autoDeleteDelay=null, autoDeleteMessageCount=null, ringSize=null, configurationManaged=null, temporary=false, autoCreateAddress=null, internal=null, transient=null, autoCreated=true]]]
2023-07-11 17:01:08.656  INFO 15344 --- [mpl$6@1f9d4b0e)] org.apache.activemq.audit.base           : AMQ601019: User anonymous@invm:0 is getting mbean info on target resource: org.apache.activemq.artemis.core.management.impl.QueueControlImpl@6d6ce5c6 []
2023-07-11 17:01:08.661  INFO 15344 --- [mpl$6@1f9d4b0e)] org.apache.activemq.audit.base           : AMQ601265: User anonymous@invm:0 is creating a core consumer on target resource ServerSessionImpl() [with parameters: [0, junit, null, 0, false, true, null]]
2023-07-11 17:01:12.262  INFO 15344 --- [mpl$6@1f9d4b0e)] org.apache.activemq.audit.base           : AMQ601072: User anonymous@invm:0 is deleting a queue on target resource: ServerSessionImpl() [with parameters: [junit]]
2023-07-11 17:01:12.278  INFO 15344 --- [ionShutdownHook] org.apache.activemq.artemis.core.server  : AMQ221002: Apache ActiveMQ Artemis Message Broker version 2.19.1 [c2f03ad7-1ffb-11ee-9a86-025041000001] stopped, uptime 5.995 seconds

My understanding is that the queue junit is created after the message is sent to VirtualTopic.audit.events, so jmsMessagingTemplate.receive("VirtualTopics.audit.events::junit") never receives it.

I worked around that by using a manual configuration of the server and setting addresses and queues before the test starts:


@Autowired
private Configuration embeddedArtemisServerConfiguration;

@Autowired
private EmbeddedActiveMQ embeddedActiveMQ;

def setup() {
    embeddedActiveMQ.stop()

    CoreAddressConfiguration coreAddressConfiguration = new CoreAddressConfiguration();
    coreAddressConfiguration.setName(statusEventDestination); // VirtualTopic.audit.events
    coreAddressConfiguration.addRoutingType(RoutingType.MULTICAST);
embeddedArtemisServerConfiguration.setAddressConfigurations(Collections.singletonList(coreAddressConfiguration));

    QueueConfiguration queueConfiguration = new QueueConfiguration("junit");
    queueConfiguration.setAddress(statusEventDestination);
    embeddedArtemisServerConfiguration.setQueueConfigs();

    embeddedActiveMQ.setConfiguration(embeddedArtemisServerConfiguration)
    embeddedActiveMQ.start()
}

....

I also had to stop and start the server between each test, @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) did not work either.

As a result I get the following logs:

2023-07-11 17:27:35.182  INFO 19936 --- [           main] org.apache.activemq.artemis.core.server  : AMQ221080: Deploying address VirtualTopic.audit.events supporting [MULTICAST]
2023-07-11 17:27:35.182  INFO 19936 --- [           main] org.apache.activemq.audit.base           : AMQ601019: User anonymous@unknown is getting mbean info on target resource: org.apache.activemq.artemis.core.management.impl.AddressControlImpl@7b3feb26 []
2023-07-11 17:27:35.184  INFO 19936 --- [           main] org.apache.activemq.artemis.core.server  : AMQ221003: Deploying MULTICAST queue junit on address VirtualTopic.audit.events
...
...
2023-07-11 17:27:35.234  INFO 19936 --- [           main] org.apache.activemq.artemis.core.server  : AMQ221007: Server is now live
2023-07-11 17:27:35.234  INFO 19936 --- [           main] org.apache.activemq.artemis.core.server  : AMQ221001: Apache ActiveMQ Artemis Message Broker version 2.19.1 [localhost, nodeID=75fdc240-1fff-11ee-b8a6-025041000001] 
2023-07-11 17:27:35.431  INFO 19936 --- [ost-1668271280)] org.apache.activemq.audit.base           : AMQ601267: User anonymous@invm:0 is creating a core session on target resource ActiveMQServerImpl::name=localhost [with parameters: [7624ab35-1fff-11ee-b8a6-025041000001, null, ****, 102400, RemotingConnectionImpl [ID=761c1fb2-1fff-11ee-b8a6-025041000001, clientID=null, nodeID=75fdc240-1fff-11ee-b8a6-025041000001, transportConnection=InVMConnection [serverID=0, id=761c1fb2-1fff-11ee-b8a6-025041000001]], false, false, false, false, null, org.apache.activemq.artemis.core.protocol.core.impl.CoreSessionCallback@14389510, true, org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager$2@22000d51, {}]]
2023-07-11 17:27:35.453  INFO 19936 --- [ost-1668271280)] org.apache.activemq.audit.base           : AMQ601267: User anonymous@invm:0 is creating a core session on target resource ActiveMQServerImpl::name=localhost [with parameters: [76282da6-1fff-11ee-b8a6-025041000001, null, ****, 102400, RemotingConnectionImpl [ID=761c1fb2-1fff-11ee-b8a6-025041000001, clientID=null, nodeID=75fdc240-1fff-11ee-b8a6-025041000001, transportConnection=InVMConnection [serverID=0, id=761c1fb2-1fff-11ee-b8a6-025041000001]], true, true, false, false, null, org.apache.activemq.artemis.core.protocol.core.impl.CoreSessionCallback@53462f7b, true, org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager$2@22000d51, {}]]
2023-07-11 17:27:35.479  INFO 19936 --- [mpl$6@39e3ea78)] org.apache.activemq.audit.message        : AMQ601500: User anonymous@invm:0 is sending a message CoreMessage[messageID=8,durable=true,userID=762bb017-1fff-11ee-b8a6-025041000001,priority=4, timestamp=Tue Jul 11 17:27:35 CEST 2023,expiration=0, durable=true, address=VirtualTopic.audit.events,size=895,properties=TypedProperties[__AMQ_CID=7624ab34-1fff-11ee-b8a6-025041000001,timestamp=1689089255360,_AMQ_ROUTING_TYPE=0]]@538326165, with Context: RoutingContextImpl(Address=null, routingType=null, PreviousAddress=null previousRoute:null, reusable=null, version=0)
..................................................

2023-07-11 17:27:36.536  INFO 19936 --- [mpl$6@39e3ea78)] org.apache.activemq.audit.base           : AMQ601265: User anonymous@invm:0 is creating a core consumer on target resource ServerSessionImpl() [with parameters: [0, junit, null, 0, false, true, null]]
2023-07-11 17:27:36.551  INFO 19936 --- [mpl$6@39e3ea78)] org.apache.activemq.audit.message        : AMQ601501: User anonymous@invm:0 is consuming a message from junit: Reference[8]:RELIABLE:CoreMessage[messageID=8,durable=true,userID=762bb017-1fff-11ee-b8a6-025041000001,priority=4, timestamp=Tue Jul 11 17:27:35 CEST 2023,expiration=0, durable=true, address=VirtualTopic.audit.events,size=895,properties=TypedProperties[__AMQ_CID=7624ab34-1fff-11ee-b8a6-025041000001,timestamp=1689089255360,_AMQ_ROUTING_TYPE=0]]@538326165
2023-07-11 17:27:36.554  INFO 19936 --- [mpl$6@39e3ea78)] org.apache.activemq.audit.message        : AMQ601502: User anonymous@invm:0 is acknowledging a message from junit: CoreMessage[messageID=8,durable=true,userID=762bb017-1fff-11ee-b8a6-025041000001,priority=4, timestamp=Tue Jul 11 17:27:35 CEST 2023,expiration=0, durable=true, address=VirtualTopic.audit.events,size=895,properties=TypedProperties[__AMQ_CID=7624ab34-1fff-11ee-b8a6-025041000001,timestamp=1689089255360,_AMQ_ROUTING_TYPE=0]]@538326165
2023-07-11 17:27:36.557  INFO 19936 --- [mpl$6@39e3ea78)] org.apache.activemq.audit.base           : AMQ601072: User anonymous@invm:0 is deleting a queue on target resource: ServerSessionImpl() [with parameters: [junit]]
2023-07-11 17:27:36.643  INFO 19936 --- [           main] org.apache.activemq.artemis.core.server  : AMQ221002: Apache ActiveMQ Artemis Message Broker version 2.19.1 [75fdc240-1fff-11ee-b8a6-025041000001] stopped, uptime 1.468 seconds

and my test passes

I don't know if there is a way to do this using only the application.yaml file