0

I have the following Java process which is listening to MyMessageQueue queue of ActiveMQ.And I can see the message getting retrieved using this statement System.out.println("Message Retrieved is:" +message); inside processBrokerQueues method.

@Component
public class MessageConsumer {

    @Autowired
    private JavaMailSender javaMailSender;

    // one instance, reuse
    private final CloseableHttpClient httpClient = HttpClients.createDefault();

    private static Connection connection;

    // URL of the JMS server. DEFAULT_BROKER_URL will just mean that JMS server is on localhost
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    private static String subject = "MyMessageQueue"; //Queue Name
    // default broker URL is : tcp://localhost:61616"
    private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    private static final Logger logger = LoggerFactory.getLogger(MessageConsumer.class);
    private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    // Working Code with JMS 2.0
    @JmsListener(destination = "MyMessageQueue")
    public void processBrokerQueues(String message) throws DaoException, JMSException {


        System.out.println("Message Retrieved is:" + message);


    }

    private void createConnection() throws JMSException {
        // Getting JMS connection from the server
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        connection = connectionFactory.createConnection();
    }

    private void close() throws IOException {
        httpClient.close();
    }

}

I am wondering, if I want to listen to 20-30 different queues, Is it the correct way to add multiple @JmsListener(destination = "MyMessageQueue") with different queue names like MessageQueue1, MessageQueue2 ....... so on and so forth until MessageQueue30?

samabcde
  • 6,988
  • 2
  • 25
  • 41
Tan
  • 1,433
  • 5
  • 27
  • 47
  • Does this answer your question? [Adding Dynamic Number of Listeners(Spring JMS)](https://stackoverflow.com/questions/34063230/adding-dynamic-number-of-listenersspring-jms) – Justin Bertram Aug 04 '21 at 12:35
  • @JustinBertram Not sure. I am wondering if I have to switch to Topics to do that? – Tan Aug 04 '21 at 18:20
  • The `@JmsListener` can be configured for either queue or topic. It doesn't matter. The answer just happened to use topics. – Justin Bertram Aug 04 '21 at 18:22
  • @JustinBertram Ok, I will test that. One more question, on the producer side, I may be sending 10 or more different messages to 10 or more different queues. And in such scenario, I would need to configure `@JmsListner` to listen to 10 or more queues. Wanted to ask there are no Topic related requirement on the producer side as well? Thanks! – Tan Aug 04 '21 at 18:30
  • I don't understand what you're asking. What do you mean by "topic related requirement on the producer side"? This is JMS. You can use queues *or* topics. There's nothing requiring you to use topics. – Justin Bertram Aug 04 '21 at 18:42
  • I haven't tried this as of yet as we are still in the design phase of aproject. I may try to create something similar to this locally and test it. – Tan Aug 10 '21 at 23:50
  • Did you ever get a chance to test this? – Justin Bertram Aug 20 '21 at 17:14
  • I didn't. Are you looking for some info after my testing? – Tan Aug 21 '21 at 17:37
  • It benefits the community when questions are resolved conclusively so I'm trying to determine whether or not you found a correct answer to your question. – Justin Bertram Aug 21 '21 at 17:40
  • I will test it sometime locally and update my post. My project designed changed and instead of ActiveMQ, the stored procedure is handling a lot of stuff. But I intend to test this locally and see how it goes, just for my curiosity. – Tan Aug 24 '21 at 16:05

1 Answers1

0

You can use wildcard topic names. A simple JUnit test is as follows...

public EmbeddedActiveMQBroker embeddedBroker = new EmbeddedActiveMQBroker();
@Autowired JmsTemplate jmsTemplate;

@Test
void testListener() throws Exception{
    jmsTemplate.convertAndSend("MyMessageQueue.1", "Test");

}

@JmsListener(destination = "MyMessageQueue.>")
public void processBrokerQueues(ActiveMQTextMessage msg) throws Exception {
    System.out.println("Message Retrieved is:" + msg.getText() + " TopicName:" + msg.getDestination().getPhysicalName());
}

Related dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
    <version>2.5.3</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq.tooling</groupId>
    <artifactId>activemq-junit</artifactId>
    <version>5.16.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>
Kemal Kaplan
  • 932
  • 8
  • 21