0

I have an example for JmsListener to receive message from an activeMQ Broker, which starts listening on application start and which runs perfectly well. I now need to start the listener during run-time as not all properties to launch the listener are available on application start, but needs to be requested from user. My problem is, that I don't know a way to start the listner on demand after starting the application. I have in mind to instantiate the @Configuration class and the @Component class during run-time on demand, but I don't know, how to do it and whether it is a common way to do. Would it be possible to help me by some code snippets to let me understand how to do it?

Thanks a lot in advance.

Please find below the coding so far:

Configuration class containing the properties, that are not necessariliy available at application start time:

package com.example.demo;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;

@Configuration
@EnableJms
public class configuration {
    @Value( "${activemq.broker-url}" )
    private String brokerUrl; // = "failover:(tcp://localhost:61616)?initialReconnectDelay=40&warnAfterReconnectAttempts=10&useExponentialBackOff=false";

    @Value( "${activemq.user}" )
    private String user;

    @Value( "${activemq.password}" )
    private String password;

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        if ("".equals(user)) {
            return new ActiveMQConnectionFactory(brokerUrl);
        }
        return new ActiveMQConnectionFactory(user, password, brokerUrl);
    }

    @Bean
    public JmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory,
                                                                   DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        factory.setPubSubDomain(true);
        factory.setClientId("myClientSpring");
        factory.setSubscriptionDurable(true);
        return factory;
    }

    @Bean
    public JmsTemplate jmsTemplate() {
        return new JmsTemplate(connectionFactory());
    }

    @Bean
    public JmsTemplate jmsTemplateTopic() {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
        jmsTemplate.setPubSubDomain(true);
        return jmsTemplate;
    }
}

Here is the code for starting the listener:

package com.example.demo;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@Component
public class consumer {
    @JmsListener( destination = "${activemq.topic-name}", subscription = "mySubSpring", selector = "test=false OR test is null")
    public void listen(String mensagem) {
        System.out.println(mensagem);
    }
}

and the application start coding:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args); }

}

Again, thanks a lot in advance. Every hint is highly appreciated.

Kind regards, Hugoman

Hugoman
  • 1
  • 1
  • Check this -> https://stackoverflow.com/questions/27809838/how-to-instantiate-spring-managed-beans-at-runtime I believe it'll be valuable. – centrumek Sep 26 '22 at 21:42

0 Answers0