简体   繁体   English

带有RabbitMQ的Spring Cloud Spring Service连接器

[英]Spring Cloud Spring Service Connector with RabbitMQ

I use Spring cloud Spring service connector to connect Rabbitmq service on CloudFoundry. 我使用Spring Cloud Spring服务连接器来连接CloudFoundry上的Rabbitmq服务。

public class CloudConfig extends AbstractCloudConfig {

    @Bean
    public ConnectionFactory rabbitFactory()
    {
         return connectionFactory().rabbitConnectionFactory();
    }
}

But I need to declare a CachingConnectionFactory and set its PublisherConfirms true. 但是我需要声明一个CachingConnectionFactory并将其PublisherConfirms设置为true。 Because we need use publisherConfirm to check ack when we send message to queue. 因为我们需要使用PublisherConfirm在将消息发送到队列时检查ack。 I have no idea about how to inject the connectionFactory which is got from cloud spring service connector. 我不知道如何注入从cloud spring服务连接器获得的connectionFactory。 Or how we could handle this situation. 或者我们如何处理这种情况。

The documentation includes examples of customizing details of the connection provided by Connectors. 文档包括自定义连接器提供的连接详细信息的示例。

In your case, you should be able to do something like this: 就您而言,您应该可以执行以下操作:

@Bean
public RabbitConnectionFactory rabbitFactory() {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("publisherConfirms", true);

    RabbitConnectionFactoryConfig rabbitConfig = new RabbitConnectionFactoryConfig(properties);
    return connectionFactory().rabbitConnectionFactory(rabbitConfig);
}

You can reconfigure the CCF created by the connector as follows: 您可以按以下方式重新配置连接器创建的CCF:

@Bean
public SmartInitializingSingleton factoryConfigurer() {
    return new SmartInitializingSingleton() {

        @Autowired
        private CachingConnectionFactory connectionFactory;

        @Override
        public void afterSingletonsInstantiated() {
            this.connectionFactory.setPublisherConfirms(true);
        }
    };
}

You must be sure not to perform any RabbitMQ operations before the application context is fully initialized (which is best practice anyway). 您必须确保在完全初始化应用程序上下文之前不执行任何RabbitMQ操作(无论如何都是最佳实践)。

This is RabbitTemplate 这是RabbitTemplate

@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory);
    template.setMandatory(true);
    template.setMessageConverter(new Jackson2JsonMessageConverter());
    template.setConfirmCallback((correlationData, ack, cause) -> {
        if (!ack) {
            System.out.println("send message failed: " + cause + correlationData.toString());
        } else {
            System.out.println("Publisher Confirm" + correlationData.toString());
        }
    });
    return template;
}

This is spring-cloud config: 这是spring-cloud config:

@Bean
public ConnectionFactory rabbitConnectionFactory() {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("publisherConfirms", true);
    RabbitConnectionFactoryConfig rabbitConfig = new RabbitConnectionFactoryConfig(properties);
    return connectionFactory().rabbitConnectionFactory(rabbitConfig);
}

When I use this sender to send message.The result is not expected. 当我使用此发件人发送消息时,不会出现预期的结果。

@Component
public class TestSender {

@Autowired
private RabbitTemplate rabbitTemplate;

@Scheduled(cron = "0/5 * *  * * ? ")
public void send() {
System.out.println("===============================================================");
    this.rabbitTemplate.convertAndSend(EXCHANGE, "routingkey", "hello world",
            (Message m) -> {
                m.getMessageProperties().setHeader("tenant", "aaaaa");
                return m;
            }, new CorrelationData(UUID.randomUUID().toString()));
    Date date = new Date();
    System.out.println("Sender Msg Successfully - " + date);
}

} }

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

相关问题 使用Spring Cloud + Spring ampq + Spring Boot + RabbitMQ时ShutdownSignalException - ShutdownSignalException while using Spring Cloud + Spring ampq + Spring Boot + RabbitMQ spring-cloud 环境中的分布式 Rabbitmq - Distributed Rabbitmq within a spring-cloud environment 在Spring Cloud Stream(RabbitMQ)中以编程方式声明绑定? - Programmatically declare bindings in Spring Cloud Stream (RabbitMQ)? 绑定交换以使用Rabbitmq绑定器通过Spring云流进行交换 - Bind exchange to exchange through Spring cloud stream with rabbitmq binder 生成的测试(生产方)在Spring Cloud合同和AMQP / RabbitMQ中失败 - The generated tests(producer side) failed in Spring Cloud contract and AMQP/RabbitMQ Rabbitmq重新启动后,Spring Cloud Bus(AMQP)无法重新创建队列 - Spring cloud bus(AMQP) not recreating queues after rabbitmq restarted 如何在Cloud Foundry上设置Spring Boot RabbitMQ心跳? - How to set Spring Boot RabbitMQ Heartbeat on Cloud Foundry? Spring Cloud流中的BROKER rabbitMq关闭时如何处理? - How to handle when the BROKER rabbitMq is down in Spring cloud stream? Spring Cloud Stream - 功能 - 如何手动确认 rabbitmq 消息? - Spring Cloud Stream - Functions - How to manually acknowledge rabbitmq message? Spring 云 Stream:在没有 allowOverride 的情况下无法连接到 2 个 rabbitmq 集群 - Spring Cloud Stream: Cannot connect to 2 rabbitmq clusters without allowOverride
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM