0

My aim: i have multiple jobs(Processes) running parallely (seperate threads). i want to implement messaging so that each process can send message(if required) to rabbitmq Server. now i have this

@Configuration
public class SenderConfiguration {

    String content = "";
    String host = "";
    String port = "";
    String userName = "";
    String password = "";
    String queueName = "";
    InputStream input = null;

    public SenderConfiguration() {
        init();
    }

    private void init() {
        Properties prop = new Properties();
        try {
            input = new FileInputStream("R.CONFIGURATION_FILE_PATH");
            host = prop.getProperty("messaging.host");
            port = prop.getProperty("messaging.port");
            userName = prop.getProperty("messaging.userName");
            password = prop.getProperty("messaging.password");
            queueName = prop.getProperty("messaging.queue");
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        template.setRoutingKey(this.queueName);
        return template;
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
                this.host);
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }

    @Bean
    public ScheduledProducer scheduledProducer() {
        return new ScheduledProducer();
    }

    @Bean
    public BeanPostProcessor postProcessor() {
        return new ScheduledAnnotationBeanPostProcessor();
    }

    static class ScheduledProducer {

        @Autowired
        private volatile RabbitTemplate rabbitTemplate;

        private final AtomicInteger counter = new AtomicInteger();

        @Scheduled(fixedRate = 1000)
        public void sendMessage(String message) {
            rabbitTemplate.convertAndSend("Roxy " + counter.incrementAndGet());
        }
    }

}

and to call this from one of my operation

new AnnotationConfigApplicationContext(SenderConfiguration.class);

shall i make it abstract class and my every operation /process should extend it ? what would be best approach ? and can i make above process any better?

1 Answer 1

1

Just use single class with property placeholders...

Use

@Value("${messaging.host}")
String host;

etc.

No need for a subclass for each.

Sign up to request clarification or add additional context in comments.

2 Comments

ok. i did those changes :). calling send message from all my classes would be fine right ? or i can make it any better?
Your question is too vague.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.