简体   繁体   English

多个用户如何使用Spring Boot使用Rabbitmq发送消息

[英]how can multiple users send message with rabbitmq using spring boot

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. 我想实现消息传递,以便每个进程都可以将消息(如果需要)发送到Rabbitmq服务器。 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? 我可以使上述过程更好吗?

Just use single class with property placeholders... 只需将单个类与属性占位符一起使用...

Use 采用

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

etc. 等等

No need for a subclass for each. 每个都不需要一个子类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Spring 引导中发送并确认消息传递到 RabbitMQ? - How to send and confirm that message delivered to RabbitMQ in Spring Boot? 使用@SentTo通过Spring Boot和RabbitMq发送消息 - Use @SentTo to send a message with Spring Boot and RabbitMq 如何使用Spring Integration Java DSL将消息发送到Rabbitmq队列 - How to send message to the rabbitmq queue using spring integration java DSL Spring开机骆驼问题使用rest后发消息给rabbitmq - Spring boot Camel problems when using rest post to send a message to rabbitmq 无法使用 header 与 Spring Boot 交换将消息发布到 RabbitMQ - Not able to publish a message onto RabbitMQ using header exchange with Spring Boot 如何使用 RabbitMQ 和 Spring Boot 将批量电子邮件发送到电子邮件地址列表? - How to send Bulk emails to list of email-addresses using RabbitMQ and Spring boot? 如何在Spring Boot Rabbitmq中识别消息路由到的交换机? - How to identify the exchange to which the message is routed to in Spring boot rabbitmq? 如何在spring boot中实现延迟时间rabbitmq公开消息 - How to public message with delay time rabbitmq implement in spring boot 我可以在 rabbitMQ 和 spring 引导中将多个队列绑定到同一个使用者吗? - Can I bind multiple queues to the same consumer in rabbitMQ and spring boot? 我可以使用 RabbitMQ 和 spring 引导开发多租户应用程序吗? - Can I develop multi tenancy application by using RabbitMQ and spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM