mardi 9 janvier 2018

What's a good way to store class properties and load at run time?

I have a messaging producer (RabbitMQ) and depending on what kind of message i have to send, i need to change the routing key and exchange at runtime.

Given this i'd implemented a strategy to load each class with specific properties, but it's not appear a good solution.

For example:

    public class MyProducerStrategy1 extends RabbitMessagingProducer {

    private static final String ROUTING_KEY = "order1";
    private static final String EXCHANGE = "myexchange1";

    @Override
    String getRoutingKey() {
        return ROUTING_KEY;
    }

    @Override
    String getExchange() {
        return EXCHANGE;
    }

    @Override
    public void sendMessage(Message message) {
        super.sendMessage(message);
    }
}


public class MyProducerStrategy2 extends RabbitMessagingProducer {

    private static final String ROUTING_KEY = "fullfilment";
    private static final String EXCHANGE = "myexchange2";

    @Override
    String getRoutingKey() {
        return ROUTING_KEY;
    }

    @Override
    String getExchange() {
        return EXCHANGE;
    }

    @Override
    public void sendMessage(Message message) {
        super.sendMessage(message);
    }
}

public abstract class RabbitMessagingProducer implements MessagingProducerStrategy {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    abstract String getRoutingKey();

    abstract String getExchange();

    @Override
    public void sendMessage(Message message) {
        rabbitTemplate.convertAndSend(getExchange(), getRoutingKey(), message);
    }
}

Does it make sense? or there's another approach to load there properties and have maybe one class?

Aucun commentaire:

Enregistrer un commentaire