Search code examples
activemq-classicmessage-queue

Type mismatch: cannot convert from ActiveMQQueue to Queue


I'm following this tutorial to have my main application run as a server listening to messages and a supporting app/client to send messages using ActiveMQ remote server.

I am getting an issue on the client side when defining the queue as follows:

@Bean Queue queue() {
    return new ActiveMQQueue("remotingQueue");
}

The issue is as follows:

Type mismatch: cannot convert from ActiveMQQueue to Queue

Solutions suggested by Eclipse are:

  • Add cast to 'Queue'
  • Change method return type to ActiveMQueue.

The first solution gets rid of the issue, but when I'm running the app, it fails with the following message:

class org.apache.activemq.command.ActiveMQQueue cannot be cast to class javax.jms.Queue

I'm getting the same issue when I try to run the app without casting.

Using the second solution suggested by Eclipse (i.e. Change method return type to ActiveMQueue) fixes the issue, but I'm not sure if configuring the queue this way will accomplish the goal of syncing up with the queue configured on the server side. The server side defines the queue as follows without issues:

@Bean Queue queue() {
    return new ActiveMQQueue("remotingQueue");
}

Why does this definition works in one app, but it doesn't work in the second app?


Solution

  • Since you are using version 3.1.3 of spring-boot-starter-activemq then you are using this dependency transitively:

        <dependency>
          <groupId>org.apache.activemq</groupId>
          <artifactId>activemq-client-jakarta</artifactId>
          <version>5.18.2</version>
          <scope>compile</scope>
        </dependency>
    

    This is the Jakarta dependency which means all the JMS objects use the jakarta namespace rather than the javax namespace. Therefore, your code should be using, for example, jakarta.jms.Queue instead of javax.jms.Queue.

    I can't say why your other app isn't having the same issue. My guess is that it's using a different dependency or the runtime environment is different.

    Lastly, you should avoid using implementation objects (e.g. ActiveMQQueue) whenever possible as it ruins the portability of your code and nullifies one of the main benefits of using an API like JMS in the first place.