Switching between Spring Profiles

70 views Asked by At

I want to use ArtemisTopic in local profile and the JmsTopic in default profile. I have separate configurtion files for JMX stuff with Servicebus and ActiveMQ Artemis stuff. The idea is to use ActiveMQ Artemis for local development. How to achieve this?

@Service
public class Publisher{

    private static final JmsTopic JmsTopic = new JmsTopic("raw");
    private static final ActiveMQTopic ArtemisTopic = new ActiveMQTopic("raw");

    public void send(Request request) {

        try {
            jmsTemplate.convertAndSend(ArtemisTopic, mapper.writeValueAsString(request), message -> {
                return message;
            });
        } catch (JsonProcessingException exception) {
            LOGGER.log(System.Logger.Level.ERROR, exception);
        }
    }
}

Adding the configuration:

@Configuration
@EnableJms
@Profile("local")
@PropertySource("classpath:application-local.properties")
public class MessagingConfigArtemis {

    private static final String ARTEMIS_BROKER_URL = "tcp://localhost:61616";
    private static final String ARTEMIS_USERNAME = "artemis";
    private static final String ARTEMIS_PASSWORD = "artemis";

    @Bean
    public ConnectionFactory jmsConnectionFactory() throws JMSException {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(ARTEMIS_BROKER_URL);
        connectionFactory.setUser(ARTEMIS_USERNAME);
        connectionFactory.setPassword(ARTEMIS_PASSWORD);
        return connectionFactory;
    }

    @Bean
    public JmsTemplate jmsTemplate() throws JMSException {
        JmsTemplate jmsTemplate = new JmsTemplate(jmsConnectionFactory());
        return jmsTemplate;
    }

    @Bean
    public JmsListenerContainerFactory<DefaultMessageListenerContainer> jmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory returnValue = new DefaultJmsListenerContainerFactory();
        returnValue.setConnectionFactory(connectionFactory);
        return returnValue;
    }

    @Bean
    public JmsListenerContainerFactory<DefaultMessageListenerContainer> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory returnValue = new DefaultJmsListenerContainerFactory();
        returnValue.setConnectionFactory(connectionFactory);
        returnValue.setSubscriptionShared(true);
        returnValue.setSubscriptionDurable(true);
        returnValue.setPubSubDomain(Boolean.TRUE);
        return returnValue;
    }
}
0

There are 0 answers